Configure @sanity/client in Functions
Learn to use the JavaScript client in a Sanity Function.
Functions have the ability to connect back to Sanity and manipulate not only the incoming document, but any document in your dataset. By including details about your project in the context, Functions enable you to configure a @sanity/client
instance and interact with any Sanity API.
In this guide, you'll learn to install and configure @sanity/client
in a Sanity Function. Then you'll use it to make interact with your project dataset.
Prerequisites:
- Complete the Functions quick start, or be comfortable writing and deploying a Sanity Function.
sanity
CLI v3.88.1 or higher is required to interact with Blueprints and Functions. You can always run the latest CLI commands withnpx sanity@latest
.
Create a function
If you don't already have a Blueprint and function set up, create them now.
Initialize a Blueprint:
npx sanity@latest blueprints init
Add a function:
npx sanity blueprints add function
In this example, we'll set the function to trigger on Document Publish, use TypeScript, and set the name to clientExample.
✔ Enter function name: clientExample
✔ Choose function type: Document Publish
✔ Choose function language: TypeScript
This creates a function in the functions/clientExample
directory.
Add @sanity/client
to the function
Functions are self-contained. To install the Sanity client, first navigate into the clientExample
directory so that you're in the same directory as the index.ts
file.
Next, install @sanity/client
:
npm install @sanity/client
Once the install completes, open the function's index.ts
file and update the starter code.
- Import
createClient
. - Use
context.clientOptions
to configure the client. - Make and log a request to the API to test the client.
import { createClient } from '@sanity/client'
export async function handler({context, event}) {
const client = createClient({
...context.clientOptions,
apiVersion: '2025-05-08',
})
const posts = await client.fetch('*[_type == "post"]')
console.log(posts)
}
The context includes a clientOptions
object with details about your project, dataset, and a robot token. Read more about clientOptions in the handler reference.
Use clientOptions
along with any prefered settings to create a new client. Aside from the values included with clientOptions
, you should also set your preferred apiVersion
.
Additional client-specific configuration options, and usage guides for the JavaScript client are available in the @sanity/client
readme.
Tips for using the client
- Be cautious mutating the incoming document—the one that triggered the function—in a way that will trigger it again.
- Don't re-fetch the
event
document unless you need. Use the incoming payload to save a request. - The Sanity client isn't required to make requests to Sanity. If you're focused on the fastest, lightest function possible, you can build API calls manually with Node's
fetch
and the token, projectId, and dataset fromcontext.clientOptions
.
Was this page helpful?