Function handler reference
Reference documentation for the shape of the function wrapper.
Every Function must export a handler
. Handlers contain the logic that the Function infrastructure runs when your document changes trigger the function.
Create a function handler with the sanity blueprints add function
command. Every handler receives an object containing context
and event
parameters.
context
properties
clientOptionsobject
Coming soon
clientOptions
properties
projectIdstring
The ID of the project that triggered this function.
datasetstring
The dataset name of the project that triggered this function.
apiHoststring
Defaults to
https://api.sanity.io
.tokenstring
A token provided by the function with access to your Sanity project. Note: the token is obfuscated in logs for security. You can directly use it to configure the Sanity client or to make API calls.
Example context
{
clientOptions: {
apiHost: 'https://api.sanity.io',
projectId: 'abc123',
dataset: 'production',
token: '***************'
}
}
event
properties
dataobject
A Sanity Document containing the default document shape and available values, or the shape defined in the Blueprints configuration function's
event.projection
.
Example event
{
data: {
_id: '1234',
_type: 'article',
title: 'Functions quick start',
_createdAt: "2025-04-24T16:26:58.901Z",
_publishedAt: "2025-04-24T16:26:58.901Z",
}
}
Example handler
import { documentEventHandler } from '@sanity/functions'
export const handler = documentEventHandler(async ({ context, event }) => {
console.log("Context: ", context)
console.log("Event: ", event)
})
export async function handler({context, event}) {
console.log("Context: ", context)
console.log("Event: ", event)
}
TypeScript types
When you create a new TypeScript function with sanity blueprint add
, you'll be prompted to add types.
If you did not add types as part of the init process, they are available in the @sanity/functions package:
npm install -D @sanity/functions
You can then import and use the documentEventHandler
helper to provide type support. See the example TS handler above for implementation details.
Was this page helpful?