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
    • Agent Actions
    • 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

Next

Deploy custom functions to automate content operations

Was this page helpful?

On this page

  • Commands
  • dev
  • env
  • logs
  • test
FunctionsLast updated September 16, 2025

Functions

Reference documentation for the Sanity CLI Functions command.

The functions CLI command enables managing and testing functions. It's used alongside the blueprints command to create and deploy functions.

Functions introduction

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

Blueprints introduction

Learn what Blueprints are, how they work, and how to get started.

usage: npx sanity functions [--default] [-v|--version] [-d|--debug] [-h|--help] <command> [<args>]

Commands:
   dev   Start the Sanity Function emulator
   env   Add or remove an environment variable or list environment variables for a Sanity function
   logs  Retrieve or delete logs for a Sanity Function
   test  Invoke a local Sanity Function

See 'npx sanity help functions <command>' for specific information on a subcommand.

Commands

dev

usage: npx sanity functions dev [--port <port> --open]

   Start the Sanity Function emulator

Options
  --port <port> Port to start emulator on
  --open Open dev server in a new browser tab

Examples
  # Start dev server on default port
  sanity functions dev

  # Start dev server on specific port
  sanity functions dev --port 3333

  # Start dev server and open a new browser tab
  sanity functions dev --open

env

usage: npx sanity functions env <add|list|remove> <name> [key] [value]

   Add or remove an environment variable or list environment variables for a Sanity function

Commands
  add    Add or update an environment variable
  list   List the environment variables
  remove Remove an environment variable

Arguments
  <name> The name of the function
  <key> The name of the environment variable
  <value> The value of the environment variable

Examples
  # Add or update an environment variable
  sanity functions env add echo API_URL https://api.example.com/

  # Remove an environment variable
  sanity functions env remove echo API_URL

  # List environment variables
  sanity functions env list echo

logs

usage: npx sanity functions logs <name> [--limit <number>] [--json] [--utc] [--delete [--force]] [--watch]

   Retrieve or delete logs for a Sanity Function

Arguments
  <name> The name of the Function to retrieve logs for

Options
  --limit <limit> The number of log entries to retrieve [default 50]
  --json          If set return json
  --utc           Use UTC dates in logs
  --delete        Delete all logs for the Function
  --force         Force delete all logs for the Function
  --watch         Watch for new logs (streaming mode)

Examples
  # Retrieve logs for Sanity Function
  sanity functions logs echo

  # Retrieve the last two log entries for Sanity Function
  sanity functions logs echo --limit 2

  # Retrieve logs for Sanity Function in json format
  sanity functions logs echo --json

  # Delete all logs for Sanity Function
  sanity functions logs echo --delete

  # Watch for new logs (streaming mode)
  sanity functions logs echo --watch

test

usage: npx sanity functions test <name> [--event create|update|delete] [--data <json>] [--data-before <json>] [--data-after <json>] [--file <filename>] [--file-before <filename>] [--file-after <filename>] [--document-id <id>] [--document-id-before <id>] [--document-id-before <id>] [--timeout <seconds>] [--api <version>] [--dataset <name>] [--project-id] <id>] [--with-user-token]

   Invoke a local Sanity Function

Arguments
  <name> The name of the Sanity Function

Options
  --event <create|update|delete> The type of event to simulate (default: 'create')
  --data <data> Data to send to the function
  --data-before <data> Data to send to the function when event is update
  --data-after <data> Data to send to the function when event is update
  --file <file> Read data from file and send to the function
  --file-before <file> Read data from file and send to the function when event is update
  --file-after <file> Read data from file and send to the function when event is update
  --document-id <id> Document to fetch and send to function
  --document-id-before <id> Document to fetch and send to function when event is update
  --document-id-after <id> Document to fetch and send to function when event is update
  --timeout <timeout> Execution timeout value in seconds
  --api <version> Sanity API Version to use
  --dataset <dataset> The Sanity dataset to use
  --project-id <id> Sanity Project ID to use
  --with-user-token Prime access token from CLI config into context.clientOptions


