Skip to main content
Version: Config V1 (legacy)

OpenFeature Provider for Java

ConfigCat OpenFeature Provider for Java on GitHub

Getting started

1. Install the provider

build.gradle
dependencies {
implementation 'dev.openfeature.contrib.providers:configcat:0.0.4'
}

2. Initialize the provider

The ConfigCatProvider constructor takes a ConfigCatProviderConfig argument containing the configuration options for the ConfigCat Java SDK:

// Build options for the ConfigCat SDK.
ConfigCatProviderConfig configCatProviderConfig = ConfigCatProviderConfig.builder()
.sdkKey("#YOUR-SDK-KEY#")
.pollingMode(PollingModes.autoPoll())
.logLevel(LogLevel.WARNING)
.build();

// Configure the provider.
OpenFeatureAPI.getInstance().setProviderAndWait(new ConfigCatProvider(configCatProviderConfig));

// Create a client.
Client client = OpenFeatureAPI.getInstance().getClient();

For more information about all the configuration options, see the Java SDK documentation.

3. Evaluate your feature flag

boolean isAwesomeFeatureEnabled = client.getBooleanValue("isAwesomeFeatureEnabled", false);
if(isAwesomeFeatureEnabled)
{
doTheNewThing();
}
else
{
doTheOldThing();
}

Evaluation Context

An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.

The following table shows how the different context attributes are mapped to User Object attributes.

Evaluation contextUser ObjectRequired
TargetingKeyidentifier
Emailemail
Countrycountry
Any othercustom

To evaluate feature flags for a context, use the OpenFeature Evaluation API:

MutableContext evaluationContext = new MutableContext();
evaluationContext.setTargetingKey("#SOME-USER-ID#");
evaluationContext.add("Email", "[email protected]");
evaluationContext.add("Country", "CountryID");
evaluationContext.add("Rating", 4.5);

boolean isAwesomeFeatureEnabled = client.getBooleanValue("isAwesomeFeatureEnabled", false, context);

Look under the hood