{"id":1730,"library":"statsig-python-core","title":"Statsig Python Core SDK","description":"The Statsig Python Core SDK (`statsig-python-core`) provides server-side Python bindings for integrating Statsig's feature flagging, A/B testing, and experimentation platform into your applications. It enables evaluating feature gates, dynamic configs, and experiments, and logging exposures without requiring a client-side Statsig SDK. It's currently at version 0.18.1 and receives regular updates, typically aligning with feature releases.","status":"active","version":"0.18.1","language":"en","source_language":"en","source_url":"https://github.com/statsig-io/python-sdk/tree/main/packages/statsig-python-core","tags":["statsig","feature-flags","experimentation","sdk","ab-testing","server-side"],"install":[{"cmd":"pip install statsig-python-core","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"HTTP client for API communication","package":"httpx"},{"reason":"Data validation and settings management","package":"pydantic"},{"reason":"Cryptographic operations for payload decryption","package":"pycryptodomex"}],"imports":[{"symbol":"StatsigServer","correct":"from statsig import StatsigServer"}],"quickstart":{"code":"import asyncio\nimport os\nfrom statsig import StatsigServer\n\nasync def main():\n    # Replace with your actual Server Secret Key, or set as STATSIG_SERVER_SECRET_KEY env var\n    sdk_key = os.environ.get(\"STATSIG_SERVER_SECRET_KEY\", \"YOUR_SERVER_SECRET_KEY_HERE\")\n\n    if sdk_key == \"YOUR_SERVER_SECRET_KEY_HERE\":\n        print(\"Please set your STATSIG_SERVER_SECRET_KEY environment variable or replace the placeholder.\")\n        return\n\n    await StatsigServer.initialize(sdk_key)\n\n    # Define a user object using a dictionary (StatsigUser class is deprecated)\n    user = {\n        \"userID\": \"test-user-123\",\n        \"email\": \"test@example.com\",\n        \"custom\": {\"plan\": \"enterprise\"}\n    }\n\n    # Check a feature gate\n    if await StatsigServer.check_gate(user, \"my_feature_gate\"): # Replace with your gate name\n        print(\"my_feature_gate is ON for this user!\")\n    else:\n        print(\"my_feature_gate is OFF for this user.\")\n\n    # Get a dynamic config\n    config = await StatsigServer.get_config(user, \"my_dynamic_config\") # Replace with your config name\n    string_value = config.get(\"string_param\", \"default_string\")\n    int_value = config.get(\"int_param\", 0)\n    print(f\"Config values: string_param={string_value}, int_param={int_value}\")\n\n    # Get an experiment\n    experiment = await StatsigServer.get_experiment(user, \"my_experiment\") # Replace with your experiment name\n    variant_value = experiment.get(\"variant_param\", \"control\")\n    print(f\"Experiment variant: {variant_value}\")\n\n    # IMPORTANT: Call shutdown when your application exits to ensure metrics are flushed\n    await StatsigServer.shutdown()\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates how to initialize the Statsig server SDK, define a user, check a feature gate, retrieve dynamic configs and experiments, and properly shut down the SDK. Ensure you replace `YOUR_SERVER_SECRET_KEY_HERE` with your actual Statsig server secret key and adjust gate/config/experiment names."},"warnings":[{"fix":"Prefix calls to `initialize()` and `shutdown()` with `await` and ensure they are run within an async context (e.g., `asyncio.run(main())`).","message":"The `StatsigServer.initialize()` and `StatsigServer.shutdown()` methods are now asynchronous and must be awaited.","severity":"breaking","affected_versions":"0.16.0+"},{"fix":"Ensure your `sdk_key` provided to `StatsigServer.initialize()` is a server-side secret key obtained from your Statsig console.","message":"You must use a 'Server Secret Key' (prefixed with `secret-`) for `StatsigServer`. Using a 'Client Key' (prefixed with `client-`) will lead to authentication errors and incorrect behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always call `await StatsigServer.shutdown()` as part of your application's graceful exit process to ensure all queued events are flushed.","message":"Failing to call `StatsigServer.shutdown()` when your application exits can result in unsent metrics and exposures, leading to incomplete data in Statsig.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Instead of `StatsigUser('user-id')`, use a dictionary like `{'userID': 'user-id', 'email': 'example@example.com'}`.","message":"The `StatsigUser` class has been removed. User data should now be passed as a dictionary.","severity":"breaking","affected_versions":"0.10.0+"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}