Amba
SDKsFeatures

Feature flags

Server-controlled boolean and variant flags with attached JSON payload, plus hooks for React.

Feature flags let you ship code dark and turn it on without a deploy. Today each flag has a global on/off and a global default variant — every user who fetches the flag set sees the same answer. Per-user targeting (segment-scoped flags, sticky A/B variants) is on the roadmap; the SDK contract is already shaped for it so client code won't need to change when it lands.

Two flag types:

  • Boolean flags — global on / off. Read with flags.isEnabled(name) (no request), useFlagEnabled(name) in React, or flags.fetch().
  • Variant flags — return a string (e.g. 'control' / 'short' / 'long'). Today the variant is the same for every user, so use these for kill-switched rollouts and config-as-flag, not as A/B traffic splitters yet.

Each flag can also carry a payload JSON value — useful for config-driven feature gates ({ "max_items": 25 }).

Coming next: per-user targeting

Segment-scoped flags and sticky A/B variants are tracked work. Until they ship, treat flags as global kill-switches plus attached config — not as A/B traffic splitters. If you ship a multi-variant flag today, every user gets the same arm.

Quick start

import { Amba } from '@layers/amba-web';
 
const flags = await Amba.flags.fetch();
const newUi = flags.find((f) => f.name === 'new_ui')?.enabled ?? false;
const copy = flags.find((f) => f.name === 'signup_copy')?.variant ?? 'control';

Read flags on the first frame

flags.isEnabled(name, default) and flags.getCached(name) answer at once, with no request, from what the SDK holds: the last fetch, else the flags the device cached on an earlier launch. isEnabled then reads a boolean entry under the flag's name in the configDefaults you pass at configure, then the default you pass (false when you pass none).

await Amba.configure({
  projectId: 'proj_...',
  clientKey: 'amba_ck_...',
  configDefaults: { new_ui: false },
});
 
const newUi = Amba.flags.isEnabled('new_ui');
const layout = Amba.flags.getCached('new_ui_layout')?.variant ?? 'control';

The first cached flag read for each user starts one flags.fetch() in the background (once after configure, and again after a sign-in or sign-out); its result replaces the cached flags. Configure itself sends no request. A flag switched off on the server is left out of the next fetch, so it leaves the cache and isEnabled returns your default from then on. A single-flag flags.get(name) updates the cache the same way. The cache is per project and per signed-in user, and sign-out deletes the signed-out user's flags; see startup values for where each SDK stores it. Node, which runs server-side, keeps no cache.

Operations

fetch()

Returns the full flag set for this project. Each entry has the shape:

{
  name: 'new_onboarding',     // flag name
  enabled: true,               // boolean on/off (global)
  variant: 'short',            // optional variant tag (global)
  payload: { max_items: 25 },  // optional, attached JSON config
}

Every fetch() call sends a request to the server and refreshes the device cache that isEnabled and getCached read.

Today every signed-in user receives the same project-wide set of enabled flags. When per-user targeting ships, the response shape stays the same and the values start varying by user.

React hooks

useFlag(name) returns the flag's assignment ({ name, enabled, variant, payload }), or null when the flag is unknown or disabled. useVariant(name) returns the variant string (or null if unset). useFlagEnabled(name, default) returns a boolean and also reads a boolean configDefaults entry.

import { useFlag, useFlagEnabled, useVariant } from '@layers/amba-react';
 
function NewFeature() {
  const enabled = useFlagEnabled('new_feature');
  const layout = useVariant('new_feature_layout'); // 'horizontal' | 'vertical' | null
  if (!enabled) return null;
 
  return layout === 'horizontal' ? <Horizontal /> : <Vertical />;
}

While the hooks' fetch is in flight they return what the SDK holds from the device cache (the flags from the last launch), so the first render already has the last known answer; with nothing cached, useFlag returns null and useFlagEnabled returns its default.

Patterns

Gate render at app start

For flags that gate large slabs of the UI, read them with isEnabled at app start: it answers from the device cache with no request, and its first call refreshes the cache in the background.

export function getFlag(name: string): boolean {
  return Amba.flags.isEnabled(name);
}

Config-driven gates

For A/B tests with parameterized behavior, attach a payload to the variant in the console and read it on the client:

async function loadPosts() {
  const flags = await Amba.flags.fetch();
  const flag = flags.find((f) => f.name === 'pagination_size');
  const limit = (flag?.payload as { limit?: number })?.limit ?? 20;
  const { data } = await Amba.collections.find('posts', { limit });
  return data;
}

Server-side parity

Server functions read the same flag set from the client endpoint GET /v1/client/flags, authenticated with your server key as X-Api-Key plus X-On-Behalf-Of: <user id>. Store the server key as a function secret under your own name (amba secrets set APP_SERVER_KEY <server key> --function <fn>); names starting with AMBA_ are reserved for values the platform injects. Useful for gating server logic on the same answer as the UI:

// Amba functions are ES modules with the `{ async fetch(req, env, ctx) }`
// shape; bindings come in via `env`. The X-Amba-User-Id header carries
// the calling user's id, signed by the edge.
export default {
  async fetch(req, env, ctx) {
    // Trust X-Amba-User-Id only after the edge signature checks out:
    // verifyEdgeHeaders is the verifier from Signed-header identity in
    // the functions runtime docs (/functions/runtime#signed-header-identity).
    if (!(await verifyEdgeHeaders(req, env.EDGE_HEADER_SIGNING_SECRET))) {
      return Response.json({ error: 'unsigned_request' }, { status: 401 });
    }
    const userId = req.headers.get('X-Amba-User-Id');
    if (!userId) return Response.json({ error: 'sign_in_required' }, { status: 401 });
 
    const res = await fetch(`${env.AMBA_API_URL}/v1/client/flags`, {
      headers: { 'X-Api-Key': env.APP_SERVER_KEY, 'X-On-Behalf-Of': userId },
    });
    if (!res.ok) return Response.json({ error: 'flags_unavailable' }, { status: 503 });
 
    const { data: flags } = await res.json();
    const newPipeline = flags.find((f) => f.name === 'new_processing_pipeline')?.enabled ?? false;
    return newPipeline ? newImpl() : oldImpl();
  },
};

Limits

  • Flag count: up to 100 flags per project visible to a single fetch.
  • Variant count: up to 8 variants per multi-variant flag (control + 7 treatments is a common ceiling).
  • Payload size: up to 8 KB JSON per flag.
  • Caching: each fetch() is a request; the device SDKs keep the last flag set per project and user for isEnabled / getCached (Node keeps none).

Reference

On this page