Sanity — Write Content
Create, update, and delete documents in your Sanity dataset using the Sanity client.
Last updated: 2026-03-29
Install & Configure
Install the Sanity client and set up your project credentials.
npm install @sanity/client
// src/lib/sanity.js
import { createClient } from '@sanity/client';
export const client = createClient({
projectId: import.meta.env.PUBLIC_SANITY_PROJECT_ID,
dataset: import.meta.env.PUBLIC_SANITY_DATASET || 'production',
apiVersion: '2024-01-01',
useCdn: false,
token: import.meta.env.SANITY_WRITE_TOKEN, // write token — server only
});Create a Document
Use client.create() to add new documents to your dataset.
// Create a new blog post
const doc = await client.create({
_type: 'post',
title: 'Hello World',
slug: { current: 'hello-world' },
publishedAt: new Date().toISOString(),
});
console.log('Created:', doc._id);Update a Document
Use client.patch() to update specific fields on an existing document.
await client
.patch('document-id')
.set({ title: 'Updated Title' })
.commit();Delete a Document
Use client.delete() to remove a document by ID.
await client.delete('document-id');