Roles and authentication
Structured content
Was this page helpful?
How to install and use the Sanity's official Vercel integration
Sanity’s Vercel integration lets you connect your Vercel and Sanity projects. The Integration lets you manage features like billing and plan management directly in Vercel.
The integration adds environment variables to your Vercel project(s) with the Project ID, a given dataset name, and an editor token with read+write permissions. You can use these environment variables to connect your project on Vercel to Sanity’s Content Lake and fetch, create, and update data in it.
Go to Vercel's integrations marketplace and follow the instructions.
We recommend you to choose the Native Integration option as this lets you to have a deeper integration with Vercel’s ecosystem.
Select Install and follow the steps to set up the connection. These are a few key decisions you need to make:
NEXT_PUBLIC in the examples below.Once the installation completes, follow the getting started guide for your framework of choice.
This integration adds your project information as environment variables. You can go to Vercel's documentation to learn more about how to use and configure them.
The project ID will be exposed by the following environment variables in your Vercel project.
SANITY_API_PROJECT_IDSANITY_STUDIO_API_PROJECT_IDNEXT_PUBLIC_SANITY_PROJECT_IDProject IDs are considered public.
The dataset will be exposed by the following environment variables in your Vercel project.
SANITY_API_DATASETNEXT_PUBLIC_SANITY_DATASETSANITY_STUDIO_API_DATASETDataset names are considered public.
The write token will be exposed on the following environment variables in your Vercel project:
SANITY_API_WRITE_TOKENSince write tokens give access to changing data in your dataset, they should be considered secret.
Here is an example of a serverless function that can be run on Vercel that will take a request with some data, and create a new document of that data in the Sanity Content Lake:
const {createClient} = require('@sanity/client');
const config = {
projectId: process.env.SANITY_API_PROJECT_ID,
dataset: process.env.SANITY_API_DATASET,
token: process.env.SANITY_API_WRITE_TOKEN,
useCdn: false,
apiVersion: '2021-03-25'
};
async function handleForm(req, res) {
const payload = JSON.parse(req.body);
try {
const result = await createClient(config).create(payload);
return res.status(200).send('ok');
} catch (error) {
return res.status(500)send('error');
}
}
export default handleForm;When you sign in to your studio, or when the CLI prompts you to authenticate, select the sign-in method that matches your Sanity account configuration:
If you've signed in to Sanity but don't see the projects or data you expect, you may have multiple accounts.
If you used Sanity before the Vercel integration, you likely already had an account. During integration provisioning, a new account may have been created. To resolve this:
You may have accidentally created a separate account. You have two options:
Before continuing with the Getting Started guide, add another sign-in method in Account Settings to ensure seamless access between sign-in methods.
Projects set up via the Vercel integration need to be managed through the integration dashboard in Vercel.
Deleting a project via Sanity management interface is not supported. Plan changes need to happen through the integration Dashboard in Vercel.
const {createClient} = require('@sanity/client');
const config = {
projectId: process.env.SANITY_API_PROJECT_ID,
dataset: process.env.SANITY_API_DATASET,
token: process.env.SANITY_API_WRITE_TOKEN,
useCdn: false,
apiVersion: '2021-03-25'
};
async function handleForm(req, res) {
const payload = JSON.parse(req.body);
try {
const result = await createClient(config).create(payload);
return res.status(200).send('ok');
} catch (error) {
return res.status(500)send('error');
}
}
export default handleForm;