Skip to main content
Version: Config V1 (legacy)

OpenFeature Provider for Python

CI PyPI PyPI

ConfigCat OpenFeature Provider for Python on GitHub

Getting started

1. Install the provider

pip install configcat-openfeature-provider

2. Initialize the provider

The ConfigCatProvider constructor takes the SDK key and an optional ConfigCatOptions argument containing the additional configuration options for the ConfigCat Python SDK:

from configcatclient import ConfigCatOptions, PollingMode
from openfeature import api
from configcat_openfeature_provider import ConfigCatProvider

# Configure the OpenFeature API with the ConfigCat provider.
api.set_provider(
ConfigCatProvider(
"#YOUR-SDK-KEY#",
# Configure the ConfigCat SDK.
ConfigCatOptions(
polling_mode=PollingMode.auto_poll(60),
),
)
)

# Create a client.
client = api.get_client()

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

3. Evaluate your feature flag

is_awesome_feature_enabled = client.get_boolean_value("isAwesomeFeatureEnabled", False)
if is_awesome_feature_enabled:
do_the_new_thing()
else:
do_the_old_thing()

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
targeting_keyidentifier
Emailemail
Countrycountry
Any othercustom

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

context = EvaluationContext(
targeting_key='#SOME-USER-ID#',
attributes={
'Email': '[email protected]',
'Country': 'CountryID',
'Rating': 4.5,
'RegisteredAt': datetime.fromisoformat('2023-11-22 12:34:56 +00:00'),
'Roles': [ 'Role1', 'Role2' ]
}
)

is_awesome_feature_enabled = client.get_boolean_value('isAwesomeFeatureEnabled', False, context);

Look under the hood