Sanity logosanity.ioAll Systems Operational© Sanity 2026
Change Site Theme
Sanity logo

Documentation

    • Overview
    • Platform introduction
    • Next.js quickstart
    • Nuxt.js quickstart
    • Astro quickstart
    • React Router quickstart
    • Studio quickstart
    • Build with AI
    • Content Lake
    • Functions
    • APIs and SDKs
    • Visual Editing
    • Blueprints
    • Platform management
    • Dashboard
    • Studio
    • Canvas
    • Media Library
    • App SDK
    • Content Agent
    • HTTP API
    • CLI
    • Libraries
    • Specifications
    • Changelog
    • User guides
    • Developer guides
    • Courses and certifications
    • Join the community
    • Templates
Functions
Overview

  • Introduction
  • Get started

    Create a Document Function
    Create a Media Library Asset Function
    Official recipes

  • Concepts

    Manage dependencies
    Local testing

  • Guides

    Configure @sanity/client
    Add environment variables
    Common patterns

  • Reference

    Handler reference
    CLI reference

On this page

Previous

Common patterns

Was this page helpful?

On this page

  • context properties
  • clientOptions properties
  • Example context
  • event properties
  • Example event
  • Example handler
  • Type support
  • Basic usage
  • Pass type for event data
  • Type only (TypeScript)
  • Type only (JavaScript)
FunctionsLast updated January 29, 2026

Function handler reference

Reference documentation for the shape of the function wrapper.

Overview

Learn how to take advantage of Functions in your Sanity projects.

Quick start

Start building with Functions by deploying a new function to Sanity's infrastructure.

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. The function does not require a return value.

context properties

  • clientOptionsobject

    Provides properties for configuring the Sanity client (@sanity/client). Most commonly used to pass details about the invoking project dataset to a client configuration. See the configuring @sanity/client in Functions guide for details.

  • localboolean

    The context.local value is set to true for functions invoked with sanity functions test and sanity functions dev. This can be helpful when you want code to only execute in local environments.

    It is undefined for functions in production.

  • eventResourceTypestring

    The resource type that triggered the function. For Document functions, this would be dataset. For Media Library functions, this would be media-library.

  • eventResourceIdstring

    The resource ID that triggered the function. For Document functions, this would be the ID of a dataset in the form <project-id>.<dataset-name>. For Media Library functions, this would be the Media Library id.

clientOptions properties

  • projectIdstring

    The ID of the project that triggered this function.

  • datasetstring

    The dataset name of the project that triggered this function.

    The sanity functions test command won't include a dataset by default. Run with the --dataset flag to pass a dataset to clientOptions. For example: sanity functions test log-event --dataset production

  • apiHoststring

    Defaults to https://api.sanity.io.

  • tokenstring

    A token provided by the function with access to your Sanity project. This token is automatically generated with the editor role and added to your project when deploying the blueprint.

    The sanity functions test command won't include a token by default. Run with the --with-user-token flag to pass a the logged-in user's token.

    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

Contains the shape of the event. In the case of document triggers, like publish, the event shape is the document. This will vary based on your schema.

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)
}

Type support

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
pnpm add -D @sanity/functions
yarn add --dev @sanity/functions
bun add --dev @sanity/functions

You can then import and use the documentEventHandler helper to provide type support. See the example TS handler above for implementation details.

Basic usage

Import documentEventHandler.

import {documentEventHandler} from '@sanity/functions'

export const handler = documentEventHandler(async ({context, event}) => {
  // Your function implementation
  console.log('Document updated:', event.data)
})

Pass type for event data

If you need to type event.data, and you know the shape of your incoming data, you can provide it to documentEventHandler.

import {documentEventHandler} from '@sanity/functions'

interface NotificationData {
  documentId: string
  text: string
}

export const handler = documentEventHandler<NotificationData>(async ({event}) => {
  console.log(event.data.text) // Typed as `string`
  console.log(event.data.notSet) // Will yield type error
})

Type only (TypeScript)

Import the DocumentEventHandler type.

import {type DocumentEventHandler} from '@sanity/functions'

export const handler: DocumentEventHandler = async ({context, event}) => {
  // …
}

// …you can also define the data type:
export const handler: DocumentEventHandler<{text: string}> = async ({event}) => {
  console.log(event.data.text)
}

Type only (JavaScript)

Use the @type comment syntax.

/** @type {import('@sanity/functions').DocumentEventHandler} */
export const handler = async ({context, event}) => {
  console.log(event.data.text)
}

// …you can also define the data type:
/** @type {import('@sanity/functions').DocumentEventHandler<{text: string}>} */
export const handler = async ({event}) => {
  console.log(event.data.text)
}

  • Article
  • Changelog
{
  clientOptions: {
    apiHost: 'https://api.sanity.io',
    projectId: 'abc123',
    dataset: 'production',
    token: '***************'
  }
}
{
  data: { 
    _id: '1234',
    _type: 'article',
    title: 'Functions quick start',
    _createdAt: '2025-04-24T16:26:58.901Z',
    _publishedAt: '2025-04-24T16:26:58.901Z',
  }
}
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)
}
npm install -D @sanity/functions
pnpm add -D @sanity/functions
yarn add --dev @sanity/functions
bun add --dev @sanity/functions
npm install -D @sanity/functions
pnpm add -D @sanity/functions
yarn add --dev @sanity/functions
bun add --dev @sanity/functions
import {documentEventHandler} from '@sanity/functions'

export const handler = documentEventHandler(async ({context, event}) => {
  // Your function implementation
  console.log('Document updated:', event.data)
})
import {documentEventHandler} from '@sanity/functions'

interface NotificationData {
  documentId: string
  text: string
}

export const handler = documentEventHandler<NotificationData>(async ({event}) => {
  console.log(event.data.text) // Typed as `string`
  console.log(event.data.notSet) // Will yield type error
})
import {type DocumentEventHandler} from '@sanity/functions'

export const handler: DocumentEventHandler = async ({context, event}) => {
  // …
}

// …you can also define the data type:
export const handler: DocumentEventHandler<{text: string}> = async ({event}) => {
  console.log(event.data.text)
}
/** @type {import('@sanity/functions').DocumentEventHandler} */
export const handler = async ({context, event}) => {
  console.log(event.data.text)
}

// …you can also define the data type:
/** @type {import('@sanity/functions').DocumentEventHandler<{text: string}>} */
export const handler = async ({event}) => {
  console.log(event.data.text)
}