Skip to main content

Automating Feature Flags using ConfigCat’s Public API + Cron + cURL

· 6 min read
Endre Toth
David Herbert

ConfigCat hero image

Feature flags are a great way to control the release of new features in your software. They allow you to easily turn new features on or off without redeploying your code.

However, when implementing a feature flag in an application, we may want to customize its management after the initial release, i.e., schedule an incremental release/delivery strategy that is carried out automatically or schedule future changes to a flag's targeting rule that can take effect at specific time intervals.

For example:

  • Activate a flag’s config by date and time. For instance, release a feature at noon on a Sunday afternoon.
  • Gradually increase the discount value of an ongoing promotion at specific times of weekdays.
  • Enable a feature only on weekends or on weekdays.
  • Three days from today, turn on a feature for internal testing and enable it for public use a week later.

What is ConfigCat?

What is the best solution to solve this business need? It's possible to solve this by using a combination of ConfigCat + cURL + Cron jobs = Feature Flags Scheduler.

For context, ConfigCat is a developer-centric feature flag service with unlimited team size, awesome support, and a reasonable price tag. It supports simple feature toggles, user segmentation, and A/B testing and has a generous free tier for low-volume use cases or those just starting out.

Creating a Feature Flags Scheduler with ConfigCat + Cron + cURL

cover image for Automating Feature Flags using ConfigCat’s Public API + Cron + cURL

The ability to schedule feature flags allows you to define and set up future changes to the targeting rules of your flag. You can create custom rules to govern your flag using the following:

cURL

cURL is an open-source command-line tool commonly used for transferring data and interacting with web APIs using URLs over various internet protocols, such as HTTP/S protocol. It is an important Linux tool that is often used in scripts and automated tasks to perform certain actions.

Cron

Cron is a useful utility that allows you schedule time-based jobs, commands, or tasks in Unix-like computer operating systems (alternatively, you can use Task Scheduler or schtasks on the Windows platform). Cron enables users to schedule jobs such as commands or shell scripts to run automatically at a specified time or date.

ConfigCat’s Public Management API

ConfigCat offers a Public Management API as part of its feature flagging service that provides users programmatic access to the ConfigCat platform. Its purpose is to offer the same functionality as their intuitive Dashboard UI for tests, tools, or scripts.

Using the API, you can create, read, update, and delete any flags, configurations, environments, or products within ConfigCat.

Step 1: Setting up Requirements

  • Create the required ConfigCat Public Management API credentials. Follow this setup to get the credential to be able to invoke any ConfigCat Public Management API endpoints.
  • Install cURL in your local machine using this command.
sudo apt install curl
  • Install cron on your machine. Usually, most Linux system distributions come with some form of cron pre-installed by default. However, if you're using an Ubuntu machine on which cron isn't installed, you can install it using.
sudo apt update
sudo apt install cron
sudo systemctl enable cron

Step 2: Creating the Command

With the required tools installed and credentials created, we can now create a command to enable the feature flag value. Open your favorite text editor (I'll be using nano) and create a file e.g., enableMyAwsomeFeature.sh.

nano enableMyAwsomeFeature.sh

Next, insert the following script into the file and replace the API credentials and SDK key with those from your ConfigCat account.

auth_credentials_in_base64="<INSERT YOUR PUBLIC API CREDENTIAL>"
setting_key="myAwesomeFeature"
reason="Enable from cron job"
SDK_KEY="<INSERT YOUR SDK KEY>"

curl -X PUT \
"https://api.configcat.com/v1/settings/${setting_key}/value?reason=${reason}" \
-H "X-CONFIG CAT-SDK KEY: ${SDK_KEY}" \
-H "Authorization: Basic ${auth_credentials_in_base64}" \
-H "Content-Type: application/json" \
-d "{\"value\":true}"

Note: Ensure enableMyAwsomeFeature.sh is executable. In this case, I used chmod 755.

Step 3: Setting up the Cron Job

A cron job is a term that describes a scheduled cron task or activity that should be executed at a certain time. Cron jobs are typically stored in a particular file called crontab in the system. To schedule a job, you need to open up your crontab for editing and create a task. The cron job is written in the form of cron expression, where the expression syntax is broken down into six fields:

  1. Minute
  2. Hour
  3. Day of the month
  4. Month
  5. Day of the week
  6. Command to execute

For example, the cron expression to run my script every Monday at 7:59 AM is: 59 7 * * MON enableMyAwsomeFeature.sh. Cron expressions are pretty powerful and can be used to schedule just about anything.

Note: You can easily compose cron expressions using crontab.guru.

Step 4: Setting up the Crontab

As mentioned earlier, the cron expressions are stored in a file on your system. However, it's not recommended editing this file manually. You can manage your cron jobs safely using the terminal via the crontab -e command. This gives you the list of existing cron jobs in your system (if you don't already have any, your list will be empty).

Each line in this file represents a cron job, and you can register a new job by inserting a new line. For example, add this line to your list: 59 7 * * MON enableMyAwsomeFeature.sh. If you use nano, press CTRL + X, then Y, and finally ENTER to save the new job. You can also check all of your cron jobs by running crontab -l.

However, if you need system-wide cron expressions that are globally accessible on your machine. You could edit the system-wide crontab with the following command: sudo nano /etc/crontab. These types of cron tabs have an additional field to specify the associated user profile. As stated earlier, you can schedule anything with cron expressions. For example:

  • Enable a feature only on weekdays between 8 AM - 5 PM.
 0 8 \* _ 1-5 enableMyAwsomeFeature.sh
0 17 _ \* 1-5 disableMyAwsomeFeature.sh
  • Turn on a feature on release day at a specified time, i.e., on the 6th of May at 10 AM.
0 10 6 5 * enableMyAwsomeFeature.sh

Note: The \* - expression is a cron expression to run a job every year. To ensure execution time accuracy, ensure that your machine's clock is synchronized or use network clock synchronization (NTP).

Conclusion

Launching features and change software configuration without code deployment helps a lot. Feature flags make it relatively easy to control the release of features in your software. Using the ConfigCat feature flagging service, you can combine its Public Management API with cURL + Cron jobs to create a feature flag scheduler that allows you to automate the deployment of changes to your flags, which can come in pretty handy.

To learn more about how to use feature flags to optimize your feature delivery strategy and software development lifecycle, you can follow ConfigCat on Twitter, Facebook, LinkedIn, and GitHub.