Skip to main content

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_ttl seconds 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flagbase_sdk-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flagbase_sdk-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

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

Hashes for flagbase_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8f3cf1bf25704ade2479c763c9a811a6ce7685f9382e8ea2065c6269670b427a
MD5 5748b67a9730c777dcb5427ceddcf66b
BLAKE2b-256 53052cbb672d8a5ad4b796881dd6dea19aac14509fc991752df59a5e183750a0

See more details on using hashes here.

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

Hashes for flagbase_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76b3b4e210c7590161433d5fac2ab4a7fc99f79cce19c426551b7bf47bc87394
MD5 35691062040f73a207e777d519ae0aa5
BLAKE2b-256 e79f56ca7c962ecd9cf13b014ea67aa88026557326b459f0de687709093691a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page