Skip to main content
This documentation is intended for implementing the GrowthBook adapter for Vercel’s Flags SDK in Next.js back-end or hybrid environments. For front-end-only GrowthBook implementations, please see our React docs.

Installation

Install with a package manager

Adapter Usage

Import the Default Adapter

A default adapter is available for use, assuming the appropriate environment variables are set

Environment Variables

The default adapter automatically uses following environment variables:

Create a Custom Adapter

You can provide custom configuration by using createGrowthBookAdapter:

User Identification

GrowthBook uses Attributes to evaluate feature flags and experiments. You should write an identify function providing these Attributes to GrowthBook flags:
Dedupe is used above to ensure that the Attributes are computed once per request.

Adapter Methods and Properties

.feature<T>()

This method implements the Adapter interface for a GrowthBook feature. Typically flag definitions are applied in single file (e.g. flags.ts).
You may optionally pass in an options object for further customization (ex: feature({ exposureLogging: false }))

.initialize()

Initializes the GrowthBook SDK. This is done on-demand when a growthbook flag is evaluated, and is not required to be called manually:

.setTrackingCallback()

Set a back-end callback to handle experiment exposures. This allows you to log exposures to your analytics platform. Typically this is done in the same file where your flags are defined (e.g. flags.ts).
Front-end experiment tracking is also supported, although it requires additional manual setup. See client-side tracking for more information.

.setStickyBucketService()

Sticky bucketing ensures that users see the same experiment variant, even when user session, user login status, or experiment parameters change. See the Sticky Bucketing docs for more information. If your organization and experiment supports sticky bucketing, you must implement an instance of the StickyBucketService to use Sticky Bucketing.

.growthbook

You may access the underlying GrowthBook instance. Specifically, our Flags SDK adapter wraps the GrowthBookClient class read more. All user evaluation options (attributes, tracking callbacks, sticky buckets) are applied at the userContext level (not globally).

.stickyBucketService

If you have set a sticky bucket service, you may retrieve its instance.

Edge Config

The adapter can load your SDK payload from Vercel’s Edge Config to lower the latency of feature flag evaluation:
  • Set GROWTHBOOK_EDGE_CONNECTION_STRING (or EXPERIMENTATION_CONFIG if installed through the Vercel Marketplace) in your environment. Optionally set GROWTHBOOK_EDGE_CONFIG_ITEM_KEY to override the default key name (defaults to your client key).
  • Or pass edgeConfig directly to the adapter.
If Edge Config is not set, the adapter will fetch configuration from GrowthBook’s API.

Configuring a SDK Webhook

  1. To automatically populate the Edge Config whenever your feature definitions change, create a SDK Webhook on the same SDK Connection that you are using for the Next.js integration.ts
  2. Select “Vercel Edge Config” as the webhook type and fill out the following fields:
  • Vercel Edge Config ID (begins with ecfg_)
  • Team ID (optional)
  • Vercel API Token (see Vercel → Account Settings → Tokens)
Under the hood, the webhook is being configured with the following properties. If you need to change any of these settings for any reason, you can always edit the webhook.
  • Endpoint URL is being set to
  • Method is being set to PATCH
  • An Authorization: Bearer token header is being added with your Vercel API Token
  • The Payload format is being set to Vercel Edge Config
Vercel Edge Config limitationsVercel’s Edge Config is subject to storage size limitations. If your SDK payload is excessively large, you may not be able to populate the your Edge Config. Read more about limitations here.

Additional Configuration

  • Initialization: The adapter auto-initializes when a flag is evaluated. To pre-initialize, call initialize() manually.
  • Exposure Logging: By default, exposures are logged when flags are evaluated. You can disable this with exposureLogging: false or provide a custom tracking callback (applicable to back-end tracking only).

Flags Explorer Integration

To expose GrowthBook data to the Flags Explorer, use the getProviderData function in your API route:
Note the required environment variable GROWTHBOOK_API_KEY. You will need to create and provide either a Personal Access Token (read-only) or an API Key. You will also need to provide a FLAGS_SECRET environment variable. See Vercel’s Flags Explorer guide.

Client-side Tracking

When using the Flags SDK, we strongly encourage using server-side tracking when possible. However, some event trackers are best suited for client-side implementation. Additionally, some user attributes may only be available in a front-end context. We recommend implementing client-side tracking using some glue components:
  1. A <GrowthbookTracking> server component which takes a list of evaluated feature ids and prepares the data necessary to hydrate the tracking calls for the client. It embeds a <GrowthbookTrackingClient> client component.
  2. A <GrowthbookTrackingClient> client component responsible for executing and deduping your tracking calls in the browser. You will need to define a client-side tracking callback within this component. Under the hood, this component works by replaying all of the experiment evaluations in a client context.
  3. You must include <GrowthbookTracking> in any component which evaluates a feature flag (or specifically: all referenced flags that may trigger experiment tracking callbacks).
1. <GrowthbookTracking> components/growthbook/client-side-tracking/growthbook-tracking.tsx
2. <GrowthbookTrackingClient> components/growthbook/client-side-tracking/client.tsx
3. Implement tracking using <GrowthbookTrackingClient> ex: app/layout.tsx
Follow the example implementation here .

Examples

Supported Features