Node.js SDK
Configure the released Node.js SDK, evaluate typed flags locally, and handle refreshes safely.
Configure the released Node.js SDK, evaluate typed flags locally, and handle refreshes safely.
The Node.js SDK is built for trusted server-side applications. It loads one environment snapshot, evaluates flags locally, and refreshes configuration in the background.
A server runtime key reads the environment's full flag snapshot. Keep it on the server and load it from your secret-management system. Never include it in browser or mobile bundles.
Install the ESM-only SDK in a Node.js 22 or newer application:
npm install @featuregate/node
Create one client per runtime key and reuse it for the lifetime of the process. initialize() loads
the first remote snapshot and starts polling.
import { FeatureGate } from "@featuregate/node";
export const featuregate = new FeatureGate({
runtimeApiKey: process.env.FEATUREGATE_RUNTIME_KEY!,
});
await featuregate.initialize();FeatureGate requires a runtimeApiKey, an in-memory flags snapshot, or both.
| Option | Default | Purpose |
|---|---|---|
runtimeApiKey | — | Secret key used to load one environment snapshot. |
flags | {} | In-memory flags available immediately for bootstrap or local evaluation. |
apiBaseUrl | https://api.featuregate.dev | API base URL. |
fetch | globalThis.fetch | Custom fetch implementation. |
pollIntervalMs | 30000 | Refresh interval; set to 0 to disable polling. |
requestTimeoutMs | 2000 | Maximum duration of each snapshot request. |
onError | — | Receives background polling failures. |
Errors from initialize() and refresh() reject their promises. onError is used only for
automatic refresh failures.
Every evaluation takes an explicit default. The SDK uses it when the flag is missing or its stored value does not match the selected getter.
const enabled = featuregate.getBooleanValue("checkout", false, context);
const heading = featuregate.getStringValue("checkout-heading", "Checkout", context);
const pageSize = featuregate.getNumberValue("page-size", 25, context);
const theme = featuregate.getObjectValue("theme", { mode: "system" }, context);
Each value getter has a corresponding details getter:
| Value | Details |
|---|---|
getBooleanValue() | getBooleanDetails() |
getStringValue() | getStringDetails() |
getNumberValue() | getNumberDetails() |
getObjectValue() | getObjectDetails() |
Details include the final value, the evaluation reason, and whether the SDK usedDefault.
Use targetingKey for a stable subject identifier and attributes for application-specific data.
Nested attributes are addressed by dot-separated paths in targeting rules.
const context = {
targetingKey: "customer-123",
attributes: {
account: {
id: "account-456",
plan: "pro",
},
country: "AU",
},
};
const enabled = featuregate.getBooleanValue("checkout", false, context);Stable identifiers keep percentage-rollout assignment deterministic across evaluations.
The client polls every 30 seconds by default and uses ETag revalidation. Call refresh() when you
need to request configuration immediately:
const result = await featuregate.refresh();
if (result.status === "updated") {
console.log(`Loaded snapshot ${result.version}`);
}
Failed refreshes keep the last successful snapshot available. Call close() during application
shutdown to stop automatic polling; the last snapshot remains available for evaluation.
| Error | Meaning |
|---|---|
FeatureGateAuthenticationError | The API rejected the runtime key with 401 or 403. |
FeatureGateConfigurationError | SDK options or the returned snapshot were invalid. |
FeatureGateRequestError | A request failed, timed out, was rate limited, or returned 5xx. |
For tests and local development, provide an in-memory flags snapshot without a runtime key. Local
instances are ready immediately and do not need initialize().
const featuregate = new FeatureGate({
flags: {
checkout: { defaultValue: true },
},
});
featuregate.getBooleanValue("checkout", false); // true
Evaluate sensitive flags only in trusted server code, but continue enforcing permissions, entitlements, and other security-sensitive decisions independently in your application.