Install
npm i @flaggish/sdk@betaCopy the client key from Settings › API keys in the dashboard and set it as an environment variable:
FLAGGISH_CLIENT_KEY=your-client-keyCreate a client
import { createFlaggish } from '@flaggish/sdk';
export const flaggish = createFlaggish({ clientKey: process.env.FLAGGISH_CLIENT_KEY });createFlaggish reads FLAGGISH_CLIENT_KEY by itself, so createFlaggish() with no options works too. It takes every option in SDK options. Without env or FLAGGISH_ENV, it reads Production flags when NODE_ENV is production, and Development flags otherwise.
Check flags
import { flaggish } from './flags';
if (await flaggish.isEnabled('new-checkout', { userId: 'user_123' })) {
// ...
}
const all = await flaggish.getAll({ userId: 'user_123' });
// { 'new-checkout': true, 'new-banner': false, ... }isEnabled(key, ctx?, fallback?)resolves one flag totrueorfalse.getAll(ctx?)resolves every flag, with yourdefaultsmerged in.getConfig()returns the raw public config, ornullif none has loaded. It never contains the people in a list.getGroupIds(ctx?)returns the IDs of the list groups the person is in. Use it to passgroupIdsto a browser provider.
None of them ever reject. See If Flaggish is down.
The context
ctx is { userId?, anonymousId?, email? }. The SDK uses userId if it’s set, otherwise anonymousId, to decide a rollout. All of them can be null. email is only used for groups.
Pass an ID to be counted. In Node there’s no cookie, and the SDK doesn’t invent IDs on the server. A check without an ID is off in a partial rollout (unless it’s at 100%), and reach doesn’t count it.
Typed flag names
Pass your flag names as a type, and TypeScript checks every name you pass to isEnabled:
import { createFlaggish } from '@flaggish/sdk';
type Flag = 'new-checkout' | 'new-banner';
export const flaggish = createFlaggish<Flag>();Groups
Groups give a flag to specific people first. Set your secret key in the server’s environment, and pass email next to userId:
FLAGGISH_SECRET_KEY=your-secret-keyimport { flaggish } from './flags';
const on = await flaggish.isEnabled('dialog', { userId: 'user_123', email: 'bob@acme.com' });createFlaggish reads FLAGGISH_SECRET_KEY by itself, or you can pass secretKey. With it, the SDK downloads the people in the lists your flags target, caches them for 30 seconds like your flags, and checks them in memory. If a refresh fails, it keeps the last good copy.
Without a secret key, list groups count as “not in the group”, so people fall to the percentage, and the SDK logs one console warning per process. Company-domain groups work either way, as long as you pass email. A list matches userId exactly or email in any case; a domain matches the email’s domain exactly, so acme.com doesn’t match mail.acme.com.
Caching
The first check fetches your flags, and they’re cached in memory for 30 seconds. Checks in that time are lookups, with no network call. After 30 seconds, the next check fetches a fresh copy, so a change in the dashboard shows up within about 30 seconds.
The cache is shared by every client in the process that uses the same client key, API URL and environment. Change the time with cacheTtlMs.
Flush reach on shutdown
On Full Mast projects with reach on, the SDK sends reach in batches: when 200 events are queued, or 60 seconds after the first one. The timer never keeps the process alive. To send what’s queued before the process exits, await flushReach():
import { flushReach } from '@flaggish/sdk';
process.on('SIGTERM', async () => {
await flushReach();
process.exit(0);
});flushReach() never rejects. On serverless platforms the timer may never fire, so call await flushReach() before your function returns if you want those counts. Without it, you lose some counts; flag results are never affected.
Counting reach needs Node 19 or later, where crypto is available globally. On older versions, flag checks work as normal and nothing is counted.
To turn reach off for this client, pass reach: false to createFlaggish, or set FLAGGISH_REACH=false.
The examples use top-level await, which needs an ES module. In CommonJS, put the checks inside an async function.