- 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
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
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
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: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.
sanity.config.ts:
.env file as follows:
@sanity/studio-secrets and is never exposed to your frontend application.
Expand plugin configuration options
Expand plugin configuration options
| Option | Type | Required | Description |
|---|---|---|---|
projectKey | String | Yes | Your LaunchDarkly project key |
fields | Array | Yes | Field types to enable for experiments. Accepts type name strings or complete field definitions. |
tags | Array | No | Filter flags to only those with all specified tags |
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
Expand field type mappings
| Original type | Flag type | Variant type |
|---|---|---|
string | flagString | variantString |
text | flagText | variantText |
image | flagImage | variantImage |
| Custom types | flag | variant |
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.
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
Expand stored field properties
| Property | Type | Description |
|---|---|---|
_type | String | Schema type, such as flagString |
default | Varies | The default/control content value |
active | Boolean | Whether the experiment is active on this field |
flagId | String | LaunchDarkly feature flag key |
variants | Array | Array of variant content objects |
flagString field with two variants is stored as in this example:
variants array contains:
Expand variant object properties
Expand variant object properties
| Property | Type | Description |
|---|---|---|
_key | String | Unique key for the array element |
_type | String | Schema type, such as variantString |
flagId | String | LaunchDarkly feature flag key |
variantId | String | LaunchDarkly variation value |
value | Varies | The variant content value |
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 thecoalesce 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
IfflagString 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.