
LaunchDarkly Flags SDK Example
This example uses LaunchDarkly for feature flags with the Flags SDK along with the @flags-sdk/launchdarkly LaunchDarkly adapter and the Flags Explorer.
How it works
This demo uses two feature flags on LaunchDarkly to control the visibility of two banners on the page. Both flags are configured to show/hide each banner 50% of the time.
Once you visit the page, you can see a variation of both/one/none of the banners. Since this example is using a stable id to identify users, you will see the same variation until you reset your id.
To test different variations, you can use the Dev Tools at the bottom to reset the stable id and reload the page. This allows you to test different variations of the banners.
If you deployed your own and configured the feature flags on LaunchDarkly, you can also use the Flags Explorer to test different variations by creating overrides.
Deploy this template
The easiest way to get started with LaunchDarkly is through the integration in Vercel Marketplace.
Step 1: Link the project
In order to use the Flags Explorer, you need to link the project on your local machine.
vercel link
Select the project from the list you just deployed.
Step 2: Pull all environment variables
This allows the Flags SDK and the Flags Explorer to work correctly, by getting additional metadata.
vercel env pull
Step 3: Create Feature Flags
Head over to the LaunchDarkly Console and create the feature flags and experiments required by this template.
Be sure to enable the SDKs using Client-side ID option for each Feature Flag.
Feature Flags:
Summer Sale(type boolean) with the keysummer-saleand the variationstrueandfalse. Edit the default targeting rule to serve a percentage rollout with a 50/50 split byuser.key.Free Delivery(type boolean) with the keyfree-deliveryand the variationstrueandfalse. Edit the default targeting rule to serve a percentage rollout with a 50/50 split byuser.key.Proceed to Checkout(type string) with the keyproceed-to-checkoutand the following variations:- Name:
Control, Value:blue - Name:
Test, Value:green - Name:
Test #2, Value:red
- Name:
You can also find the flag keys in the flags.ts file.
Ensure all Flags are ON.
Step 4: Configure the Experiment
Create the Proceed to Checkout experiment:
- Name:
Proceed to Checkout - Hypothesis:
Button color influences rate at which customers proceed to checkout - Type:
Feature change - Randomization Unit:
user - Randomization Attribute:
key - Metric: Create a new metric:
- Event kind:
Custom - Event key:
proceed-to-checkout-clicked - What do you want to measure?
Occurence (conversion: binary) - Metric definition:
Percentage of user units that sent the event, where higher is better - Metric name:
Proceed to Checkout Clicked
- Event kind:
- Varaiations: Choose flag
proceed-to-checkout - Audience:
- In this experiment: Choose
Customand enter100% - Split audience: Choose
Split equally
- In this experiment: Choose
- Statistical approach: Default values
After that, start the Experiment.
Step 6: Set environment variables
See .env.example for a template.
FLAGS_SECRETEDGE_CONFIG(Vercel Edge Config connection string)LAUNCHDARKLY_PROJECT_SLUGLAUNCHDARKLY_CLIENT_SIDE_IDNEXT_PUBLIC_LAUNCHDARKLY_CLIENT_SIDE_ID(set to same value asLAUNCHDARKLY_CLIENT_SIDE_ID)
(Optional) If you provide the LAUNCHDARKLY_API_KEY, LAUNCHDARKLY_PROJECT_KEY and LAUNCHDARKLY_ENVIRONMENT environment variables, the Flags Explorer will fetch additional metadata from the LaunchDarkly API.
This will show the description (if set) and displays a link to the feature flag on the LaunchDarkly Console.
You can create an API key and find project and environment values in the LaunchDarkly Console.

