Sanity CMS integration
This topic explains how to use the integration between LaunchDarkly and Sanity CMS. The integration connects LaunchDarkly feature flags to Sanity Studio so that content editors can manage A/B/n test variations on individual content fields directly inside Sanity, while LaunchDarkly handles experiment targeting and traffic allocation.
Use the integration to:
- Add A/B/n test variations to individual content fields in Sanity Studio
- Sync feature flags and their variations automatically from LaunchDarkly
- Let content editors author variant content without developer intervention
- Serve personalized content based on LaunchDarkly targeting rules at runtime
- Filter which flags appear in the Studio using LaunchDarkly tags
The integration stores variant content in Sanity entries alongside each field’s default value. Your application uses a LaunchDarkly SDK to evaluate the active variation and queries Sanity with that variation to retrieve the correct content.
How the integration works
The integration uses the @sanity/personalization-plugin, a Sanity Studio plugin that enables A/B/n experiments on individual content fields. When you install and configure the plugin, content editors can assign LaunchDarkly feature flags to fields and author variant content directly in the Studio.
When configured, the plugin:
- Registers new schema types for each field type you enable for experiments. For example, enabling the
stringtype createsflagStringandvariantStringtypes. - Fetches available feature flags and their variations from LaunchDarkly using a Reader API token you configure in the Studio
- Lets editors assign a flag to a field, then author content for each variation
- Stores the default value and all variant content on the Sanity entry
All flag targeting rules and traffic allocation remain managed in LaunchDarkly. The integration reads flags but does not create or modify them.
Prerequisites
To use the integration, you must have:
- A Sanity Studio v3 project
- A LaunchDarkly account with access to API tokens, projects, and environments
- Node.js 18 or later
- A LaunchDarkly API access token with the Reader role, or a custom role with read access to flags
Create or manage API access tokens on the Authorization page. To learn more, read API access tokens.
Set up the Sanity integration
Setting up the Sanity integration involves three steps:
After setup, content editors can add experiments to fields directly in Sanity Studio.
Install the plugin
To install the plugin in your Sanity Studio project, enter the following command in your terminal:
Configure the plugin
Before you configure the plugin, configure your LaunchDarkly flags with string variation types and descriptive variation names. You can also apply tags to filter which flags appear in the Studio.
Here is an example flag configuration:
To configure the plugin in sanity.config.ts:
- Import
fieldLevelExperimentsfrom@sanity/personalization-plugin/launchDarkly. - Add the plugin to your Sanity config and provide your LaunchDarkly project key.
- Specify the field types you want to enable for experiments.
- Optionally, provide
tagsto filter which flags appear in the Studio.
Here is an example sanity.config.ts:
Set the project key in your .env file as follows:
The API token is stored securely in Sanity using @sanity/studio-secrets and is never exposed to your frontend application.
Expand plugin configuration options
Enable experiment fields
The plugin registers new schema types for each field type you configure. For each type, the plugin creates two new types: one for the flag field and one for each variant entry.
Expand field type mappings
To use experiment-enabled fields, replace the original field type with the corresponding flag type in your schema.
Here is an example schema:
Add experiments in Sanity Studio
After configuring the plugin and updating your schema, content editors can add experiments to fields directly in the Studio.
To add an experiment to a field:
- Open a document with experiment-enabled fields.
- Enter your LaunchDarkly API token when prompted. The Studio stores this token securely. You only need to enter it once.
- Select the three-dot field actions menu on an experiment-enabled field.
- Select Add flag.
- Choose a flag from the menu, which is populated from your LaunchDarkly project.
- Add variant content using the quick-add buttons.
- Use the Copy default button to duplicate the default field value to a variant as a starting point.
The Studio displays a visual distinction between the default content and each variant.
Stored fields and implementation
This section describes how the integration stores experiment data in Sanity and provides examples for retrieving variant content in your application.
Stored fields on entries
The integration stores the following data on each experiment-enabled field in Sanity:
Expand stored field properties
For example, a flagString field with two variants is stored as in this example:
Each variant object in the variants array contains:
Expand variant object properties
Implementation examples
These examples show how to evaluate feature flags and retrieve the mapped variant content in an application.
Resolve variant content using GROQ
Use the coalesce function in GROQ (Graph-Relational Object Queries), Sanity’s query language, to retrieve the correct variant content for the active variation. If no variant matches, the query falls back to the default value.
Here is an example of a GROQ query with variant resolution:
Evaluate a flag and fetch personalized content
This example shows how a Next.js application retrieves the active variation from LaunchDarkly and passes it to Sanity to fetch the matching content:
Handle multiple flags
For documents with multiple experiment fields using different flags, pass each flag key and variant ID separately.
Here is an example:
Troubleshooting
If you encounter issues with the LaunchDarkly Sanity integration, try these solutions.
Flags do not appear in the Studio menu
If no flags appear when adding an experiment to a field:
- Verify that your API token has read access to the selected project.
- Confirm that
projectKeyin the plugin config matches your LaunchDarkly project key, not the SDK key. - Verify that the expected flags have all the specified tags applied in LaunchDarkly when using the
tagsoption. - Refresh the Studio page to re-fetch flags from LaunchDarkly.
API token errors
If the Studio displays a token error or repeatedly prompts for a token:
- Navigate to a document with experiment-enabled fields.
- Enter a new valid LaunchDarkly API token when prompted. The old token is replaced.
- Confirm that the token has Reader access or a custom role with read access to flags.
Variant content is not delivered correctly
If your application serves the default content instead of the expected variant:
- Confirm that the
variantIdpassed to your GROQ query matches the variation value defined in LaunchDarkly, not the variation name. - Verify that the Sanity document is published. Draft changes are not visible to your frontend.
- Confirm that the
flagIdon the field matches the flag key in your GROQ query. - Clear any CDN caches on your frontend after publishing changes in Sanity.
Schema types are not available
If flagString or other flag types are not available in your schema, confirm that the field type is listed in the fields array in the plugin config. Then restart the Sanity Studio dev server after updating the plugin configuration.