Examples
  # Test function passing event data on command line
  sanity functions test echo --data '{ "id": 1 }'

  # Test function passing event data via a file
  sanity functions test echo --file 'payload.json'

  # Test function passing event data on command line and cap execution time to 60 seconds
  sanity functions test echo --data '{ "id": 1 }' --timeout 60

  # Test function simulating an update event
  sanity functions test echo --event update --data-before '{ "title": "before" }' --data-after '{ "title": "after" }'
  • Article
  • Changelog
usage: npx sanity functions [--default] [-v|--version] [-d|--debug] [-h|--help] <command> [<args>]

Commands:
   dev   Start the Sanity Function emulator
   env   Add or remove an environment variable or list environment variables for a Sanity function
   logs  Retrieve or delete logs for a Sanity Function
   test  Invoke a local Sanity Function

See 'npx sanity help functions <command>' for specific information on a subcommand.
usage: npx sanity functions dev [--port <port> --open]

   Start the Sanity Function emulator

Options
  --port <port> Port to start emulator on
  --open Open dev server in a new browser tab

Examples
  # Start dev server on default port
  sanity functions dev

  # Start dev server on specific port
  sanity functions dev --port 3333

  # Start dev server and open a new browser tab
  sanity functions dev --open
usage: npx sanity functions env <add|list|remove> <name> [key] [value]

   Add or remove an environment variable or list environment variables for a Sanity function

Commands
  add    Add or update an environment variable
  list   List the environment variables
  remove Remove an environment variable

Arguments
  <name> The name of the function
  <key> The name of the environment variable
  <value> The value of the environment variable

Examples
  # Add or update an environment variable
  sanity functions env add echo API_URL https://api.example.com/

  # Remove an environment variable
  sanity functions env remove echo API_URL

  # List environment variables
  sanity functions env list echo
usage: npx sanity functions logs <name> [--limit <number>] [--json] [--utc] [--delete [--force]] [--watch]

   Retrieve or delete logs for a Sanity Function

Arguments
  <name> The name of the Function to retrieve logs for

Options
  --limit <limit> The number of log entries to retrieve [default 50]
  --json          If set return json
  --utc           Use UTC dates in logs
  --delete        Delete all logs for the Function
  --force         Force delete all logs for the Function
  --watch         Watch for new logs (streaming mode)

Examples
  # Retrieve logs for Sanity Function
  sanity functions logs echo

  # Retrieve the last two log entries for Sanity Function
  sanity functions logs echo --limit 2

  # Retrieve logs for Sanity Function in json format
  sanity functions logs echo --json

  # Delete all logs for Sanity Function
  sanity functions logs echo --delete

  # Watch for new logs (streaming mode)
  sanity functions logs echo --watch
usage: npx sanity functions test <name> [--event create|update|delete] [--data <json>] [--data-before <json>] [--data-after <json>] [--file <filename>] [--file-before <filename>] [--file-after <filename>] [--document-id <id>] [--document-id-before <id>] [--document-id-before <id>] [--timeout <seconds>] [--api <version>] [--dataset <name>] [--project-id] <id>] [--with-user-token]

   Invoke a local Sanity Function

Arguments
  <name> The name of the Sanity Function

Options
  --event <create|update|delete> The type of event to simulate (default: 'create')
  --data <data> Data to send to the function
  --data-before <data> Data to send to the function when event is update
  --data-after <data> Data to send to the function when event is update
  --file <file> Read data from file and send to the function
  --file-before <file> Read data from file and send to the function when event is update
  --file-after <file> Read data from file and send to the function when event is update
  --document-id <id> Document to fetch and send to function
  --document-id-before <id> Document to fetch and send to function when event is update
  --document-id-after <id> Document to fetch and send to function when event is update
  --timeout <timeout> Execution timeout value in seconds
  --api <version> Sanity API Version to use
  --dataset <dataset> The Sanity dataset to use
  --project-id <id> Sanity Project ID to use
  --with-user-token Prime access token from CLI config into context.clientOptions


Examples
  # Test function passing event data on command line
  sanity functions test echo --data '{ "id": 1 }'

  # Test function passing event data via a file
  sanity functions test echo --file 'payload.json'

  # Test function passing event data on command line and cap execution time to 60 seconds
  sanity functions test echo --data '{ "id": 1 }' --timeout 60

  # Test function simulating an update event
  sanity functions test echo --event update --data-before '{ "title": "before" }' --data-after '{ "title": "after" }'