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
| Option | Default | Description |
|---|---|---|
| sdkKey | required | The server key for one environment. |
| baseUrl | http://localhost:3000 | The address of your FlagDiligence server. Always set this outside local development. |
| mode | required | "streaming", "polling" or "static". If you leave it out, the SDK warns and uses "static". |
| pollingInterval | 30000 | Milliseconds between refreshes in polling mode. At least 10000. |
| timeout | 5000 | Request timeout in milliseconds. |
Choosing a mode
| Mode | Behaviour | Choose it when |
|---|---|---|
| streaming | Changes are pushed to your service as they happen. | You want changes, including kill switches, to apply quickly. |
| polling | Your service checks for changes on a timer. | Long-lived connections are a problem, for example behind some proxies. |
| static | Flags 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
keyso percentage rollouts stay consistent. - Use one key per environment and keep them in configuration, not in source code.
Need help with FlagDiligence?
Open the app, or tell us what you are trying to do.
