Overview
The@flagsync/node-sdk integrates into Node.js applications for server-side feature management and event tracking—ideal for backend services and APIs.
npm
Latest version available on npm
GitHub
Explore the source code on GitHub
Installation
Install the SDK with your preferred package manager:Quickstart
A basic example of using the SDK in a Node.js application (e.g., an Express server):Initialization
Get Your SDK Key
Find your server-side SDK key in your workspace settings. Keep this key private and secure.Initialize the SDK
Initialize the SDK with your SDK key:Wait for Readiness
Use promises or events to ensure the SDK is ready before evaluating flags. Always wait for SDK initialization before evaluating any flags. Initialization usually completes within 30–50ms, depending on flag count and ruleset complexity.1
Promises
2
Events
SDK Not Ready
If the SDK isn’t ready, it returns thedefaultValue or control:
SDK Ready
Once ready,flag() returns the server-evaluated value:
User Context Identification
Define the user context with a helper function that returns anFsUserContext object. Pass this object to flag() and track() functions.
User contexts enable personalized flag evaluations via Individual Targeting and consistent experiences during Percentage Rollouts.
1
Create a Helper Function
Create a helper function to construct the user context from request cookies, headers, or your application’s auth system.Adjust the file below to meet your needs.
lib/flagsync/user-context.ts
The
key is set using a persistent userId or visitorId from cookies, falling back to a generated ID with nanoid(). For proper MAU tracking and consistent flag evaluations, ensure this key is unique and persistent across requests—see User Context Best Practices.2
Set Cookies in Middleware (Optional)
Use Express middleware to set user identification cookies, simplifying context retrieval in the helper function.
lib/flagsync/identify.ts
3
Use the Helper in Routes
In your Express routes, use the helper to build the
FsUserContext for flag evaluations or event tracking.app.ts
Usage
Evaluate Flags
Evaluates a feature flag for a given user context. Applies targeting rules, rollouts, and defaults values.app.ts
FlagSync CLI
When using the CLI, flag values are automatically inferred from the generated types:Track Events
Submit user actions with thetrack() function:
SDK Event Listeners
The SDK emits these events for SDK lifecycle management:SDK_UPDATE: Emitted when flags are updatedSDK_READY: Emitted when the SDK is readyERROR: Emitted when an error occurs during initialization
SDK_UPDATE does not fire if syncing is disabled.Error Handling
Configuration
Configure the SDK with theFsConfig interface:
Custom Attributes
- Define user attributes for targeting in Flags: User Segments.
- See User Context Identification for the recommended approach to building the context.
Flag Syncing
Configure flag update strategies with thesync object: ws, sse, poll, or off.
1
WebSocket (Default)
Stream updates over a WebSocket connection—flag updates are reevaluated on the server and pushed to the client in real time:
2
SSE
Stream updates via server-sent events (SSE):
3
Polling
Poll the server on an interval:
4
Off
Disable syncing:
Best Practices
- Wait for SDK readiness before evaluating flags.
- Select a sync strategy (
ws/sse/poll/off) based on your application’s needs. - Follow User Context Identification for simplified context management.
- Add user attributes for targeted feature rollouts.
Environment Variables
Set the following environment variable:FLAGSYNC_SDK_KEY: Your server-side FlagSync SDK key (required)