Official Python SDK for FlagSwift feature flags
Project description
FlagSwift Python SDK
Official Python client for FlagSwift feature flags.
Installation
pip install flagswift
Quick Start
from flagswift import FlagSwift
# Initialize the client
flags = FlagSwift(api_key='sk_live_your_api_key_here')
# Check if a feature is enabled
if flags.is_enabled('new-feature'):
# Use new feature
print("New feature is enabled!")
else:
# Use old feature
print("Using old feature")
Usage Examples
Basic Feature Flag Check
from flagswift import FlagSwift
flags = FlagSwift(
api_key='sk_live_your_api_key_here',
environment='production'
)
# Simple check
if flags.is_enabled('dark-mode'):
enable_dark_mode()
User Targeting
# Check flag for specific user
user_id = 'user-123'
if flags.is_enabled('beta-feature', user_id=user_id):
show_beta_feature()
else:
show_standard_feature()
Multiple User Identifiers
# Check flag with multiple identifiers (email, user_id, etc.)
identifiers = ['user@example.com', 'user-123']
if flags.is_enabled('premium-feature', user_id=identifiers):
show_premium_feature()
Django Integration
# settings.py
from flagswift import FlagSwift
FLAGSWIFT = FlagSwift(
api_key='sk_live_your_api_key_here',
environment='production'
)
# views.py
from django.conf import settings
def my_view(request):
user_id = str(request.user.id)
if settings.FLAGSWIFT.is_enabled('new-dashboard', user_id=user_id):
return render(request, 'new_dashboard.html')
else:
return render(request, 'old_dashboard.html')
FastAPI Integration
from fastapi import FastAPI, Depends
from flagswift import FlagSwift
app = FastAPI()
# Initialize FlagSwift
flags = FlagSwift(
api_key='sk_live_your_api_key_here',
environment='production'
)
@app.get("/feature")
async def get_feature(user_id: str):
if flags.is_enabled('new-api', user_id=user_id):
return {"version": "v2", "features": ["new", "improved"]}
else:
return {"version": "v1", "features": ["standard"]}
Flask Integration
from flask import Flask, request
from flagswift import FlagSwift
app = Flask(__name__)
flags = FlagSwift(
api_key='sk_live_your_api_key_here',
environment='production'
)
@app.route('/api/data')
def get_data():
user_id = request.headers.get('X-User-ID')
if flags.is_enabled('new-algorithm', user_id=user_id):
return use_new_algorithm()
else:
return use_old_algorithm()
Advanced Features
Configuration Object
from flagswift import FlagSwift, FlagSwiftConfig
config = FlagSwiftConfig(
api_key='sk_live_your_api_key_here',
environment='staging',
base_url='https://flagswift.com',
timeout=10,
cache_timeout=300, # 5 minutes
max_retries=3,
failsafe_fallbacks={
'critical-feature': True, # Always enabled if API fails
'experimental-feature': False # Always disabled if API fails
},
global_identifier='system-service-id' # Default user for all checks
)
flags = FlagSwift.from_config(config)
Kill Switch
Immediately disable features in production:
# Activate kill switch (disables flags immediately)
flags.activate_kill_switch(
flags=['problematic-feature'],
environments=['production']
)
# Deactivate kill switch (restores previous state)
flags.deactivate_kill_switch(
flags=['problematic-feature'],
environments=['production']
)
# Check kill switch status
if flags.is_kill_switch_enabled():
print("Kill switch is active!")
Refresh Flags
# Force refresh from server
flags.refresh()
# Refresh with specific user context
flags.refresh(user_id='user-123')
Get All Flags
# Get all flags and their states
all_flags = flags.get_all_flags()
print(all_flags)
# {'feature-1': True, 'feature-2': False, ...}
Get Flag Configuration
# Get detailed flag configuration
config = flags.get_flag_config('my-feature')
print(config)
# {
# 'enabled': True,
# 'rolloutPercentage': 50,
# 'targeting': {'enabled': True, 'users': ['user-1', 'user-2']}
# }
SDK Status
# Get SDK status
status = flags.get_status()
print(status)
# {
# 'initialized': True,
# 'flag_count': 10,
# 'environment': 'production',
# 'kill_switch_active': False,
# 'cache_valid': True
# }
ML Model A/B Testing Example
from flagswift import FlagSwift
flags = FlagSwift(api_key='sk_live_your_key')
def predict(data, user_id):
if flags.is_enabled('new-ml-model', user_id=user_id):
# Use new model
return new_model.predict(data)
else:
# Use stable model
return stable_model.predict(data)
Error Handling
from flagswift import FlagSwift, FlagSwiftError, AuthenticationError, NetworkError
try:
flags = FlagSwift(api_key='sk_live_your_key')
flags.initialize()
except AuthenticationError:
print("Invalid API key")
except NetworkError:
print("Network error - using fallback values")
except FlagSwiftError as e:
print(f"FlagSwift error: {e}")
Best Practices
1. Initialize Once
# ✅ Good: Initialize once at application startup
flags = FlagSwift(api_key='sk_live_your_key')
# ❌ Bad: Don't create new instances for each check
def check_feature():
flags = FlagSwift(api_key='sk_live_your_key') # Don't do this!
return flags.is_enabled('feature')
2. Use Failsafe Fallbacks
# Always provide fallbacks for critical features
flags = FlagSwift(
api_key='sk_live_your_key',
failsafe_fallbacks={
'payment-processing': True, # Critical: always enabled
'experimental-ui': False # Experimental: always disabled
}
)
3. Cache Strategy
# For high-traffic APIs, use longer cache
flags = FlagSwift(
api_key='sk_live_your_key',
cache_timeout=600 # 10 minutes
)
# For real-time updates, use shorter cache or manual refresh
flags = FlagSwift(
api_key='sk_live_your_key',
cache_timeout=30 # 30 seconds
)
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
api_key |
str | Required | Your FlagSwift server API key |
environment |
str | 'production' |
Environment name |
base_url |
str | 'https://flagswift.com' |
FlagSwift API URL |
timeout |
int | 5 |
Request timeout (seconds) |
cache_timeout |
int | 300 |
Cache validity (seconds) |
max_retries |
int | 3 |
Maximum retry attempts |
failsafe_fallbacks |
dict | {} |
Default values when API fails |
kill_switch_fallbacks |
dict | {} |
Values during kill switch |
global_identifier |
str/list | None |
Default user identifier |
API Reference
FlagSwift Class
Methods
is_enabled(flag_name, user_id=None)- Check if flag is enabledget_all_flags()- Get all flagsget_flag_config(flag_name)- Get flag configurationrefresh(user_id=None)- Force refresh from serveractivate_kill_switch(flags, environments=None)- Activate kill switchdeactivate_kill_switch(flags, environments=None)- Deactivate kill switchis_kill_switch_enabled()- Check kill switch statusget_status()- Get SDK status
Testing with Postman
You can test your FlagSwift integration using the same backend server from your Node.js example. The Python SDK makes the same API calls to /api/flags/server.
Support
- 📧 Email: support@flagswift.com
- 📚 Docs: https://docs.flagswift.com
- 🐛 Issues: https://github.com/flagswift/flagswift-python/issues
License
MIT License - see LICENSE file for details
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
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 flagswift-1.0.0.tar.gz.
File metadata
- Download URL: flagswift-1.0.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4db38d029b0ccec24180fc660f46bd02f20494b0b05c676d21919096edd6b293
|
|
| MD5 |
c5d43b393a1c5e569c1c90857eae2dba
|
|
| BLAKE2b-256 |
671a14fc61439e111462c30d3b0dd4ff735ffa7e5b0dcbdcadada60e61c00da2
|
File details
Details for the file flagswift-1.0.0-py3-none-any.whl.
File metadata
- Download URL: flagswift-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea6a844c1e91956f456ff213ad92bf4a913c242c5603b86d73fac09407a30266
|
|
| MD5 |
c72ec419e461494d8bbadc76e1a2a21b
|
|
| BLAKE2b-256 |
21f7044ffa7ea131f40eb4db50801b3ea959f42087449a15b738d10879e59ab9
|