Using Flagonic from Python
Flagonic has no Python SDK of its own — by design. You use the official OpenFeature Python SDK with its official OFREP provider. Nothing in your requirements is Flagonic-specific, so swapping flag vendors later is a config change, not a refactor.
Prerequisites
- A running Flagonic server with a flag defined — see the overview & quickstart.
- An SDK key (
fgn_sdk_…) for your project + environment.
Install
pip install openfeature-sdk openfeature-provider-ofrep
Initialize once at startup
import os
from openfeature import api
from openfeature.contrib.provider.ofrep import OFREPProvider
from openfeature.evaluation_context import EvaluationContext
api.set_provider(OFREPProvider(
base_url=os.environ["FLAGONIC_URL"], # e.g. https://flags.example.com
headers_factory=lambda: {"Authorization": f"Bearer {os.environ['FLAGONIC_SDK_KEY']}"},
))
client = api.get_client("checkout-service")
Evaluate flags
The evaluation context carries the targeting key (a stable subject identifier — it keeps percentage rollouts sticky) plus any attributes your rules match on:
ctx = EvaluationContext(
targeting_key=user.id,
attributes={"plan": user.plan, "email": user.email},
)
if client.get_boolean_value("new-checkout", False, ctx):
new_checkout()
else:
legacy_checkout()
theme = client.get_string_value("banner-theme", "default", ctx)
limit = client.get_integer_value("rate-limit", 100, ctx)
ratio = client.get_float_value("sampling-ratio", 0.1, ctx)
config = client.get_object_value("checkout-config", {}, ctx)
Each call takes a default that is returned if the flag is missing or Flagonic is unreachable — pick the value you'd want during an outage.
Inspect why a value was served
details = client.get_boolean_details("new-checkout", False, ctx)
print(details.value, details.variant, details.reason)
# True on Reason.TARGETING_MATCH — others: SPLIT, DEFAULT, STATIC, DISABLED, ERROR
Switching vendors
The provider speaks standard OFREP: point base_url at any other
OFREP-compatible backend — one line, no import changes.
Provider signatures occasionally shift between releases — if something doesn't import,
check the python-sdk-contrib
README for your installed version.