Use SwitchPilot with Python
Control product features from Python backends, API servers, jobs, and server-side scripts.
Python SDK
switchpilot
Python 3.10+
Requirements
The official Python SDK requires Python 3.10 or newer and is designed for server-side usage only.
It works with FastAPI, Django, Flask, scripts, jobs, backend services, and API servers.
1. Install the SDK
pip install switchpilot2. Configure the API key
SWITCHPILOT_API_KEY="sp_live_xxxxxxxxx"The key is shown once. If it is lost, regenerate it in Project Settings and replace SWITCHPILOT_API_KEY before restarting or redeploying the service. The previous key stops working immediately.
3. Initialize the client and check a flag
import os
from switchpilot import SwitchPilot
flags = SwitchPilot(
api_key=os.environ["SWITCHPILOT_API_KEY"]
)
enabled = flags.is_enabled("new-dashboard")4. Check a flag
if flags.is_enabled("new-dashboard"):
show_new_dashboard()
else:
show_current_dashboard()FastAPI example
Import the reusable client from your server module, then check it inside the route.
from fastapi import FastAPI
app = FastAPI()
@app.get("/dashboard-mode")
def dashboard_mode() -> dict[str, str]:
enabled = flags.is_enabled("new-dashboard")
return {"mode": "new" if enabled else "current"}Safe fallback behavior
The default is False. Missing flags, invalid responses, timeouts and normal network failures return the fallback instead of raising during flag evaluation.
flags.is_enabled("new-dashboard", True)Cache and refresh
all_flags = flags.get_all()
fresh_flags = flags.refresh()
status = flags.get_status()The in-memory cache lasts 60 seconds by default. While fresh, evaluations are local. A dashboard change can take about one minute to appear unless you call refresh() manually.
ETag and Last Known Good
flags = SwitchPilot(
api_key=os.environ["SWITCHPILOT_API_KEY"],
cache_ttl=60,
stale_if_error=300,
fallback=False,
)The SDK automatically uses ETags; a 304 renews freshness without downloading or parsing the flags again. If synchronization fails, the last valid configuration remains usable for five minutes beyond its normal TTL. Only then does the fallback apply.
Concurrent threads share a refresh. A 429 response is not retried in a loop: the SDK respects Retry-After and exposes the deadline throughget_status(), which performs no I/O.