Using Global Config with LaunchDarkly
This guide will help you get started with using Vercel's LaunchDarkly integration with Global Config. This integration allows you to use Global Config as a configuration source for your LaunchDarkly feature flags.
LaunchDarkly allows you to enable and disable feature flags dynamically, decoupling feature rollouts from deployments. The LaunchDarkly Global Config integration enables you to evaluate flags in the region closest to the user without making network calls to LaunchDarkly.
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:Terminalpnpm i -g vercel@latestTerminalyarn global add vercel@latestTerminalnpm i -g vercel@latestTerminalbun add -g vercel@latest - A project. If you don't have one, you can run the following terminal commands to create a Next project:
pnpm create next-app@latest yarn create next-app@latest npx create-next-app@latest bunx create-next-app@latest - A Vercel project. If you don't have one, see Creating a Project
- A Global Config. If you don't have one, follow the Global Config quickstart
- The Global Config SDK:
Terminal
pnpm i @vercel/global-configTerminalyarn add @vercel/global-configTerminalnpm i @vercel/global-configTerminalbun add @vercel/global-config
Visit the LaunchDarkly page in the 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 LaunchDarkly
- Select the Authorize button to allow the integration to access your LaunchDarkly account data
- Name the integration, and select an existing Global Config or create a new one
To use the integration, you'll need your client-side ID from LaunchDarkly. Here's how to add it to your project:
- Go to the settings page of your LaunchDarkly dashboard.
- Select the LaunchDarkly project your integration is connected to
- On the next page, copy the Client-side ID under the environment your integration is connected to (for example, Test or Production)
Now, you must add the value to your project as an Environment Variable:
- Navigate to your Vercel dashboard and select the project you want to use LaunchDarkly with
- Under the Settings tab, navigate to Environment Variables, and create an
LD_CLIENT_SIDE_IDvariable with the value of your client-side ID
Open your project's code on your local machine and do the following:
- Install LaunchDarkly's Vercel Server SDK:
Terminalpnpm i @launchdarkly/vercel-server-sdkTerminalyarn add @launchdarkly/vercel-server-sdkTerminalnpm i @launchdarkly/vercel-server-sdkTerminalbun add @launchdarkly/vercel-server-sdk-
Use Vercel CLI to pull your Vercel project's environment variables:
vercel env pull -
Finally, create a
middleware.tsfile at the root of your project. This file will configure a Middleware that redirects your site visitors from/homepageto/new-homepagebased on a feature flag fetched from LaunchDarkly:middleware.tsimport { init } from '@launchdarkly/vercel-server-sdk'; import { createClient } from '@vercel/global-config'; const globalConfigClient = createClient(process.env.GLOBAL_CONFIG!); const launchDarklyClient = init( process.env.LD_CLIENT_SIDE_ID!, globalConfigClient, ); export const config = { // Only run the middleware on the /homepage route matcher: '/homepage', }; export default async function middleware( request: Request, ): Promise<Response | undefined> { await launchDarklyClient.initFromServerIfNeeded(); const launchDarklyContext = { kind: 'org', key: 'my-org-key' }; const showExperimentalHomepage = await launchDarklyClient.variation( 'experimental-homepage', launchDarklyContext, true, ); if (showExperimentalHomepage) { const url = new URL(request.url); url.pathname = '/new-homepage'; return Response.redirect(url); } }middleware.jsimport { init } from '@launchdarkly/vercel-server-sdk' import { createClient } from '@vercel/global-config' const globalConfigClient = createClient(process.env.GLOBAL_CONFIG); const launchDarklyClient = init( process.env.LD_CLIENT_SIDE_ID, globalConfigClient, ); export const config = { // Only run the middleware on the /homepage route matcher: '/homepage', }; export default async function middleware(request) { await launchDarklyClient.initFromServerIfNeeded(); const launchDarklyContext = { kind: 'org', key: 'my-org-key' }; const showExperimentalHomepage = await launchDarklyClient.variation( 'experimental-homepage', launchDarklyContext, true ); if(showExperimentalHomepage) { const url = new URL(request.url); url.pathname = '/new-homepage'; return Response.redirect(url); } }
Now that you have set up the LaunchDarkly Global Config integration, you can explore the following topics to learn more:
Was this helpful?