{"id":1731,"library":"statsig","title":"Statsig Python Server SDK","description":"The Statsig Python Server SDK enables developers to integrate feature flags, A/B tests, and dynamic configurations into their Python applications. It provides server-side evaluation of gates and experiments, ensuring consistent user experiences. The library is actively maintained with frequent minor releases, currently at version 0.71.6.","status":"active","version":"0.71.6","language":"en","source_language":"en","source_url":"https://github.com/statsig-io/python-sdk","tags":["feature flags","experimentation","a/b testing","sdk","analytics"],"install":[{"cmd":"pip install statsig","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for HTTP communication with Statsig APIs, including logging events and fetching configurations.","package":"httpx","optional":false}],"imports":[{"note":"While `StatsigServer` exists, the idiomatic way to interact with the SDK is through the top-level `statsig` module functions like `statsig.initialize()`, `statsig.check_gate()`, etc.","wrong":"from statsig import StatsigServer","symbol":"statsig","correct":"import statsig"},{"symbol":"StatsigUser","correct":"from statsig import StatsigUser"},{"symbol":"StatsigOptions","correct":"from statsig import StatsigOptions"}],"quickstart":{"code":"import statsig\nimport os\nimport asyncio\n\nasync def main():\n    # It is crucial to use a server secret key (sk-...) for the server SDK.\n    # NEVER expose client keys (pk-..., client-...) in your server-side code.\n    secret_key = os.environ.get('STATSIG_SERVER_SECRET', 'YOUR_SERVER_SECRET_KEY')\n\n    if not secret_key or secret_key == 'YOUR_SERVER_SECRET_KEY':\n        print(\"Please set the STATSIG_SERVER_SECRET environment variable or replace 'YOUR_SERVER_SECRET_KEY' in the code.\")\n        return\n\n    # Initialize the SDK. This is an asynchronous operation.\n    print(\"Initializing Statsig SDK...\")\n    await statsig.initialize(secret_key)\n    print(\"Statsig SDK initialized.\")\n\n    # Define a StatsigUser object with relevant attributes\n    user = statsig.StatsigUser(\n        user_id=\"example-user-123\",\n        email=\"user@example.com\",\n        country=\"US\",\n        custom={\n            \"plan\": \"premium\"\n        },\n        private_attributes={\n            \"phone_number\": \"+15551234567\"\n        }\n    )\n\n    # Check a feature gate\n    if statsig.check_gate(user, \"my_feature_gate\"):\n        print(\"Feature 'my_feature_gate' is ON for the user.\")\n    else:\n        print(\"Feature 'my_feature_gate' is OFF for the user.\")\n\n    # Get a dynamic config\n    config = statsig.get_config(user, \"my_dynamic_config\")\n    print(f\"Value for 'my_dynamic_config': {config.value}\")\n\n    # Get an experiment\n    experiment = statsig.get_experiment(user, \"my_a_b_test\")\n    print(f\"Value for 'my_a_b_test': {experiment.value}\")\n\n    # Log an event\n    statsig.log_event(user, \"product_viewed\", value=10.99, metadata={\"product_id\": \"item_xyz\"})\n\n    # Shut down the SDK. This is crucial for flushing all pending events.\n    print(\"Shutting down Statsig SDK...\")\n    await statsig.shutdown()\n    print(\"Statsig SDK shut down.\")\n\nif __name__ == \"__main__\":\n    asyncio.run(main())","lang":"python","description":"This quickstart demonstrates how to initialize the Statsig Python SDK, define a user, check a feature gate, retrieve a dynamic config, fetch an experiment, log an event, and properly shut down the SDK. Remember to replace `YOUR_SERVER_SECRET_KEY` with your actual server secret from the Statsig console, or set it via the `STATSIG_SERVER_SECRET` environment variable."},"warnings":[{"fix":"Ensure you are using the correct server secret key obtained from your Statsig console. Do not confuse it with client keys.","message":"The SDK requires a **Server Secret Key** (starts with `sk-`). Using a Client SDK Key (starts with `pk-` or `client-`) will result in initialization failures and prevent the SDK from fetching configurations and evaluating correctly. Server keys should never be exposed in client-side code.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always call `await statsig.initialize(secret_key)` at the start of your application and `await statsig.shutdown()` before the application terminates. Ensure your application's entry point uses `asyncio.run()` or similar async executor.","message":"Both `statsig.initialize()` and `statsig.shutdown()` are asynchronous operations and **must be awaited**. Failing to `await statsig.initialize()` can lead to the SDK operating with stale or default configurations, and `check_gate`/`get_config` calls may block or return incorrect values. Failing to `await statsig.shutdown()` will result in lost event data as pending events may not be flushed to Statsig servers before your application exits.","severity":"breaking","affected_versions":"All versions, especially when using recent async patterns."},{"fix":"Always provide the most complete and accurate `StatsigUser` object possible. Include `user_id`, and any custom attributes or private attributes that are used in your Statsig rules. Ensure `user_id` is consistent across sessions.","message":"The accuracy of feature gate, dynamic config, and experiment evaluations depends entirely on the attributes provided in the `StatsigUser` object. Incomplete or incorrect user attributes can lead to users being evaluated against the wrong segment or receiving default values, resulting in inconsistent experiences.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}