Skip to main content

C++ SDK Reference

Star on GitHub Build Status Coverage Status

ConfigCat C++ SDK on GitHub

Getting Started:

1. Add the ConfigCat SDK to your project

With Vcpkg

  • On Windows:

    git clone https://github.com/microsoft/vcpkg.\vcpkg\bootstrap-vcpkg.bat.\vcpkg\vcpkg install configcat

    In order to use vcpkg with Visual Studio, run the following command (may require administrator elevation):

    .\vcpkg\vcpkg integrate install

    After this, you can create a New non-CMake Project (or open an existing one). All installed libraries are immediately ready to be #included and used in your project without additional setup.

  • On Linux/Mac:

    git clone https://github.com/microsoft/vcpkg./vcpkg/bootstrap-vcpkg.sh./vcpkg/vcpkg install configcat

2. Include configcat.h header in your application code:

#include <configcat/configcat.h>using namespace configcat;

3. Create the ConfigCat client with your SDK Key

auto client = ConfigCatClient::get("#YOUR-SDK-KEY#");

4. Get your setting value

bool isMyAwesomeFeatureEnabled = client->getValue("isMyAwesomeFeatureEnabled", false);if (isMyAwesomeFeatureEnabled) {    doTheNewThing();} else {    doTheOldThing();}

5. Close ConfigCat client​

You can safely shut down all clients at once or individually and release all associated resources on application exit.

ConfigCatClient::closeAll(); // closes all clientsConfigCatClient::close(client); // closes a specific client

Setting up the ConfigCat Client

ConfigCat Client is responsible for:

  • managing the communication between your application and ConfigCat servers.
  • caching your setting values and feature flags.
  • serving values quickly in a failsafe way.

ConfigCatClient::get(<sdkKey>) returns a client with default options.

PropertiesDescription
baseUrlOptional, sets the CDN base url (forward proxy, dedicated subscription) from where the sdk will download the configurations.
dataGovernanceOptional, defaults to Global. Describes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. More about Data Governance. Available options: Global, EuOnly.
connectTimeoutMsOptional, defaults to 8000ms. Sets the amount of milliseconds to wait for the server to make the initial connection (i.e. completing the TCP connection handshake). 0 means it never times out during transfer
readTimeoutMsOptional, defaults to 5000ms. Sets the amount of milliseconds to wait for the server to respond before giving up. 0 means it never times out during transfer.
modeOptional, sets the polling mode for the client. More about polling modes.
cacheOptional, sets a custom cache implementation for the client. More about cache.
overrideOptional, sets the local feature flag & setting overrides. More about feature flag overrides.
ConfigCatOptions options;options.mode = PollingMode::manualPoll();auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);
caution

We strongly recommend you to use the ConfigCatClient as a Singleton object in your application. The ConfigCatClient constructs singleton client instances for your SDK keys with its ConfigCatClient::get(<sdkKey>) static factory method. These clients can be closed all at once with ConfigCatClient::closeAll() method or individually with the ConfigCatClient::close(client).

Anatomy of getValue()

