Node.js SDK
Integrate FlagSync feature flags into Node.js applications.
Overview
The @flagsync/node-sdk
integrates into Node.js applications for server-side feature management and event tracking—ideal for backend services and APIs.
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):
See User Context Identification for details on the getFlagSyncUserContext
object.
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 to ensure the SDK is ready before evaluating flags:
Promises
Events
User Context Identification
Define the user context with a helper function that returns an FsUserContext
object. Pass this object to flag()
and track()
functions.
User contexts enable personalized flag evaluations via Individual Targeting and consistent experiences during Percentage Rollouts.
Ensure the key
in FsUserContext
is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.
Recommended Approach
Create a Helper Function
Create a helper function to construct the user context from request cookies and headers.
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.
Set Cookies in Middleware (Optional)
Use Express middleware to set user identification cookies, simplifying context retrieval in the helper function.
Middleware is optional—user identification logic can be implemented directly in getFlagSyncUserContext()
or elsewhere in your application.
Use the Helper in Routes
In your Express routes, use the helper to build the FsUserContext
for flag evaluations or event tracking.
Usage
Evaluate Flags
Evaluate flags with flag()
, which applies targeting rules, rollouts, and defaults for the user context:
An Impression is automatically registered when flag()
is called.
SDK Not Ready
If the SDK isn’t ready, it returns the defaultValue
or control
:
SDK Ready
Once ready, flag()
returns the server-evaluated value:
Track Events
Submit user actions with the track()
function:
See Events: Tracking to learn about numeric and property events.
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
All exposed errors are normalized to FsServiceError
.
Configuration
Configure the SDK with the FsConfig
interface:
Custom Attributes
- Define user attributes for targeting in Flags: User Segments.
- See User Context Identification for the recommended approach to building the context.
Ensure the key
in FsUserContext
is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.
Flag Syncing
Configure flag update strategies with the sync
object: stream
, poll
, or off
.
By default, flag updates propagate in milliseconds via server-side events (SSE), ensuring the latest values are used in evaluations.
Stream (Default)
Stream updates via SSE—flag updates are reevaluated on the server and sent to the client:
Polling
Poll the server on an interval:
Off
Disable syncing:
Best Practices
- Wait for SDK readiness before evaluating flags.
- Select a sync strategy (
stream
/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)