APIs and SDKs

Getting started with @sanity/client

Learn how to install and configure the official Sanity JavaScript client for querying and mutating content across different environments.

The @sanity/client library is the official JavaScript client for interacting with Sanity's APIs. It provides a type-safe way to query and mutate your content from any JavaScript environment, including browsers, Node.js, Deno, Bun, and edge runtimes.

With @sanity/client, you can:

Requirements

To use the client, you need:

  • A JavaScript runtime: Node.js 18+, modern browsers, Bun, Deno, or edge runtimes.
  • A Sanity project with a project ID and dataset name.
  • An API token (optional, required only for authenticated requests like mutations or accessing private datasets).

Installation

Install @sanity/client using your preferred package manager:

Basic client setup

Create a client instance using the createClient() function with your project configuration:

Query with the client

Retrieving your content from Content Lake is often done with client.fetch.

You can learn more about fetching content in our querying content guide.

Common configuration options

The client accepts several configuration options to customize its behavior. Here are the most commonly used options:

  • RequiredprojectIdstring

    Your Sanity project ID. You can find this at sanity.io/manage.

  • Requireddatasetstring

    The dataset name. Common values are production, staging, or development.

  • useCdnboolean

    When set to true, queries use Sanity’s global CDN for faster response times and lower latency. Set to false when you need the freshest data or are performing mutations. Default is true.

  • RequiredapiVersionstring

    Specifies which version of the Sanity API to use. Set to a YYYY-MM-DD format. This ensures your application continues to work as expected when the API evolves. Learn more about API versions.

  • tokenstring

    An authentication token for accessing private datasets or performing mutations. You can create tokens in your project settings.

Additional configuration options can be found in the client reference documentation.

Build on configuration with withConfig

You can extend the functionality of an existing client and change some values as needed with withConfig.

Use within Studio

You can use the client within Studio components by importing useClient. It comes pre-configured with settings passed down from the containing Studio components. Note that this method requires that you set an API version.

import {useClient} from 'sanity'

const client = useClient({
  apiVersion: '2026-03-01'
})

Environment-specific examples

The @sanity/client library works across different JavaScript environments. Here are examples for common setups:

Was this page helpful?