Flagsmith Python SDK

5.1.1 · active · verified Sun Apr 12

The Flagsmith Python SDK enables you to integrate feature flags and remote configuration into your Python applications. It allows for managing features across multiple projects, environments, and organizations, facilitating controlled rollouts and A/B testing. The current version is 5.1.1, and it typically sees regular releases with bug fixes, features, and dependency updates.

Warnings

Install

Imports

Quickstart

Initializes the Flagsmith client, retrieves environment flags, checks feature status, gets feature values, and demonstrates how to get flags for a specific identity with traits. It highlights the use of local evaluation for performance and secret management via environment variables.

import os
from flagsmith import Flagsmith

# Replace 'YOUR_ENVIRONMENT_KEY' with your actual Flagsmith Environment Key
# It's recommended to use environment variables for keys in production.
# Server-side Environment Keys begin with 'ser.'
environment_key = os.environ.get('FLAGSMITH_ENVIRONMENT_KEY', 'YOUR_ENVIRONMENT_KEY')

# Initialize Flagsmith client
# enable_local_evaluation=True is recommended for production for performance
# and resilience, as it polls the API in a separate thread.
flagsmith = Flagsmith(
    environment_key=environment_key,
    enable_local_evaluation=True
)

# Wait for initial flags to be loaded if using local evaluation
# In a real application, you might want to handle this asynchronously
# or with a proper startup sequence.
flagsmith.update_environment()

# Get all flags for the environment
environment_flags = flagsmith.get_environment_flags()

# Check if a feature is enabled
if environment_flags.is_feature_enabled('my_feature'):
    print("My feature is enabled!")

# Get a feature value
feature_value = environment_flags.get_feature_value('another_feature')
print(f"Value of 'another_feature': {feature_value}")

# Get flags for an identity
# Traits will automatically be persisted against the identity in Remote Evaluation mode.
# In Local Evaluation mode, only traits provided at runtime are used.
identity_flags = flagsmith.get_identity_flags(identifier='test_user_123', traits={'plan': 'premium'})
if identity_flags.is_feature_enabled('premium_feature'):
    print("Premium feature is enabled for test_user_123!")

# Clean up / close the Flagsmith client (important for local evaluation threads)
flagsmith.close()

view raw JSON →