SkillAgentSearch skills...

Client

JavaScript client for retrieving, creating and patching data from Sanity.io

Install / Use

/learn @sanity-io/Client
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

@sanity/client

npm stat npm version [![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

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

A

View on GitHub
GitHub Stars109
CategoryDevelopment
Updated6d ago
Forks35

Languages

TypeScript

Security Score

100/100

Audited on Mar 28, 2026

No findings