Python Server SDK
Statsig's Next-gen Python Server SDK built in our [Server Core](/server-core) framework
Set up the SDK
Install the SDK
Installation
shellpip install statsig-python-coreInitialize the SDK
After installation, initialize the SDK using a Server Secret Key from the Statsig console.Keep Server Secret Keys private. If you expose one, you can disable and recreate it in the Statsig console.
An optional
optionsparameter accepts a StatsigOptions object to customize the SDK.pythonfrom statsig_python_core import Statsig, StatsigOptions # note, import statement has underscores while install has dashes options = StatsigOptions() options.environment = "development" statsig = Statsig("secret-key", options) statsig.initialize().wait() # If you're running this in a script, be sure to wait for shutdown at the end to flush event logs to statsig statsig.shutdown().wait()initializeperforms a network request. Afterinitializecompletes, virtually all SDK operations are synchronous (refer to Evaluating Feature Gates in the Statsig SDK). The SDK fetches updates from Statsig in the background independently of API calls.Process forking and WSGI servers
Never fork processes after calling
statsig.initialize(). Doing so puts Statsig in an undefined state and can cause a deadlock.The Python Core SDK uses internal threading and async runtime components that don't work correctly when copied across process boundaries. When a process forks after initialization, these components can become corrupted, leading to:
- Deadlocks in event logging.
- Hanging initialization calls.
- Unpredictable SDK behavior.
- Silent failures in feature evaluation.
Initializing with WSGI servers
For production deployments using WSGI servers like uWSGI or Gunicorn, initialize Statsig after the worker processes are forked, not in the main process.
✅ Correct: uWSGI example
python# app.py from statsig_python_core import Statsig, StatsigOptions from flask import Flask app = Flask(__name__) statsig = None def init_statsig(): global statsig if statsig is None: options = StatsigOptions() options.environment = "production" statsig = Statsig("your-server-secret-key", options) statsig.initialize().wait() # Initialize in each worker process @app.before_first_request def before_first_request(): init_statsig() @app.route('/') def index(): # Use statsig here return "Hello World"ini# uwsgi.ini [uwsgi] module = app:app master = true processes = 4 # Statsig will be initialized in each worker processinitializeperforms a network request. Afterinitializecompletes, virtually all SDK operations are synchronous (refer to Evaluating Feature Gates in the Statsig SDK). The SDK fetches updates from Statsig in the background independently of API calls.
Working with the SDK
Checking a feature flag/gate
After you initialize the SDK, you can fetch a Feature Gate. Feature Gates create logic branches in code that you can roll out to different users from the Statsig Console. Gates are always CLOSED or OFF (equivalent toreturn false;) by default.All APIs require you to specify the user (refer to Statsig user) associated with the request. For example:user = StatsigUser("a-user")
if statsig.check_gate(user, "a_gate"):
# Gate is on, enable new feature
else:
# Gate is off
Reading a dynamic config
Feature Gates are useful for simple on/off switches with optional user targeting. To send a different set of values (strings, numbers, and similar types) to clients based on user attributes such as country, use Dynamic Configs. The API is similar to Feature Gates, but returns a full JSON object configurable on the server from which you can fetch typed parameters.# Get a dynamic config for a specific user
config = statsig.get_dynamic_config(StatsigUser("my_user"), "a_config")
# Access config values with type-safe getters and fallback values
product_name = config.get_string("product_name", "Awesome Product v1") # returns String
price = config.get_float("price", 10.0) # returns float
should_discount = config.get_bool("discount", False) # returns bool
quantity = config.get_integer("quantity", 1) # returns int64
# Advanced Usage:
# You can disable exposure logging for this specific check
options = DynamicConfigEvaluationOptions(disable_exposure_logging=True)
config = statsig.get_dynamic_config(user, "a_config", options)
# The config object also provides metadata about the evaluation
print(config.rule_id) # The ID of the rule that served this config
print(config.id_type) # The type of the evaluation (experiment, config, etc)
The get_dynamic_config() method returns a DynamicConfig object that allows you to:
- Fetch typed values with fallback defaults using
get_string(),get_float(),get_boolean(), andget_integer() - Access evaluation metadata through properties like
rule_idandid_type - Configure evaluation behavior using
DynamicConfigEvaluationOptions
By default, Statsig automatically logs exposures when it evaluates configs. You can disable exposure logging for specific checks using the evaluation options.
Getting a layer/experiment
Use Layers/Experiments to run A/B/n experiments. Two APIs are available. Statsig recommends Layers because they make parameters reusable and support mutually exclusive experiments.# Values via get_layer
layer = statsig.get_layer(StatsigUser("my_user"), "user_promo_experiments")
title = layer.get_string("title", "Welcome to Statsig!")
discount = layer.get_float("discount", 0.1)
# Via get_experiment
title_exp = statsig.get_experiment(StatsigUser("my_user"), "new_user_promo_title")
price_exp = statsig.get_experiment(StatsigUser("my_user"), "new_user_promo_price")
title = title_exp.get_string("title", "Welcome to Statsig!")
discount = price_exp.get_float("discount", 0.1)
Retrieving feature gate metadata
In certain scenarios, you may need more information about a gate evaluation than a boolean value. For additional metadata about the evaluation, use the Get Feature Gate API, which returns a FeatureGate object:
gate = statsig.get_feature_gate(user, "example_gate")
print(gate.rule_id)
print(gate.value)
Parameter stores
Use Parameter Stores when you want to define a parameter without deciding whether it should be a Feature Gate, Experiment, or Dynamic Config. Parameter Stores let you change the parameter type at any point in the Statsig console without a new deployment. Parameter Stores are optional, but parameterizing your application provides future flexibility and allows non-technical Statsig users to turn parameters into experiments.
# Get a Parameter Store by name
param_store = statsig.get_parameter_store(user, "my_parameter_store")
Retrieving parameter values
Parameter Store provides methods for retrieving values of different types with fallback defaults.
# String parameters
string_value = param_store.get_string("string_param", "default_value")
# Boolean parameters
bool_value = param_store.get_bool("bool_param", False)
# Numeric parameters
float_value = param_store.get_float("float_param", 0.0)
integer_value = param_store.get_integer("integer_param", 0)
# Complex parameters
default_array = ["item1", "item2"]
array_value = param_store.get_array("array_param", default_array)
default_map = {"key": "value"}
map_value = param_store.get_map("map_param", default_map)
Evaluation options
You can disable exposure logging when retrieving a parameter store:
from statsig_python_core import ParameterStoreEvaluationOptions
options = ParameterStoreEvaluationOptions(disable_exposure_logging=True)
param_store = statsig.get_parameter_store(user, "my_parameter_store", options)
Logging an event
To track custom events, call the Log Event API. Specify the user, event name, and an optional value or metadata object:
statsig.log_event(
user=StatsigUser("user_id"), # Replace with your user object
event_name="add_to_cart",
value="SKU_12345",
metadata={
"price": "9.99",
"item_name": "diet_coke_48_pack"
}
)
Sending events to Log Explorer
You can forward logs to Logs Explorer for convenient analysis using the Forward Log Line Event API. This lets you include custom metadata and event values with each log.
user = StatsigUser(
user_id="a-user",
custom={
"service": "my-service",
"pod": "my-pod",
"namespace": "my-namespace",
"container": "my-container",
# ...include any service-specific metadata
}
)
# levels: trace, debug, info, log, warn, error
statsig.forward_log_line_event(user, "warn", "script failed to load", {
"custom_metadata": "script_name:my-script"
# ... include any event-specific metadata
})
Using shared instance
To create a single Statsig instance accessible globally throughout your codebase, use the shared instance functionality, which provides a singleton pattern:
# Create a shared instance that can be accessed globally
statsig = Statsig.new_shared("secret-key", options)
statsig.initialize().wait()
# Access the shared instance from anywhere in your code
shared_statsig = Statsig.shared()
is_feature_enabled = shared_statsig.check_gate(StatsigUser("user_id"), "feature_name")
# Check if a shared instance exists
if Statsig.has_shared_instance():
# Use the shared instance
pass
# Remove the shared instance when no longer needed
Statsig.remove_shared()
The shared instance helps when multiple parts of your codebase need Statsig without passing an instance between them.
Statsig.new_shared(sdk_key, options): Creates a new shared instance of Statsig that you can access globallyStatsig.shared(): Returns the shared instanceStatsig.has_shared_instance(): Checks if a shared instance exists (useful when the shared instance may not be ready yet)Statsig.remove_shared(): Removes the shared instance (useful when you want to switch to a new shared instance)
has_shared_instance() and remove_shared() are helpful in specific scenarios but aren't required in most use cases where the shared instance is set up near the top of your application.
Also note that only one shared instance can exist at a time. Attempting to create a second shared instance results in an error.
Manual exposures
By default, the SDK automatically logs an exposure event when you check a gate, get a config, get an experiment, or call get() on a parameter in a layer. To delay exposure logging (for example, to log only after the user actually uses the feature), use manual exposures.
All main SDK functions (check_gate, get_dynamic_config, get_experiment, get_layer) accept an optional disable_exposure_logging parameter. When set to True, the SDK doesn't automatically log an exposure. You can then log the exposure manually at a later time:
result = statsig.check_gate(aUser, 'a_gate_name', FeatureGateEvaluationOptions(disable_exposure_logging=True))
statsig.manually_log_gate_exposure(aUser, 'a_gate_name')
Statsig user
The StatsigUser object represents a user in Statsig. You must provide a userID or at least one of the customIDs to identify the user.
In addition to userID, the top-level fields on StatsigUser are: email, ip, userAgent, country, locale, and appVersion. You can also pass any key-value pairs in an object or dictionary to the custom field and create targeting based on them.
Private attributes
Private attributes are user attributes that Statsig uses for evaluation but doesn't forward to any integrations. They are useful for PII or sensitive data that you don't want to send to third-party services.
from statsig_python_core import StatsigUser
user = StatsigUser(
user_id="a-user-id",
email="user@example.com",
ip="192.168.1.1",
user_agent="Mozilla/5.0...",
country="US",
locale="en_US",
app_version="1.0.0",
custom={
# Custom fields
"plan": "premium",
"age": 25
},
custom_ids={
# Custom ID types
"stable_id": "stable-id-123"
},
private_attributes={
# Private attributes not forwarded to integrations
"email": "private@example.com"
}
)
Statsig options
You can pass an optional options parameter in addition to sdkKey during initialization to customize the Statsig client.
Was this helpful?