DevCycle Python Server SDK

3.13.7 · active · verified Fri Apr 17

The DevCycle Python Server SDK provides real-time feature flag delivery and experimentation capabilities for Python applications. It allows developers to define, manage, and evaluate feature flags and A/B tests to deliver personalized experiences. The SDK fetches configurations, evaluates variables, and tracks events. The current version is 3.13.7, with frequent releases addressing bug fixes and minor feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the DevCycle client, waits for it to fetch configurations, defines a user, and evaluates a boolean and a string variable. It uses an environment variable for the SDK key and demonstrates asynchronous usage, which is mandatory for this SDK.

import os
import asyncio
from devcycle_python_sdk import DevCycleClient, DevCycleOptions, DevCycleUser

DEVCYCLE_SERVER_SDK_KEY = os.environ.get('DEVCYCLE_SERVER_SDK_KEY', '')

async def main():
    if not DEVCYCLE_SERVER_SDK_KEY:
        print("Error: DEVCYCLE_SERVER_SDK_KEY environment variable not set.")
        return

    # Configure options, e.g., enable cloud bucketing for server-side evaluation
    # By default, local bucketing (WASM) is used if not specified
    options = DevCycleOptions(enable_cloud_bucketing=True)
    client = DevCycleClient(DEVCYCLE_SERVER_SDK_KEY, options)

    try:
        # Wait for the client to be initialized and fetch configurations
        await client.on_initialized()
        print("DevCycle client initialized.")

        # Define a user for feature evaluation
        user = DevCycleUser(
            user_id="example-user",
            email="test@example.com",
            country="US"
        )

        # Evaluate a boolean variable
        feature_enabled = await client.variable(user, "my-feature", False)
        if feature_enabled.value:
            print("Feature 'my-feature' is ON for the user.")
        else:
            print("Feature 'my-feature' is OFF for the user.")

        # Evaluate a string variable
        welcome_message = await client.variable(user, "welcome-text", "Hello!")
        print(f"Welcome message for user: {welcome_message.value}")

    except Exception as e:
        print(f"An error occurred during DevCycle operations: {e}")
    finally:
        # Ensure the client is closed to release resources
        await client.close()
        print("DevCycle client closed.")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →