- Statsig automatically pushes project changes to Vercel’s Edge Config, providing low latency SDK startup.
- Statsig offers a Vercel helper that handles client initialization and event flushing, so you can focus on your business logic.
1
Configure Integration
- Head over to the Vercel Marketplace and install the Statsig integration.
-
You will be prompted to create a new Statsig account or link an existing Statsig account.
New Statsig Account
- Click Accept and Create
- Enable Edge Config Syncing to have statsig automatically push configs into Vercel’s Edge Config
Once created, you can select Open in Statsig in the top right to set up gates and experiments for your newly created Statsig project.
Your Statsig Integration for Vercel is completed!
Existing Statsig Account
- Select your project and edge config
- Click Save
2
Install the Statsig SDK
3
import the Vercel helper
4
Use the SDK
handlerThis is your Vercel function codeparamsParameter Optional Type Description configKeyNo stringThe Key associated with your Statsig specs in your Edge Config envStatsigKeyNo stringYour Statsig Client API Key statsigOptionsYes StatsigOptionsSee StatsigOptions here
- Store
configKeyandenvStatsigKeyas environment variables in your Vercel project settings
Example Usage
api/index.js
How it Works
How it Works
The
Instead of exporting it, you pass it into
handler parameter is your Vercel function code .This is the same code you would normally export directly in your API route (for example, myHandler in the snippet above).Instead of exporting it, you pass it into
handleWithStatsig, which takes care of the Statsig setup and cleanup for you.Here’s what happens behind the scenes:- The Statsig SDK initializes a client using the config specs stored in your Edge Config.
- Your function code (the handler) runs as usual.
- Any events you log are automatically flushed back to Statsig when execution finishes.
- Initializes the Statsig Client with config specs from your Edge Config
- Executes your Vercel function code (Your business logic + Statsig usage)
- Flushes all events after your handler completes execution
- Cleans up resources
Advanced Implementation
Advanced/manual usage
Advanced/manual usage
Use the advanced/manual setup if:The client instantiation takes two arguments:This is a gate check in code.The This is an event log in code.The This flushes all events from the SDK to Statsig. Without this, you will not be able to get diagnostic information in the Statsig Console, nor any event data you logged.
- You need fine-grained control over initialization timing
- You need fine-grained control over event flushing timing
- You need to customize error handling behavior
Prerequisites
- Completed the Statsig Vercel integration setup
1
Install the Statsig SDK
2
Import the Statsig SDK
3
Creating a StatsigVercelClient instance
sdkKey : stringThis is your Statsig client API key. It is available from the Project Settings page in the Statsig Console. This is used to authenticate your requests.options : StatsigOptionsSee here, for more options.
- Store
sdkKeyas an environment variable in your Vercel project settings
4
Client Initialization
The following line initializes the client by loading feature gate and experiment configurations directly from your Vercel Edge Config.The client initialization takes one argument:
ConfigKey : stringThe Key associated with your Statsig specs in your Edge Config
5
Checking a Gate
checkGate method takes two arguments:name : stringThe name of the Statsig gate that you are checking.user : StatsigUserThe Statsig user object for whom the gate is being checked. For more information on the user object, see here.
6
logEvent method takes two parameters:eventOrName : string | StatsigEventThis is the name and details of the event you are logging.user : StatsigUserThe Statsig user object for whom the event is being logged.
7
Putting it all together
api/index.js
Other Considerations
Polling for updates v5.13.0+
The SDK cannot poll for updates across requests since Vercel Edge Functions do not allow for timers outside of the request handler. To optimize for edge use cases, we do not provide an api to recognize updates to your config specs. However, when a change is made to your project definition on the Statsig console, the changes will be propagated to your Edge Config and will be reflected the next time you initialize the Statsig client.Flushing events v4.16.0+
The SDK enqueues logged events and flushes them in batches. In order to ensure events are properly flushed, we recommend calling flush usingwaitUntil() from @vercel/functions. This will keep the request handler alive until events are flushed without blocking the response.Size Limits
Vercel Edge Config has maximum size limits that may prevent Statsig from pushing configs into your Edge Config. See here for the latest Vercel Edge Config limits.Unsupported Features
Statsig ID Lists are not currently synced into Vercel Edge Config. If you rely on large (>1000) ID lists, you will not be able to check them in your Vercel edge functions. This is why we setinitStrategyForIDLists: 'none' in the SDK initialization.
Using Flags SDK in NextJS
If you’re using NextJS in your Vercel project, you can use Statsig through Flags SDK and take advantage of built in precompute patterns for improved performance. See the Statsig Adapter for Flags SDK docs for steps on how to get started. Note that the marketplace app sets all required environment variables for the Flags SDK by default for a quick setup process.Sending logs to Statsig
You can connect your Vercel logs to Statsig with a Log Drain to start exploring them in Logs Explorer.- From the Vercel dashboard, go to Settings -> Drains and click Add Drain -> Integration.
- Select Statsig, follow the configuration steps provided, and choose a project to connect with the service.
- Navigate to Statsig’s Logs Explorer to see your logs flow through.