Unlock 1 Year of EdgeOne + 1TB CDN: Join Our Developer Journey
Get Started Now !
Sanity Integration
Sanity is a modern Headless CMS that provides powerful content management features and a flexible API, making it an excellent choice for building high-performance websites. By combining Sanity with the Next.js framework, we can build websites that are both easy to manage and offer outstanding performance.
Overview
This article introduces how to use Sanity as a Headless CMS and integrate it with the Next.js framework to build high-performance websites. With EdgeOne Pages' one-click deployment capability, you can quickly complete the integration of Sanity and Next.js while obtaining global CDN acceleration to achieve the best performance and user experience for your website.
Getting Started
EdgeOne Pages provides a complete Sanity + Next.js integration solution template. You can click the deploy button in the template for quick deployment.
This article's integration guide mainly includes the following three steps: configure Sanity as a data source, use Next.js to generate pages, and deploy to EdgeOne Pages. If you want to learn about the specific implementation principles of this solution or need to perform customized development according to your own requirements, you can continue reading the detailed configuration steps below.
Configuring Sanity
1. Register a Sanity Account
To use Sanity, the first step is to create an account. Visit the Sanity official website, click "Get Started" to begin registration. Sanity offers only a 30-day free trial, and paid use is required afterward.
2. Creating a Project
After registration is completed, you need to create a new Sanity project. First, enter the Sanity management interface, find and click the "Select a project" button in the top navigation bar, select "New project" in the pull-down menu, then:
1. Enter the project name
2. Select whether to use the default dataset configuration
3. Select a project template (select the "Next.js" template)
4. Click "Create project" to complete the creation

These contents will be used for subsequent testing of data sync and display functionality.
3. Configuring API Access
After creating a project, we also need to configure related options for the API. In the Sanity management interface, click the "API" option in the following tab, and you will see the API configuration panel, which includes the following configuration options:
Tokens: Used for creating and managing API access tokens
CORS origins: Configure the domains allowed to access the API. When your Next.js application is deployed to a cloud service, you need to use AJAX requests to access the Sanity API to retrieve data. At this point, you need to configure your application domain name in CORS origins to ensure that the Sanity server returns the correct cross-domain access headers (CORS headers), allowing your application to normally access API data.

For your data security, carefully save the Project ID and Dataset Name. They will be used for configuration of the connection between Next.js and Sanity.
4. Configure a Content Model
Content management in Sanity is achieved through Sanity Studio. Sanity Studio is a customizable content management interface that provides an intuitive interface to manage your content.
The main features of Sanity Studio include:
Visualize creating and editing content
Custom content editing interface
Manage media files (images, videos)
Preview content in real time
Manage user permissions and access control
You can create Sanity Studio through the following command on the terminal of your local machine.
npm create sanity@latest -- --project {projectId} --dataset production --template clean --typescript --output-path studio-{your-project-name}cd studio-{your-project-name}
Replace {projectId} with your Sanity project ID, and {your-project-name} with the project name you want.

The content model definitions in Sanity Studio are located under the
schemaTypes
directory. You can define multiple model files here, and then import them collectively in schemaTypes/index.ts
. For example, we can create a postType.ts
file to define the content model for blog articles:import {defineField, defineType} from 'sanity'export const postType = defineType({name: 'post',title: 'Post',type: 'document',fields: [defineField({name: 'title',type: 'string',validation: (rule) => rule.required(),}),defineField({name: 'slug',type: 'slug',options: {source: 'title'},validation: (rule) => rule.required(),}),defineField({name: 'category',type: 'string',validation: (rule) => rule.required(),})],})
Then import this model in
schemaTypes/index.ts
:import {postType} from './postType'export const schemaTypes = [postType]
This content model defines the following fields:
title: Article title (required)
slug: Article URL alias (required, auto-generated based on the title)
category: Article category (required)
You can modify these field definitions as needed, add or delete fields.
5. Starting Sanity Studio
Run the following command in the project root directory to launch Sanity Studio:
npm run dev
After startup, access
http://localhost:3333
to enter the Sanity Studio content management interface.
6. Deploying Sanity Studio
After completing the content configuration, you need to deploy Studio to the Sanity platform. Run `npm run deploy` in the project root directory.
During deployment, the system will prompt you to enter a project name (for example: my-blog). After deployment, you can access your Sanity Studio via
https://my-blog.sanity.studio
. Now, the local Studio interface you previously accessed via http://localhost:3333
can be accessed through this online address.Next, you can add content in the online Studio.

