Using Edge Config with Split
Learn how to use Edge Config with Vercel's Split integration.Split is a feature flag provider that tracks event data, enabling you to release features, target them to audiences, and measure their impact on customer experience metrics securely.
The Split Edge Config integration enables you to write your Split rollout plan to an Edge Config. Doing so will allow you to evaluate feature flags at ultra-low latency with Vercel's Edge Network while tracking events and impressions data with Split.
Before using this integration, you should have:
-
The latest version of Vercel CLI. To check your version, use
vercel --version
. To install or update Vercel CLI, use:pnpm i -g vercel@latest
-
A project. If you don't have one, you can run the following terminal commands to create a Next project:
pnpm i next
terminalnpx create-next-app@latest
-
A Vercel project. If you don't have one, see Creating a Project
-
An Edge Config. If you don't have one, follow the Edge Config quickstart
-
The Edge Config SDK:
pnpm i @vercel/edge-config
The following steps will walk you through:
- Configuring Vercel's Split Edge Config integration
- Using it to evalute feature flags from your frontend code
To configure this integration, Split Admin access (Split Admin users can add feature flags and segments, and edit them at will) is required.
Visit the Split page in the Vercel Integration Marketplace and select the Add Integration button. From the Integration dialog:
- Select a Vercel team and project to connect the integration to
- Log into Split
- Select the Split Environment you want to use
- Select an existing Edge Config or create a new one
- Copy the Edge Config item key provided on this page. You'll need it to add it to your Environment Variables
You can also find your Edge Config Split item key in your dashboard on Vercel . In the Integrations tab, select Manage, then select Configure on the integration page. You should see the item key on the page that opens.
If you already have existing feature flags, you can skip this step and use those. In this example, we'll create one called
New_Marketing_Page
. You can set the user targeting to Joe and Bobby.To create a feature flag in Split:
- Log into your Split management console and select the workspace icon near the top-left of the page
- In the sidebar, under Target, select Feature flags. Add the name
New_Marketing_Page
, and set the traffic type touser
. Select Create to finish - With your feature flag created, select the feature flag and select the Definition tab. Select Initiate Environment to configure your flag
- Add valid users to the feature flag
- Scroll down to Targeting and select Add new individual target
- Under To user, add any username you want to test. This example uses
Joe
. - Select Add new individual target, then set the Description option to
off
. Add another username under To user. This example usesBobby
- Select Review Changes, then Create to finish
Next, you need to add your credentials to your project's local environment to use the Split integration in your code.
Next, you'll add the following credentials to your Vercel project:
SPLIT_SDK_CLIENT_API_KEY
EDGE_CONFIG_SPLIT_ITEM_KEY
EDGE_CONFIG
To add environment variables to your project, visit your Vercel dashboard and select the project you want to use the Split integration with. Then select Settings > Environment Variables.
To get your Split client-side API keys:
- Log into your Split management console and select the workspace icon near the top-left of the page
- In the list of options that appears, select Admin Settings, then navigate to API Keys -> SDK API Keys
- Copy the client-side keys associated with the workspace and environment you're using
To add your Edge Config Split item key, if you didn't copy it after setting up the integration on Vercel:
- Visit your dashboard on Vercel
- In the Integrations tab, select Manage
- On the integration page, select Configure
- You should see the item key on the page that opens. Copy it
To add your Edge Config's connection string to your project:
- Visit your project's page in the dashboard
- Select the Storage tab. Select Connect Store and select the Edge Config associated with your Split integration. The
EDGE_CONFIG
environment variable will be set automatically.
Now you're ready to use the Split Edge Config integration in your code.
Open your project's code on your local machine and do the following:
-
Install Split's Browser SDK, Vercel integration utilities, and Vercel's Edge Config SDK:
pnpm i @splitsoftware/splitio-browserjs @splitsoftware/vercel-integration-utils @vercel/edge-config
-
Create an API route in your project. The following example fetches a treatement based on which user is visiting. You can specify the user by appending
?userKey=Joe
or?userKey=Bobby
to the URL when visiting the route:
app/api/marketing-example/route.tsimport { SplitFactory, PluggableStorage, ErrorLogger, } from '@splitsoftware/splitio-browserjs'; import { EdgeConfigWrapper } from '@splitsoftware/vercel-integration-utils'; import { createClient } from '@vercel/edge-config'; export async function GET(request: Request) { const { EDGE_CONFIG_SPLIT_ITEM_KEY, SPLIT_SDK_CLIENT_API_KEY } = process.env; if (!SPLIT_SDK_CLIENT_API_KEY || !EDGE_CONFIG_SPLIT_ITEM_KEY) return new Response( `Failed to find your SDK Key (${SPLIT_SDK_CLIENT_API_KEY}) or item key ${EDGE_CONFIG_SPLIT_ITEM_KEY}`, ); const edgeConfigClient = createClient(process.env.EDGE_CONFIG); const { searchParams } = new URL(request.url); const userKey = searchParams.get('userKey') || 'anonymous'; const client = SplitFactory({ core: { authorizationKey: SPLIT_SDK_CLIENT_API_KEY, key: userKey, }, mode: 'consumer_partial', storage: PluggableStorage({ wrapper: EdgeConfigWrapper({ // The Edge Config item key where Split stores // feature flag definitions edgeConfigItemKey: EDGE_CONFIG_SPLIT_ITEM_KEY, // The Edge Config client edgeConfig: edgeConfigClient, }), }), // Disable or keep only ERROR log level in production, // to minimize performance impact debug: ErrorLogger(), }).client(); await new Promise((resolve) => { client.on(client.Event.SDK_READY, () => resolve); client.on(client.Event.SDK_READY_TIMED_OUT, () => resolve); }); // Replace this with the feature flag you want const FEATURE_FLAG = 'New_Marketing_Page'; const treatment = await client.getTreatment(FEATURE_FLAG); // Must await in app-router; waitUntil() is not // yet supported await client.destroy(); // treatment will be 'control' if the SDK timed out if (treatment == 'control') return new Response('Control marketing page'); return treatment === 'on' ? new Response('New marketing page') : new Response('Old marketing page'); }
-
- Start a local development server. If you're using Vercel CLI, enter the following command in the terminal:
terminalvercel dev
- Navigate to http://localhost:3000/api/split-example?userKey=Joe. You should see either
New marketing page
orOld marketing page
based on how your feature flags are configured in Split- Try changing the
userKey
search param's value toBobby
, or deleting it altogether, to see different responses when you visit the route
- Try changing the
Quickstart
Create and read from your Edge Config in minutes.
Read with the SDK
Read from your Edge Config at the fastest speeds.
Use the Dashboard
Manage your Edge Configs in the Vercel dashboard.
Manage with the API
Manage your Edge Configs with the Vercel API.
Edge Config Limits
Learn about the limits of Edge Configs.
Was this helpful?