{"library":"launchdarkly-server-sdk","code":"import os\nfrom launchdarkly_server_sdk import LdClient, Config\n\n# Replace with your actual SDK key from LaunchDarkly\nSDK_KEY = os.environ.get('LAUNCHDARKLY_SDK_KEY', 'YOUR_SDK_KEY_HERE')\n\nif not SDK_KEY or SDK_KEY == 'YOUR_SDK_KEY_HERE':\n    print(\"Warning: LAUNCHDARKLY_SDK_KEY environment variable not set or default used. Client will be in offline mode.\")\n    # In a real application, you might raise an error or handle this differently.\n    config = Config(offline=True)\nelse:\n    config = Config()\n\n# Initialize the LaunchDarkly client\nld_client = LdClient(SDK_KEY, config)\n\n# Wait for the client to be initialized (recommended for server-side apps)\nif ld_client.is_initialized():\n    print(\"LaunchDarkly client initialized!\")\n\n    # Define a user for flag evaluation\n    user = {\n        \"key\": \"example-user-key\",\n        \"name\": \"Example User\",\n        \"custom\": {\n            \"group\": \"beta_testers\"\n        }\n    }\n\n    # Evaluate a feature flag\n    flag_value = ld_client.variation(\"my-feature-flag\", user, False)\n    print(f\"Feature flag 'my-feature-flag' is: {flag_value} for user {user['key']}\")\n\n    # Don't forget to shut down the client when your application exits\n    ld_client.close()\n    print(\"LaunchDarkly client closed.\")\nelse:\n    print(\"LaunchDarkly client failed to initialize.\")\n    # Ensure the client is closed even if initialization fails\n    ld_client.close()\n","lang":"python","description":"This quickstart demonstrates how to initialize the LaunchDarkly client, define a user, evaluate a feature flag, and properly shut down the client. It uses an environment variable for the SDK key for security. In production, ensure the client is initialized and shut down gracefully with your application's lifecycle.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}