Python SDK for FlagBase — a self-hostable feature flag and A/B testing platform
Project description
flagbase-python-sdk
Official Python SDK for FlagBase — a self-hostable feature flag and A/B testing platform.
Installation
pip install flagbase
Quick Start
from flagbase import FlagClient
client = FlagClient(
api_key="proj_sk_...", # Generate in your FlagBase dashboard
host="http://localhost:8000", # Your FlagBase server URL
)
# Basic flag check
if client.is_enabled("new_checkout_ui", user_id="user_123"):
show_new_checkout()
else:
show_old_checkout()
client.close()
Usage
Initialisation
client = FlagClient(
api_key="proj_sk_...", # Required
host="http://...", # Required — your FlagBase server URL
cache_ttl=30, # Optional — local cache TTL in seconds (default 30)
timeout=0.5, # Optional — HTTP timeout in seconds (default 0.5)
default_value=False, # Optional — returned if server unreachable (default False)
)
is_enabled(flag_name, user_id, context=None, default_value=None)
Returns True or False. Never raises.
enabled = client.is_enabled("dark_mode", user_id="user_123")
# With targeting context
enabled = client.is_enabled(
"premium_feature",
user_id="user_123",
context={
"email": "user@example.com",
"country": "IN",
"custom_attributes": {"plan": "premium"}
}
)
evaluate(flag_name, user_id, context=None)
Returns an EvaluationResult with enabled, variant, reason, and flag_name.
result = client.evaluate("new_checkout_ui", user_id="user_123")
print(result.enabled) # True
print(result.reason) # "rollout_included"
print(result.variant) # None (variants available in v4 A/B testing)
track(event_name, user_id, flag_name, variant=None)
Track a conversion event. Non-blocking — returns immediately.
client.track(
event_name="purchase_completed",
user_id="user_123",
flag_name="new_checkout_ui",
)
Context manager
with FlagClient(api_key="proj_sk_...", host="http://...") as client:
enabled = client.is_enabled("my_flag", user_id="user_123")
# client.close() called automatically
Error Handling
The SDK never raises exceptions in normal use. All errors are caught internally and default_value is returned instead:
| Error | Behaviour |
|---|---|
| Network timeout | Returns default_value, logs warning |
| HTTP 401 (bad API key) | Returns default_value, logs error |
| Server error (5xx) | Returns default_value, logs warning |
| Any unexpected error | Returns default_value, logs error |
Caching
Flag results are cached locally for 30 seconds (configurable via cache_ttl). This means:
- Repeated calls to
is_enabled()for the same flag + user don't hit the network - Flag changes take up to
cache_ttlseconds to propagate to the SDK
Requirements
- Python 3.10+
- httpx >= 0.27.0
Running the Backend
This SDK requires a running FlagBase server. See the FlagBase backend repo for setup instructions.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flagbase_sdk-0.1.0.tar.gz.
File metadata
- Download URL: flagbase_sdk-0.1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f3cf1bf25704ade2479c763c9a811a6ce7685f9382e8ea2065c6269670b427a
|
|
| MD5 |
5748b67a9730c777dcb5427ceddcf66b
|
|
| BLAKE2b-256 |
53052cbb672d8a5ad4b796881dd6dea19aac14509fc991752df59a5e183750a0
|
File details
Details for the file flagbase_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flagbase_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76b3b4e210c7590161433d5fac2ab4a7fc99f79cce19c426551b7bf47bc87394
|
|
| MD5 |
35691062040f73a207e777d519ae0aa5
|
|
| BLAKE2b-256 |
e79f56ca7c962ecd9cf13b014ea67aa88026557326b459f0de687709093691a8
|