Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Feature Flags: Worker Binding, Evaluation, Targeting and Rollouts

How Cloudflare Flagship feature flags work in Workers: binding setup, evaluation context, API tokens, rollouts, local dev, and operations with Wrangler.

This page summarizes Cloudflare Flagship documentation on controlling feature visibility without redeploying code. Flagship groups flags into apps, propagates configuration across Cloudflare’s network, and evaluates flags locally in Workers (no outbound HTTP when you use the binding) or via OpenFeature SDKs and the evaluation API elsewhere.

Ship features safely with feature flags.

— Cloudflare, Cloudflare Flagship

Feature flag lifecycle: configure, propagate, evaluate

Section titled “Feature flag lifecycle: configure, propagate, evaluate”

Flagship feature flags move through three stages from creation to runtime resolution.

Flagship feature flags go through three stages from creation to evaluation:

Configure — Create flags and targeting rules in the Cloudflare dashboard or through the API. Propagate — Flagship automatically distributes your flag configuration across Cloudflare’s global network within seconds. Evaluate — Your Worker (or SDK) evaluates flags locally using the propagated configuration. There is no round-trip to a central server.

— Cloudflare, Concepts

Flag changes take effect globally within seconds of saving; you do not redeploy the Worker. If the dashboard is temporarily unavailable, evaluation continues using the last propagated configuration. After a change, updated values can take up to 30 seconds to appear everywhere (concepts — flag propagation).

An app is the top-level unit: one project, service, or product surface per app, with up to 5,000 flags per app (soft limits). Each flag has a unique key, variants, targeting rules, and enabled/disabled state. When a flag is disabled, it always returns the default variant, ignoring rules (concepts — flags).

Variant types: boolean, string, number, JSON

Section titled “Variant types: boolean, string, number, JSON”

Variants are the values a flag can return; one variant is the default. Flagship supports boolean, string, number, and JSON variants (concepts — variants). All variations on one flag must share the same value type.

Workers binding: how to evaluate feature flags in a Worker

Section titled “Workers binding: how to evaluate feature flags in a Worker”

Inside a Cloudflare Worker, the recommended path is a Flagship binding declared in Wrangler config. The binding exposes type-safe methods on env (for example env.FLAGS) and authenticates automatically—no application-managed API token.

Use the Workers binding inside Cloudflare Workers. The binding handles authentication automatically and avoids application-managed API tokens.

— Cloudflare, Best practices

Add the flagship binding in Wrangler configuration

Section titled “Add the flagship binding in Wrangler configuration”

Add a flagship block with binding and app_id (configuration, binding API):

{
"flagship": [
{
"binding": "FLAGS",
"app_id": "YOUR_APP_ID"
}
]
}

A Worker may bind multiple apps using multiple flagship entries, each with its own binding name (configuration — bind to multiple apps). For local development, bindings use a local flag store by default unless you set remote: true (configuration — local development).

Binding evaluation methods and safe defaults

Section titled “Binding evaluation methods and safe defaults”

Each evaluation method takes a flag key, a required default, and optional evaluation context. If evaluation fails or the flag is missing, typed methods return the default you pass (binding — methods).

Common methods include getBooleanValue, getStringValue, getNumberValue, getObjectValue, and get() for unknown types. *Details variants add reason, variant, and errorCode (evaluation reasons and error codes).

Every evaluation method requires a default value. Choose a default that keeps your application safe if the flag does not exist, cannot be evaluated, or has a type mismatch.

— Cloudflare, Best practices

For release flags, the default is usually the existing experience. Wrong typed calls (for example getBooleanValue on a string flag) return the default with errorCode TYPE_MISMATCH on *Details methods.

Evaluation context: targeting attributes and percentage rollouts

Section titled “Evaluation context: targeting attributes and percentage rollouts”

Evaluation context is the object you pass so targeting rules and rollouts can match the current user or request. It is separate from JSON flag variants returned by evaluation (concepts — evaluation context).

Pass context as the third argument on the binding:

const enabled = await env.FLAGS.getBooleanValue("new-checkout", false, {
userId: "user-42",
profile: { plan: "enterprise" },
tags: ["beta"],
});

Flagship supports nested objects, arrays, and null in context. Rules can use dot paths (up to five segments) and has / not_has on arrays (changelog — structured evaluation context). For OpenFeature SDKs, use targetingKey as the stable identifier; on the binding, use the attribute configured for rollout (for example userId) (best practices — pass context consistently).

Targeting and percentage rollouts depend on the evaluation context you pass from your application. Use stable identifiers and the same attribute names everywhere.

— Cloudflare, Best practices

Avoid sensitive data in context; include only attributes needed for rules or bucketing (concepts — evaluation context).

For HTTP evaluation without the binding, scalar context can use GET query parameters; structured context requires POST JSON (evaluation API). JSON-looking query strings remain strings and do not become nested objects.

Targeting rules: rule order, conditions, and rollout percentage

Section titled “Targeting rules: rule order, conditions, and rollout percentage”

Targeting rules serve different flag values based on attributes. Rules run top to bottom; the first matching rule wins. If none match, Flagship returns the default variant (targeting rules).

Each rule has conditions, a serve variant, and an optional rollout percentage. If context lacks an attribute referenced in a condition, that condition does not match. Place specific rules before broad catch-all rules.

