Skip to Content
SDKJavaScript / TypeScriptEvaluate Flags

getFlag(key)

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

const flag = canary.getFlag('new-checkout') // FlagData | undefined

Safe check with nullish coalescing

const isEnabled = canary.getFlag('new-checkout')?.enabled ?? false

The SDK does not expose an isEnabled() method. Always use the pattern getFlag(key)?.enabled ?? false.

getFlags()

Returns all flags for the environment as an array.

const flags = canary.getFlags() // FlagData[] for (const flag of flags) { console.log(flag.key, flag.enabled) }

Examples by flag type

Boolean flag

const maintenance = canary.getFlag('maintenance-mode') if (maintenance?.enabled) { return <MaintenancePage /> }

Rollout flag

const newDashboard = canary.getFlag('new-dashboard') // flag.enabled is true or false based on userId and percentage if (newDashboard?.enabled) { return <NewDashboard /> } // To see the configured percentage: if (newDashboard?.type === 'rollout') { console.log(`${newDashboard.percent}% of users will see this feature`) }

Before init()

If you call getFlag() before await init(), the flag will return undefined (the local cache is still empty).

const client = new CanaryGate('cg_key') // Do not do this: client.getFlag('feature') // undefined! // Do this instead: await client.init() client.getFlag('feature') // FlagData | undefined ✓

Flags that do not exist

If the flag does not exist in the environment, getFlag() returns undefined. Always use ?? false as a fallback to avoid breaking the application flow:

// If 'new-feature' does not exist in the environment, isEnabled will be false const isEnabled = canary.getFlag('new-feature')?.enabled ?? false
Last updated on