OpenFeature Provider for Flagsmith

0.1.6 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `FlagsmithProvider` with your Flagsmith Environment Key, register it with the OpenFeature API, and then use the OpenFeature client to evaluate boolean and string feature flags. Ensure your `FLAGSMITH_ENVIRONMENT_KEY` is set as an environment variable.

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}")

view raw JSON →