Client
JavaScript client for retrieving, creating and patching data from Sanity.io
Install / Use
/learn @sanity-io/ClientREADME
@sanity/client
[![gzip size][gzip-badge]][bundlephobia]
[![size][size-badge]][bundlephobia]
JavaScript client for Sanity. Works in modern browsers, as well as runtimes like [Node.js], [Bun], [Deno], and [Edge Runtime]
QuickStart
Install the client with a package manager:
npm install @sanity/client
Import and create a new client instance, and use its methods to interact with your project's [Content Lake]. Below are some simple examples in plain JavaScript. Read further for more comprehensive documentation.
// sanity.js
import {createClient} from '@sanity/client'
// Import using ESM URL imports in environments that supports it:
// import {createClient} from 'https://esm.sh/@sanity/client'
export const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset-name',
useCdn: true, // set to `false` to bypass the edge cache
// Set default headers to be included with all requests
headers: {
'X-Custom-Header': 'custom-value'
},
apiVersion: '2025-02-06', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
// token: process.env.SANITY_SECRET_TOKEN // Needed for certain operations like updating content, accessing drafts or using draft perspectives
})
// uses GROQ to query content: https://www.sanity.io/docs/groq
export async function getPosts() {
const posts = await client.fetch('*[_type == "post"]')
return posts
}
export async function createPost(post: Post) {
const result = client.create(post)
return result
}
export async function updateDocumentTitle(_id, title) {
const result = client.patch(_id).set({title})
return result
}
Table of contents
- QuickStart
- Requirements
- Installation
- API
- Creating a client instance
- Specifying API version
- Request tags
- Performing queries
- Using perspectives
- Fetching Content Source Maps
- Listening to queries
- Fetch a single document
- Fetch multiple documents in one go
- Creating documents
- Creating/replacing documents
- Creating if not already present
- Patch/update a document
- Setting a field only if not already present
- Removing/unsetting fields
- Incrementing/decrementing numbers
- Patch a document only if revision matches
- Adding elements to an array
- Appending/prepending elements to an array
- Deleting an element from an array
- Delete documents
- Multiple mutations in a transaction
- Clientless patches & transactions
- Release and version operations
- Uploading assets
- Deleting an asset
- Mutation options
- Aborting a request
- Get client configuration
- Set client configuration
- Actions
- Agent Actions
- Media Library API
- License
- From
v5 - From
v4- No longer shipping
ES5 - Node.js
v12no longer supported - The
defaultexport is replaced with the named exportcreateClient client.assets.deleteis removedclient.assets.getImageUrlis removed, replace with@sanity/image-urlSanityClientstatic properties moved to named exportsclient.clientConfigis removed, replace withclient.config()client.isPromiseAPI()is removed, replace with aninstanceofcheckclient.observable.isObservableAPI()is removed, replace with aninstanceofcheckclient._requestObservableis removed, replace withclient.observable.requestclient._dataRequestis removed, replace withclient.dataRequestclient._create_is removed, replace with one ofclient.create,client.createIfNotExistsorclient.createOrReplaceclient.patch.replaceis removed, replace withclient.createOrReplaceclient.authis removed, replace withclient.request
- No longer shipping
Requirements
Sanity Client transpiles syntax for [modern browsers]. The JavaScript runtime must support ES6 features such as class, rest parameters, spread syntax and more. Most modern web frameworks, [browsers][modern browsers], and developer tooling supports ES6 today.
For legacy ES5 environments we recommend v4.
Installation
The client can be installed from [npm]:
npm install @sanity/client
# Alternative package managers
yarn add @sanity/client
pnpm install @sanity/client
