Next.js SDK
Integrate FlagSync feature flags into Next.js applications with Vercel Flags.
Overview
The @flagsync/nextjs-sdk
integrates into Next.js applications with Vercel Flags for feature management and event tracking—ideal for SSR and static site generation workflows.
Installation
Install the SDK and required dependencies with your preferred package manager:
Quickstart
Set up the SDK in your Next.js application with these steps:
Initialize the Client
Initialize the FlagSync client singleton.
Define and Use a Feature Flag
Create a type-safe feature flag and use it in your Server Components.
See User Context Identification for the recommended approach to building the context.
Initialization
Get Your SDK Key
Find your server-side SDK key in your workspace settings. Keep server-side keys private to protect flag rules.
Initialize the SDK
Initialize the FlagSync client singleton with your server-side SDK key:
Wait for Readiness
The SDK automatically initializes during the Next.js App Router lifecycle—no manual waiting is required for Server Components.
User Context Identification
Define the user context with a custom identify()
function that returns an FsUserContext
object. Pass this function to Vercel’s flag()
function.
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 cookies and headers.
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 Up Identification
Create identify
with the helper, linking flags to user contexts.
Set Cookies in Middleware (Optional)
Set user identification cookies in middleware to simplify context retrieval
Middleware is optional—user identification logic can be implemented directly in createIdentify()
or getFlagSyncUserContext()
.
Usage
Evaluate Flags
Evaluate flags in Server Components using the Vercel Flags flag
function, which applies targeting rules, rollouts, and defaults for the user context:
An Impression is automatically registered when you evaluate the flag.
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.
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:
Bootstrapping
Initialize the SDK with a set of bootstrap flags:
Best Practices
- 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)