Skip to Content
SDKPythonEvaluate Flags

get_flag(key, context)

Returns the data for a specific flag, or None if the flag does not exist.

from canarygate import FlagEvaluationContext flag = client.get_flag("new-checkout", FlagEvaluationContext(user_id="user-42")) # FlagData or None

The user_id in the context is used to evaluate rollout flags consistently: the same user always gets the same result. When the context or user_id is empty, a per-instance anonymous ID is used.

Safe check

if flag and flag.enabled: # Flag is enabled for this user pass

get_flags(context)

Returns all flags for the environment as a list.

flags = client.get_flags(FlagEvaluationContext(user_id="user-42")) for flag in flags: print(flag.key, flag.enabled)

Examples by flag type

Boolean flag

maintenance = client.get_flag("maintenance-mode", context) if maintenance and maintenance.enabled: return maintenance_page()

Rollout flag

new_dashboard = client.get_flag("new-dashboard", context) # flag.enabled is True or False based on user_id and percentage if new_dashboard and new_dashboard.enabled: return new_dashboard_page() # To see the configured percentage: if new_dashboard and new_dashboard.type == "rollout": print(f"{new_dashboard.percent}% of users will see this feature")

Before init()

If you call get_flag() before init(), the flag will return None (the local cache is still empty).

client = CanaryGate("cg_key") # Do not do this: client.get_flag("feature") # None # Do this instead: client.init() client.get_flag("feature") # FlagData or None

Flags that do not exist

If the flag does not exist in the environment, get_flag() returns None. Always guard with a truthiness check to avoid breaking the application flow:

# If 'new-feature' does not exist in the environment, enabled will be False flag = client.get_flag("new-feature") enabled = flag.enabled if flag else False
Last updated on