Unleash Python Client SDK

6.7.0 · active · verified Sat Apr 11

The Unleash Python Client SDK is a server-side client for the Unleash feature management platform, enabling developers to integrate and evaluate feature flags within their Python applications. It connects to an Unleash server (Enterprise or Open Source) to fetch flag configurations and evaluate them locally against an Unleash context. Currently at version 6.7.0, the library maintains an active release cadence with frequent updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Unleash client, connect to an Unleash server, and evaluate feature flags. It includes examples for checking simple boolean flags, using context for targeted evaluations, and retrieving feature variants. Remember to replace placeholder URLs and API tokens with your actual Unleash server details, ideally through environment variables. The `initialize_client()` method starts background synchronization, and `destroy()` gracefully shuts it down.

import os
from UnleashClient import UnleashClient

UNLEASH_URL = os.environ.get('UNLEASH_API_URL', 'http://localhost:4242/api')
UNLEASH_APP_NAME = os.environ.get('UNLEASH_APP_NAME', 'my-python-app')
UNLEASH_INSTANCE_ID = os.environ.get('UNLEASH_INSTANCE_ID', 'instance-1')
UNLEASH_API_TOKEN = os.environ.get('UNLEASH_API_TOKEN', 'YOUR_API_TOKEN_HERE') # Typically a server-side token

# Initialize the client
client = UnleashClient(
    url=UNLEASH_URL,
    app_name=UNLEASH_APP_NAME,
    instance_id=UNLEASH_INSTANCE_ID,
    custom_headers={'Authorization': UNLEASH_API_TOKEN}
)

# Start the client (connects to Unleash server, fetches flags, etc.)
client.initialize_client()

# Check if a feature is enabled
enabled = client.is_enabled("my_feature_flag")
print(f"Feature 'my_feature_flag' is enabled: {enabled}")

# Check with context
user_context = {
    "userId": "123",
    "sessionId": "abc",
    "properties": {"region": "us-east"}
}
enabled_for_user = client.is_enabled("user_specific_feature", user_context)
print(f"Feature 'user_specific_feature' is enabled for user 123: {enabled_for_user}")

# Get a variant
variant = client.get_variant("variant_toggle", user_context)
print(f"Variant for 'variant_toggle': {variant}")

# Gracefully shut down the client when no longer needed
client.destroy()

view raw JSON →