Skip to content

Python SDK

The RocketFlag Python SDK is a stdlib-only client for evaluating flags from Python 3.9+ applications.

Terminal window
pip install rocketflag
from rocketflag import create_client
rocketflag = create_client()
flag = rocketflag.get_flag("ABC123def456")
if flag.enabled:
print(f'Feature "{flag.name}" is enabled!')

You can also construct Client directly:

from rocketflag import Client
client = Client()
flag = client.get_flag("ABC123def456")

Pass a cohort identifier to target specific users:

flag = rocketflag.get_flag("ABC123def456", {"cohort": "user@example.com"})

When using Group Flags, specify the environment (env must be alphanumeric):

flag = rocketflag.get_flag("ABC123def456", {"env": "production"})
rocketflag = create_client("v1", "https://your-custom-proxy.com")

Opt-in in-memory cache keyed by flag ID and user context:

# Cache flag responses for 5 minutes.
rocketflag = create_client(ttl_seconds=300)
flag = rocketflag.get_flag("ABC123def456", {"cohort": "beta"})
# Force a fresh fetch.
flag = rocketflag.get_flag("ABC123def456", ttl_seconds=0)

Without a client default or per-call TTL, every call hits the API.

from rocketflag import APIError, InvalidResponseError, NetworkError, create_client
rocketflag = create_client()
try:
flag = rocketflag.get_flag("ABC123def456")
except APIError as err:
print(f"API Error: {err.status} {err.status_text}")
except InvalidResponseError as err:
print(f"Invalid Response Error: {err}")
except NetworkError as err:
print(f"Network Error: {err}")
  • APIError — non-OK HTTP status (includes status / status_text). A 404 means the flag ID is unknown.
  • InvalidResponseError — body is not JSON or not a flag object.
  • NetworkError — connection failure.