Official server-side Python SDK for SwitchPilot feature flags
Project description
switchpilot
The official server-side Python SDK for SwitchPilot feature flags.
Install
pip install switchpilot
Quick start
import os
from switchpilot import SwitchPilot
flags = SwitchPilot(api_key=os.environ["SWITCHPILOT_API_KEY"])
if flags.is_enabled("new-dashboard"):
print("New dashboard enabled")
Python example
all_flags = flags.get_all()
fresh_flags = flags.refresh()
get_all() uses a valid cache. refresh() always makes a network request.
FastAPI example
Create one reusable server-side client:
import os
from fastapi import FastAPI
from switchpilot import SwitchPilot
app = FastAPI()
flags = SwitchPilot(api_key=os.environ["SWITCHPILOT_API_KEY"])
@app.get("/dashboard-mode")
def dashboard_mode() -> dict[str, str]:
enabled = flags.is_enabled("new-dashboard")
return {"mode": "new" if enabled else "current"}
Django example
Create the client in a server-only module, then use it from a view:
import os
from django.http import JsonResponse
from switchpilot import SwitchPilot
flags = SwitchPilot(api_key=os.environ["SWITCHPILOT_API_KEY"])
def dashboard_mode(request):
enabled = flags.is_enabled("new-dashboard")
return JsonResponse({"mode": "new" if enabled else "current"})
Safe fallbacks
The default fallback is False. Missing flags, invalid responses, timeouts, and normal network or API failures do not raise during flag evaluation:
flags.is_enabled("new-dashboard") # False on failure
flags.is_enabled("new-dashboard", True) # per-call fallback
If SwitchPilot is unreachable, get_all() and refresh() return cached flags when available, or {} otherwise.
Caching
Flags are cached in memory for 30 seconds by default. Configure cache_ttl to change the TTL; each client instance has its own cache.
Custom endpoint for local development
import os
from switchpilot import SwitchPilot
flags = SwitchPilot(
api_key=os.environ["SWITCHPILOT_API_KEY"],
endpoint="https://switchpilot-one.vercel.app/api/sdk/flags",
)
enabled = flags.is_enabled("new-dashboard")
Server-side only warning
This package is a server-side SDK. Never use it in frontend or browser code, and never expose a SwitchPilot API key to users. Keep API keys in server-side environment variables.
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 switchpilot-0.1.0.tar.gz.
File metadata
- Download URL: switchpilot-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26358700226fa50eadb8b7d52a5ffdc3e22fec96635d8c914d2e2968296abe47
|
|
| MD5 |
e01cda9dc9e37d2219f470b0e430f3f2
|
|
| BLAKE2b-256 |
5a2ba9014682ff3a11aebbb772e7f2a09bb8f27b22881d6ad23db6b8b64b1697
|
File details
Details for the file switchpilot-0.1.0-py3-none-any.whl.
File metadata
- Download URL: switchpilot-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c88ee46cd07992a4063f4c23be18466fa5d8c91cdb438e2628e952f313b534e9
|
|
| MD5 |
22f7031ede3d561fe24c5b82608624f0
|
|
| BLAKE2b-256 |
d6c3afa73c290ab103af58a0645b4afab6ee9e8ecd8b4bf058518f509558b8bb
|