Resources/Developer & SDK/Feature flags in code

Feature flags in code

Read feature-flag values in your application with Spectry.getFlag().

Feature flags let you turn features on/off or run multivariate rollouts without redeploying. You configure them in the dashboard (see Feature flags) and read them in code.

Reading a flag

// Boolean flag
if (Spectry.getFlag('new_checkout')) {
  renderNewCheckout();
}

// Multivariate flag — returns the assigned variant's value
const layout = Spectry.getFlag('pricing_layout'); // e.g. 'compact'

getFlag() is synchronous and returns null for a key it doesn't know about — including every key before the values have loaded (see below).

Flags are evaluated server-side, after init

Assignment happens on the server, not in the browser, so the SDK fetches this visitor's evaluated values once it has started — which is after the snippet starts loading. Code that runs before then will read null and take the "off" path. If you need to gate something at first paint, treat null as the default state explicitly and subscribe for the real value:

Spectry.onFlagChange('new_checkout', (value) => {
  if (value) renderNewCheckout();
});

The callback fires whenever the value changes, including the first time it's populated after init.

How assignment works

  • Rollout percentage gates exposure — only that share of visitors receives the flag. Visitors outside the rollout get the flag's default value, not null.
  • Multivariate flags distribute visitors across variants by weight.
  • Assignment is deterministic per visitor, so a given visitor consistently sees the same value.
Use the exact flag key from the dashboard. The key is immutable once a flag is created — which also means a typo in getFlag() fails silently as null rather than throwing.

Reading flags on the server instead

Everything above describes the browser. If the decision has to be made before the page is sent — or the flag guards something that never runs in a browser, like a background job — read it on your server with the Node.js SDK. It caches your flag definitions in memory, so there is no null window and no waiting for a fetch, and it buckets each user identically to the browser.


Put this to work on your own site.

Heatmaps, session replays, funnels and experiments in one platform. Set it up in minutes, no credit card needed, and 5,000 sessions a month are free forever.