Percentage rollouts limit the rule’s variant to a fraction of matching users. Bucketing uses consistent hashing on a configurable attribute (default targetingKey); the same user should get the same result when you pass a stable ID (percentage rollouts). Evaluation reason SPLIT indicates a rollout served the variant (evaluation reasons).

Situation What to check
Same user sees different values across requests Missing targetingKey or bucketing attribute
Rollout never reaches some users A higher-priority catch-all rule runs first
Default variant during an active rollout Context matched conditions but fell outside rollout % with no later rule

Progressive release pattern from best practices: start near 5%, monitor metrics, step through 25% → 50% → 100%, then make the winning variant default, remove temporary rules, delete the flag after removing code paths.

OpenFeature SDK and API token access outside Workers

Section titled “OpenFeature SDK and API token access outside Workers”

When code runs outside Workers—or you want a vendor-neutral API—use Flagship’s OpenFeature SDKs (TypeScript, Python, Go). In Workers you may still pass env.FLAGS into FlagshipServerProvider to keep binding performance (TypeScript Server SDK, best practices).

Off Workers, initialize the provider with appId, accountId, and authToken (API tokens — use the token). Inside a Worker, prefer the binding; it does not need a token (API tokens).

Token type Resource Typical use
Account-wide Entire account Trusted workflows touching every Flagship app
App-scoped Selected apps only CI or a backend for one product

Both support Evaluate, Read, and Write with names Flagship Evaluate / Flagship App Evaluate, etc. (API tokens). You must create a Flagship app before an app-scoped token. An app-scoped token is rejected if you evaluate a different app.

Programmatic management (create flags, rules, list apps) uses the Cloudflare API with appropriate Write/Read permissions (changelog — API reference).

Local development: pull flags, –local store, and remote binding

Section titled “Local development: pull flags, –local store, and remote binding”

wrangler dev, the Cloudflare Vite plugin, and cf dev evaluate bindings against a local flag store that starts empty, so calls return your code default until you add flags (configuration — local development).

  1. Copy remote definitions into the local store:

    npx wrangler flagship flags pull APP_ID

    pull also seeds rollout bucketing metadata so local buckets match remote (configuration — copy flags). Use --persist-to if your dev server stores state outside .wrangler/state (for example .cloudflare/state for Vite plugin 2.0).

  2. Create or edit flags only locally without touching production:

    npx wrangler flagship flags create APP_ID new-checkout --local
  3. To hit the live app from local code, set remote: true on the binding (configuration — use the remote app).

Most wrangler flagship flags commands accept --local but default to the remote app; you cannot combine --local and --remote (Wrangler commands — local flag store).

Wrangler flagship: CLI operations for flags and releases

Section titled “Wrangler flagship: CLI operations for flags and releases”

Wrangler flagship manages apps and flags from the terminal (Wrangler v4.107.0+). Authenticate with wrangler login or CLOUDFLARE_API_TOKEN with flagship:read / flagship:write as needed (Wrangler commands — authenticate).

Typical workflows:

  • Create a boolean flag: wrangler flagship flags create APP_ID new-checkout (default on/off, serves off).
  • Kill switch: wrangler flagship flags disable APP_ID new-checkout — disabled flags ignore rules.
  • Rollout: wrangler flagship flags rollout APP_ID new-checkout --to on --percentage 25 --by user_id.
  • A/B split: wrangler flagship flags split APP_ID checkout-flow --weight control=80 --weight treatment=20 --by user_id.
  • Verify context: wrangler flagship flags evaluate APP_ID new-checkout --targeting-key user-42 --context plan=enterprise.

rollout and split replace the entire rule set they generate; Wrangler may prompt before overwriting complex rules—use --force in scripts (Wrangler commands — rollout and split).

Best practices: performance, debugging, and flag cleanup

Section titled “Best practices: performance, debugging, and flag cleanup”

Evaluate once per request. Do not call the same flag inside a tight loop; store the result in a variable (best practices).

Use the *Details evaluation methods for observability when you need reason, variant, or errorCode (best practices — details).

Clean up stale flags by disabling first, monitoring, removing evaluation code, deploying, then deleting the flag in Flagship (best practices — clean up).

  • Re-evaluating the same flag many times per request (latency and inconsistent assumptions).
  • Inconsistent context attribute names between environments.
  • Unsafe defaults (new behavior on by default when the flag is missing).
  • Using request-level bucketing IDs when you need sticky per-user rollouts.
  • Leaving disabled or fully shipped flags in place indefinitely.

Per limits: flag keys up to 64 characters, variant value up to 10 KB, condition grouping depth 5, targeting attribute paths up to 5 segments, 25 MB total flag configuration per app.

  1. In the Cloudflare dashboard, open Compute → Flagship, confirm the app and flag key match your Worker binding app_id and code.
  2. Run wrangler flagship flags get APP_ID KEY (or flags list) to confirm remote definition, default variant, and rules (Wrangler commands).
  3. Run wrangler flagship flags evaluate APP_ID KEY --targeting-key ID --context plan=enterprise (adjust attributes) and check value, variant, and reason align with your targeting rules.
  4. Deploy the Worker (get started — deploy and test) and request it with the same context you pass in code (for example ?userId=user-42); change the flag in the dashboard and confirm behavior updates within the propagation window.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.