OpenFeature Provider for Flagsmith
The `openfeature-provider-flagsmith` library provides an OpenFeature-compliant provider for Flagsmith. It allows applications to integrate Flagsmith's feature flag capabilities using the OpenFeature SDK, abstracting the underlying feature flagging system. The current version is 0.1.6, with releases typically tied to updates in OpenFeature SDK or Flagsmith SDK, or feature enhancements.
Common errors
-
ModuleNotFoundError: No module named 'openfeature_provider_flagsmith'
cause The `openfeature-provider-flagsmith` library has not been installed in the current Python environment.fixRun `pip install openfeature-provider-flagsmith`. -
TypeError: FlagsmithProvider.__init__ missing 1 required positional argument: 'environment_key'
cause The `environment_key` argument, which is your Flagsmith environment's unique key, was not provided during the instantiation of `FlagsmithProvider`.fixPass your Flagsmith Environment Key: `provider = FlagsmithProvider(environment_key="YOUR_FLAGSMITH_ENVIRONMENT_KEY")`. -
flagsmith.exceptions.FlagsmithAPIError: Network Error ...
cause The `flagsmith` SDK failed to connect to the Flagsmith API due to network issues, an incorrect API key, or an invalid base URL (if customized).fixVerify your internet connection, ensure the `environment_key` is correct, and check if the Flagsmith API is reachable. If you're self-hosting Flagsmith, confirm your `api_url` parameter is correctly configured (not shown in quickstart, but an option for `FlagsmithProvider`).
Warnings
- gotcha The `environment_key` is a mandatory argument for `FlagsmithProvider`. Failure to provide a valid key will lead to errors during initialization or feature flag evaluation.
- gotcha The provider relies on network connectivity to the Flagsmith API. Issues like network timeouts or incorrect API endpoints (if customized) can prevent feature flags from being resolved.
- gotcha The OpenFeature `get_value` methods (e.g., `get_boolean_value`) require a default value. This default is returned if the flag is not found in Flagsmith, if the provider is not configured correctly, or if an error occurs during evaluation.
- deprecated The `flagsmith` SDK dependency has minimum version `4.0.0` and maximum `5.x.x` (i.e., `<6.0.0`). Newer versions of the `flagsmith` SDK (e.g., `6.x.x`) may introduce breaking changes not yet supported by this provider.
Install
-
pip install openfeature-provider-flagsmith
Imports
- FlagsmithProvider
from openfeature_provider_flagsmith.provider import FlagsmithProvider
from openfeature_provider_flagsmith import FlagsmithProvider
- OpenFeatureAPI
from openfeature import OpenFeatureAPI
Quickstart
import os
from openfeature import OpenFeatureAPI
from openfeature_provider_flagsmith import FlagsmithProvider
# Ensure you have your Flagsmith Environment Key set as an environment variable
# Or replace os.environ.get() with your actual key directly (not recommended for production)
FLAGSMITH_ENVIRONMENT_KEY = os.environ.get("FLAGSMITH_ENVIRONMENT_KEY", "")
if not FLAGSMITH_ENVIRONMENT_KEY:
print("Error: FLAGSMITH_ENVIRONMENT_KEY environment variable not set.")
print("Please set it to your Flagsmith Environment Key to run this example.")
else:
# 1. Initialize the Flagsmith Provider
provider = FlagsmithProvider(environment_key=FLAGSMITH_ENVIRONMENT_KEY)
# 2. Set the provider globally for OpenFeature
OpenFeatureAPI.set_provider(provider)
# 3. Get an OpenFeature client
client = OpenFeatureAPI.get_client()
# 4. Evaluate a feature flag (assuming 'my_boolean_flag' exists in Flagsmith)
# Provide a default value (False) in case the flag is not found or an error occurs
is_feature_enabled = client.get_boolean_value("my_boolean_flag", False)
print(f"Feature 'my_boolean_flag' is enabled: {is_feature_enabled}")
# Example with a string flag
welcome_message = client.get_string_value("welcome_message", "Hello, World!")
print(f"Welcome message: {welcome_message}")