Vercel SDK reference
Read time: 4 minutes
Last edited: Nov 11, 2024
Overview
This topic documents how to get started with the Vercel SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | LaunchDarkly Vercel SDK |
The Vercel SDK is designed to be used with one of the LaunchDarkly client-side SDKs as follows:
- The Vercel SDK gets all flags at the edge for a given context, and bootstraps them onto a cached payload
- The client-side SDK initializes the bootstrapped payload
- The client-side SDK evaluates the flags and sends events back to LaunchDarkly
If you are using Vercel SDK version 1.2.0 or later, then the Vercel SDK can send events back to LaunchDarkly directly. Using a client-side SDK is not necessary. You do need to configure the SDK to enable sending events. To learn more, read Configuration.
Configure the Vercel integration to use this SDK successfully. To learn more, read Vercel.
The Vercel integration is available to customers on an Enterprise plan. To learn more, read about our pricing. To upgrade your plan, contact Sales.
Get started
After you complete the Install a Vercel integration process in an existing project, follow these instructions to start using the LaunchDarkly Vercel SDK:
Install the SDK
First, install the Vercel SDK as a dependency in your application using your application's dependency manager.
Here's how:
yarn add @launchdarkly/vercel-server-sdk
Next, import the LaunchDarkly client in your application code:
import { init } from '@launchdarkly/vercel-server-sdk'import { createClient } from '@vercel/edge-config'
Initialize the client
After you install and import the SDK, create an edge client using your Edge Config ID. Then, initialize an LDClient
using your LaunchDarkly client-side ID and this edge client.
Here's how:
const edgeConfigClient = createClient(process.env.EDGE_CONFIG)const ldClient = init('<client-side-id-123abc>', edgeConfigClient)await ldClient.waitForInitialization()
The Vercel SDK uses a client-side ID to associate the LaunchDarkly environment with the CDN integration. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.
If you are using the Vercel SDK version 1.2.0 or later, you can optionally configure sending events during initialization. This enables Experimentation and metrics use cases. To learn more, read Experimentation and metric events.
Here's how:
const ldClient = init('<client-side-id-123abc>', edgeConfigClient, { sendEvents: true });await ldClient.waitForInitialization();
Evaluate a flag
After you initialize the client, wait for the waitForInitialization
function to resolve. When waitForInitialization
is resolved, the client can serve feature flags.
Using the client, you can check which variation a particular context will receive for a given feature flag. In your Vercel Edge application, place the variation
code so that it is invoked as needed.
Here is an example:
const ldContext = {kind: 'org',key: 'org-key-123abc',someAttribute: 'example-attribute-value',}const flagValue = await ldClient.variation('flag-key-123abc', ldContext, true)
Bootstrap flags with Next.js
If you are using Next.js with Vercel, you can bootstrap feature flags on the Root Layout for use in the LaunchDarkly React Web SDK. To bootstrap flags, specify the root layout's runtime with a value of edge
and pass the flags to a client component that initializes the LaunchDarkly React SDK.
The Vercel SDK's GitHub repository contains an example application that takes advantage of bootstrapping flags from the edge for use in the LaunchDarkly React Web SDK.
Promises and async
All asynchronous SDK methods that return a Promise
are compatible with then/catch
or async/await
. You can use either.
Shut down the client
If you send events, you must flush those events before your worker exits to ensure that they are sent back to LaunchDarkly. To learn more, read Flushing events.
Shut down the client when your application terminates. To learn more, read Shutting down.