ParametersDescription
keyREQUIRED. Setting-specific key. Set on ConfigCat Dashboard for each setting.
defaultValueREQUIRED. This value will be returned in case of an error.
userOptional, User Object. Essential when using Targeting. Read more about Targeting.
auto user = ConfigCatUser("#USER-IDENTIFIER#");auto value = client->getValue(    "keyOfMySetting", // key    false, // defaultValue    &user, // Optional User Object);

User Object

The User Object is essential if you'd like to use ConfigCat's Targeting feature.

auto user = ConfigCatUser("435170f4-8a8b-4b67-a723-505ac7cdea92");
auto user = ConfigCatUser("[email protected]");

Customized user object creation:

ArgumentDescription
idREQUIRED. Unique identifier of a user in your application. Can be any value, even an email address.
emailOptional parameter for easier targeting rule definitions.
countryOptional parameter for easier targeting rule definitions.
customOptional dictionary for custom attributes of a user for advanced targeting rule definitions. e.g. User role, Subscription type.
auto user = ConfigCatUser(    "435170f4-8a8b-4b67-a723-505ac7cdea92", // id    "[email protected]", // email    "United Kingdom", // country    {        {"SubscriptionType": "Pro"},        {"UserRole": "Admin"}    } // custom);

Polling Modes

The ConfigCat SDK supports 3 different polling mechanisms to acquire the setting values from ConfigCat. After latest setting values are downloaded, they are stored in the internal cache then all getValue() calls are served from there. With the following polling modes, you can customize the SDK to best fit to your application's lifecycle.
More about polling modes.

Auto polling (default)

The ConfigCat SDK downloads the latest values and stores them automatically every 60 seconds.

Use the autoPollIntervalInSeconds option parameter of the PollingMode::autoPoll() to change the polling interval.

auto autoPollIntervalInSeconds = 100;ConfigCatOptions options;options.mode = PollingMode::autoPoll(autoPollIntervalInSeconds);auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

Adding a callback to onConfigChanged option parameter will get you notified about changes.

auto autoPollIntervalInSeconds = 100;auto maxInitWaitTimeInSeconds = 5;auto onConfigChanged = [] {    // here you can subscribe to configuration changes};ConfigCatOptions options;options.mode = PollingMode::autoPoll(autoPollIntervalInSeconds, maxInitWaitTimeInSeconds, onConfigChanged);auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

Available options:

Option ParameterDescriptionDefault
autoPollIntervalInSecondsPolling interval.60
maxInitWaitTimeInSecondsMaximum waiting time between the client initialization and the first config acquisition in seconds.5
onConfigChangedCallback to get notified about changes.-

Lazy Loading

When calling getValue() the ConfigCat SDK downloads the latest setting values if they are not present or expired in the cache. In this case the getValue() will return the setting value after the cache is updated.

Use the cacheRefreshIntervalInSeconds option parameter of the PollingMode::lazyLoad() to set cache lifetime.

auto cacheRefreshIntervalInSeconds = 100;ConfigCatOptions options;options.mode = PollingMode::lazyLoad(cacheRefreshIntervalInSeconds);auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

Available options:

ParameterDescriptionDefault
cacheRefreshIntervalInSecondsCache TTL.60

Manual Polling

Manual polling gives you full control over when the config.json (with the setting values) is downloaded. ConfigCat SDK will not update them automatically. Calling forceRefresh() is your application's responsibility.

ConfigCatOptions options;options.mode = PollingMode::manualPoll();auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);client->forceRefresh();

getValue() returns defaultValue if the cache is empty. Call forceRefresh() to update the cache.

Flag Overrides

With flag overrides you can overwrite the feature flags & settings downloaded from the ConfigCat CDN with local values. Moreover, you can specify how the overrides should apply over the downloaded values. The following 3 behaviours are supported:

  • Local/Offline mode (OverrideBehaviour::LocalOnly): When evaluating values, the SDK will not use feature flags & settings from the ConfigCat CDN, but it will use all feature flags & settings that are loaded from local-override sources.

  • Local over remote (OverrideBehaviour::LocalOverRemote): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the local-override version will take precedence.

  • Remote over local (OverrideBehaviour::RemoteOverLocal): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the downloaded version will take precedence.

You can set up the SDK to load your feature flag & setting overrides from a file or a map.

JSON File

The SDK can be set up to load your feature flag & setting overrides from a file.

File

ConfigCatOptions options;options.override = make_shared<FlagOverrides>(make_shared<FileOverrideDataSource>("path/to/the/local_flags.json"), LocalOnly);auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

JSON File Structure

The SDK supports 2 types of JSON structures to describe feature flags & settings.

1. Simple (key-value) structure
{  "flags": {    "enabledFeature": true,    "disabledFeature": false,    "intSetting": 5,    "doubleSetting": 3.14,    "stringSetting": "test"  }}

This is the same format that the SDK downloads from the ConfigCat CDN. It allows the usage of all features you can do on the ConfigCat Dashboard.

You can download your current config.json from ConfigCat's CDN and use it as a baseline.

The URL to your current config.json is based on your Data Governance settings:

  • GLOBAL: https://cdn-global.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v5.json
  • EU: https://cdn-eu.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v5.json
{  "f": {    // list of feature flags & settings    "isFeatureEnabled": {      // key of a particular flag      "v": false, // default value, served when no rules are defined      "i": "430bded3", // variation id (for analytical purposes)      "t": 0, // feature flag's type, possible values:      // 0 -> BOOLEAN      // 1 -> STRING      // 2 -> INT      // 3 -> DOUBLE      "p": [        // list of percentage rules        {          "o": 0, // rule's order          "v": true, // value served when the rule is selected during evaluation          "p": 10, // % value          "i": "bcfb84a7" // variation id (for analytical purposes)        },        {          "o": 1, // rule's order          "v": false, // value served when the rule is selected during evaluation          "p": 90, // % value          "i": "bddac6ae" // variation id (for analytical purposes)        }      ],      "r": [        // list of targeting rules        {          "o": 0, // rule's order          "a": "Identifier", // comparison attribute          "t": 2, // comparator, possible values:          // 0  -> 'IS ONE OF',          // 1  -> 'IS NOT ONE OF',          // 2  -> 'CONTAINS',          // 3  -> 'DOES NOT CONTAIN',          // 4  -> 'IS ONE OF (SemVer)',          // 5  -> 'IS NOT ONE OF (SemVer)',          // 6  -> '< (SemVer)',          // 7  -> '<= (SemVer)',          // 8  -> '> (SemVer)',          // 9  -> '>= (SemVer)',          // 10 -> '= (Number)',          // 11 -> '<> (Number)',          // 12 -> '< (Number)',          // 13 -> '<= (Number)',          // 14 -> '> (Number)',          // 15 -> '>= (Number)',          // 16 -> 'IS ONE OF (Hashed)',          // 17 -> 'IS NOT ONE OF (Hashed)'          "c": "@example.com", // comparison value          "v": true, // value served when the rule is selected during evaluation          "i": "bcfb84a7" // variation id (for analytical purposes)        }      ]    }  }}

Map

You can set up the SDK to load your feature flag & setting overrides from a map.

const std::unordered_map<std::string, Value>& map = {    { "enabledFeature", true },    { "disabledFeature", false },    { "intSetting", 5 },    { "doubleSetting", 3.14 },    { "stringSetting", "test" }};ConfigCatOptions options;options.override = make_shared<FlagOverrides>(make_shared<MapOverrideDataSource>(map), LocalOnly);auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

getAllKeys()

You can query the keys of each feature flag and setting with the getAllKeys() method.

auto client = ConfigCatClient::get("#YOUR-SDK-KEY#");auto keys = client->getAllKeys();

getAllValues()

Evaluates and returns the values of all feature flags and settings. Passing a User Object is optional.

auto client = ConfigCatClient::get("#YOUR-SDK-KEY#");auto settingValues = client->getAllValues();// invoke with user objectauto user = ConfigCatUser("435170f4-8a8b-4b67-a723-505ac7cdea92");auto settingValuesTargeting = client->getAllValues(&user);

Custom Cache

You have the option to inject your custom cache implementation into the client. All you have to do is to inherit from the ConfigCatCache abstract class:

class MyCustomCache : public ConfigCatCache {public:    const std::string& read(const std::string& key) override {        // here you have to return with the cached value    }    void write(const std::string& key, const std::string& value) override {        // here you have to store the new value in the cache    }};

Then use your custom cache implementation:

ConfigCatOptions options;options.cache = make_shared<MyCustomCache>();auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

Force refresh

Any time you want to refresh the cached configuration with the latest one, you can call the forceRefresh() method of the library, which initiates a new download and updates the local cache.

Using ConfigCat behind a proxy

Provide your own network credentials (username/password), and proxy server settings (proxy server/port) in the ConfigCatOptions.

ConfigCatOptions options;options.proxies = {{"http", "http://www.fake_auth_proxy.com"}}; // Protocol, Proxy urloptions.proxyAuthentications = {    {"http", ProxyAuthentication{"user", "password"}} // Protocol, ProxyAuthentication};auto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

Changing the default HTTP timeout

Set the maximum wait time for a ConfigCat HTTP response by changing the connectTimeoutMs or readTimeoutMs in the ConfigCatOptions. The default connectTimeoutMs is 8 seconds. The default readTimeoutMs is 5 seconds.

ConfigCatOptions options;options.connectTimeoutMs = 10000; // Timeout in milliseconds for establishing a HTTP connection with the serveroptions.readTimeoutMs = 8000; // Timeout in milliseconds for reading the server's HTTP responseauto client = ConfigCatClient::get("#YOUR-SDK-KEY#", options);

Logging

In the ConfigCat SDK there is a ConsoleLogger writes logs to the standard output. You can define a logger with the setLogger function. You can override it with your implementation. The custom implementation must satisfy the ILogger abstract class.

#include <configcat/configcat.h>#include <configcat/consolelogger.h>configcat::ConsoleLogger logger;configcat::setLogger(&logger);

You can change the verbosity of the logs by setting the LogLevel.

configcat::setLogLevel(LOG_LEVEL_INFO);

Available log levels:

LevelDescription
LOG_LEVEL_ERROROnly error level events are logged.
LOG_LEVEL_WARNINGDefault. Errors and Warnings are logged.
LOG_LEVEL_INFOErrors, Warnings and feature flag evaluation is logged.
LOG_LEVEL_DEBUGAll of the above plus debug info is logged. Debug logs can be different for other SDKs.

Info level logging helps to inspect how a feature flag was evaluated:

[Info]: Evaluating getValue(isPOCFeatureEnabled)User object: {    "Email": "[email protected]",    "Identifier": "435170f4-8a8b-4b67-a723-505ac7cdea92",}Evaluating rule: [Email:[email protected]] [CONTAINS] [@something.com] => no matchEvaluating rule: [Email:[email protected]] [CONTAINS] [@example.com] => match, returning: true

Sensitive information handling

The frontend/mobile SDKs are running in your users' browsers/devices. The SDK is downloading a config.json file from ConfigCat's CDN servers. The URL path for this config.json file contains your SDK key, so the SDK key and the content of your config.json file (feature flag keys, feature flag values, targeting rules, % rules) can be visible to your users. This SDK key is read-only, it only allows downloading your config.json file, but nobody can make any changes with it in your ConfigCat account.

If you do not want to expose the SDK key or the content of the config.json file, we recommend using the SDK in your backend components only. You can always create a backend endpoint using the ConfigCat SDK that can evaluate feature flags for a specific user, and call that backend endpoint from your frontend/mobile applications.

Also, we recommend using confidential targeting comparators in the targeting rules of those feature flags that are used in the frontend/mobile SDKs.

Sample Applications

Check out our Sample Application how they use the ConfigCat SDK

Look Under the Hood