Skip to content

FlagDiligence docs

Node.js SDK

Evaluate flags on your server. Requires Node.js 18 or newer.

Install

npm install @weborlabs/flagdiligence-node

The SDK packages are published to the GitHub Packages registry. If your install cannot find them, add @weborlabs:registry=https://npm.pkg.github.com to your .npmrc and sign in with a token that can read packages.

Initialise

import { init } from '@weborlabs/flagdiligence-node';

const client = init({
  sdkKey: 'sdk-server_your_key',
  baseUrl: 'https://your-flagdiligence-host',
  mode: 'streaming',
});

await client.init();

Create one client when your service starts and reuse it. Always call await client.init() before you evaluate flags; before that, evaluations return your default value.

Configuration

OptionDefaultDescription
sdkKeyrequiredThe server key for one environment.
baseUrlhttp://localhost:3000The address of your FlagDiligence server. Always set this outside local development.
moderequired"streaming", "polling" or "static". If you leave it out, the SDK warns and uses "static".
pollingInterval30000Milliseconds between refreshes in polling mode. At least 10000.
timeout5000Request timeout in milliseconds.

Choosing a mode

ModeBehaviourChoose it when
streamingChanges are pushed to your service as they happen.You want changes, including kill switches, to apply quickly.
pollingYour service checks for changes on a timer.Long-lived connections are a problem, for example behind some proxies.
staticFlags are read once at start-up.Short-lived jobs or scripts that do not need live changes.

Evaluate flags

const user = {
  key: 'user-123',
  email: 'user@example.com',
  custom: { plan: 'premium', country: 'US' },
};

const on     = client.variation('new_feature', user, false);   // boolean
const theme  = client.variation('ui_theme', user, 'light');    // string
const limit  = client.variation('max_items', user, 10);        // number
const config = client.variation('feature_config', user, {});  // JSON

See why a value was returned

const result = client.variationDetail('new_feature', user, false);
// { value: true, variationId: '...', reason: 'RULE_MATCH' }

All flags for a user

const flags = client.allFlags(user);

React to changes and shut down

const stop = client.onFlagsChanged((flags) => {
  console.log('Flags updated');
});

// later
stop();
client.close(); // on shutdown

Good habits

  • Pick safe defaults: the value you want if FlagDiligence cannot be reached.
  • Never expose a server key in a browser or mobile app.
  • Use a stable user key so percentage rollouts stay consistent.
  • Use one key per environment and keep them in configuration, not in source code.

← Targeting and segmentsReact SDK →

Need help with FlagDiligence?

Open the app, or tell us what you are trying to do.