We have now completed the configuration of the Sanity content management system. You can now easily manage website content through the online Studio. Next, we will begin configuring the frontend application to retrieve these contents via the Sanity API and display them on the website.
Generating Static Files
We have already configured the content management system, but we still need a static site generator to display these contents. EdgeOne has prepared a complete template based on Next.js for you, which you can use directly. Let's start acquiring the template.
git clone --depth 1 --single-branch https://github.com/TencentEdgeOne/pages-templates.git tempmv temp/examples/portfolio-with-sanity .rm -rf tempcd portfolio-with-sanity
dependency installation: npm install.
The default content model file postType.ts is provided under the portfolio-with-sanity project. You can refer to it as needed and copy it to the studio project.
The configuration code for reading Sanity data is already included in the template, located in
src/lib/sanity.ts
:import { createClient } from 'next-sanity'const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET || 'production';if (!projectId || !dataset) {throw new Error('Sanity environment variable not set. Please ensure NEXT_PUBLIC_SANITY_PROJECT_ID and NEXT_PUBLIC_SANITY_DATASET are set up');}console.log('Sanity configuration:', {projectId,dataset,apiVersion: '2024-03-01',useCdn: false, // Disable CDN during local developmentperspective: 'published'});export const client = createClient({projectId,dataset,apiVersion: '2024-03-01',useCdn: false, // Disable CDN during local development})
Create a
.env.local
file in the project root directory and fill in the following content:NEXT_PUBLIC_SANITY_PROJECT_ID=${your sanity space ID}NEXT_PUBLIC_SANITY_DATASET=${your sanity dataset}
Let's first execute npm run dev locally to preview and verify whether the project deployment is correct:

Note: When deploying to EdgeOne Pages, you need to set the following parameters in the environment variable configuration:NEXT_PUBLIC_SANITY_PROJECT_ID
: Your Sanity Project IDNEXT_PUBLIC_SANITY_DATASET
: Your Sanity Dataset name
We have now completed the configuration of the Sanity content management system, the retrieval of the frontend template, and the verification of the local environment. Next, we only need to deploy the code to EdgeOne Pages, and your website can officially go live.
Deploying to EdgeOne Pages
1. Publishing Code to Git
Submit the code to the git repository to complete the deployment. Assuming you have created the
portfolio-with-sanity
project and associated the current directory with the project, use the following command to submit:git add .git commit -m "Initial commit"git push origin main
2. Importing a Project Into Pages
After submission, if you are already an EdgeOne Pages user and have associated your GitHub account, just visit the console to deploy the corresponding project.

3. Adding an Environment Variable
Click Deploy on portfolio-with-sanity, then on the preparation deployment page, click "Environment Variables" and configure the following environment variables separately:
NEXT_PUBLIC_SANITY_PROJECT_ID
: Fill in your Sanity Project IDNEXT_PUBLIC_SANITY_DATASET
: Fill in your Sanity Dataset Name
After the configuration is complete, click the "Start Deployment" button. Once deployed, the success interface will display.

We have completed the full deployment process of the Sanity solution. With this solution, you can:
Use Sanity to manage content and enjoy its powerful Headless CMS functionality
Leverage Next.js to generate high-performance pages and achieve ultimate access speed
Realize automatic deployment through EdgeOne Pages Simplify ops
Obtain global CDN acceleration, enhance user experience
More Related Content
View more Sanity integration solutions: Sanity templates provided by EdgeOne Pages
Learn more about Next.js features: Next.js Official Documentation
Explore more Sanity features: Sanity Official Documentation