CLI reference
Functions CLI command reference
Interact with and test your Sanity Functions from the CLI.
The functions CLI command enables managing and testing functions. It's used alongside the blueprints command to create and deploy functions.
npx sanity functions --help
pnpm dlx sanity functions --help
yarn dlx sanity functions --help
bunx sanity functions --help
Commands
add
USAGE
$ sanity functions add
FLAGS
--example=<EXAMPLE> Example to use for the Function
-n, --name=<NAME> Name of the Function to add
--type=<TYPE> Document change event(s) that should trigger the function; you can specify multiple events by specifying this flag multiple times
--language=<LANGUAGE> Language of the new Function
--javascript Use JavaScript instead of TypeScript
--helpers Add helpers to the new Function
--installer=<INSTALLER> How to install the @sanity/functions helpers
-i, --install Shortcut for --fn-installer npm
DESCRIPTION
Scaffolds a new Function in the functions/ folder and templates a resource for your Blueprint manifest.
Functions are serverless handlers triggered by document events (create, update, delete, publish) or media library events.
After adding, use 'functions dev' to test locally, then 'blueprints deploy' to publish.
EXAMPLES
sanity functions add
sanity functions add --helpers
sanity functions add --name my-function
sanity functions add --name my-function --type document-create
sanity functions add --name my-function --type document-create --type document-update --lang jsdev
USAGE
$ sanity functions dev
FLAGS
-h, --host=<HOST> The local network interface at which to listen. [default: "localhost"]
-p, --port=<PORT> TCP port to start emulator on. [default: 8080]
-t, --timeout=<TIMEOUT> Maximum execution time for all functions, in seconds. Takes precedence over function-specific `timeout`
DESCRIPTION
Runs a local, web-based development server to test your functions before deploying.
Open the emulator in your browser to interactively test your functions with the payload editor.
Optionally, set the host and port with the --host and --port flags. Function timeout can be configured with the --timeout flag.
To invoke a function with the CLI, use 'functions test'.
EXAMPLES
sanity functions dev --host 127.0.0.1 --port 8974
sanity functions dev --timeout 60env
add
USAGE
$ sanity functions env add NAME KEY VALUE
ARGUMENTS
NAME The name of the Sanity Function
KEY The name of the environment variable
VALUE The value of the environment variable
DESCRIPTION
Sets an environment variable in a deployed Sanity Function. If the variable already exists, its value is updated.
Environment variables are useful for API keys, configuration values, and other secrets that shouldn't be hardcoded. Changes take effect on the next function invocation.
EXAMPLES
sanity functions env add MyFunction API_URL https://api.example.com/list
USAGE
$ sanity functions env list NAME
ARGUMENTS
NAME The name of the Sanity Function
DESCRIPTION
Displays all environment variables (keys only) configured in a deployed Sanity Function.
Use 'functions env add' to set variables or 'functions env remove' to delete them.
EXAMPLES
sanity functions env list MyFunctionremove
USAGE
$ sanity functions env remove NAME KEY
ARGUMENTS
NAME The name of the Sanity Function
KEY The name of the environment variable
DESCRIPTION
Deletes an environment variable from a deployed Sanity Function. The change takes effect on the next function invocation.
Use 'functions env list' to see current variables before removing.
EXAMPLES
sanity functions env remove MyFunction API_URLlogs
USAGE
$ sanity functions logs [NAME]
ARGUMENTS
[NAME] The name of the Sanity Function
FLAGS
-j, --json Return logs in JSON format
--stack=<STACK> Stack name or ID to use instead of the locally configured Stack
-l, --limit=<LIMIT> Total number of log entries to retrieve
-u, --utc Show dates in UTC time zone
-d, --delete Delete all logs for the function
-f, --force Skip confirmation for deleting logs
-w, --watch Watch for new logs (streaming mode)
DESCRIPTION
Fetches execution logs from a deployed function, useful for debugging production issues or monitoring activity.
Use --watch (-w) to stream logs in real-time. Use --delete to clear all logs for a function (requires confirmation unless --force is specified).
EXAMPLES
sanity functions logs <name>
sanity functions logs <name> --json
sanity functions logs <name> --limit 100
sanity functions logs <name> --deletetest
USAGE
$ sanity functions test [NAME]
ARGUMENTS
[NAME] The name of the Sanity Function
FLAGS
-d, --data=<DATA> Data to send to the function
--data-before=<DATA-BEFORE> Original document
--data-after=<DATA-AFTER> Current document
-e, --event=<EVENT> Type of event (create, update, delete)
-f, --file=<FILE> Read data from file and send to the function
--file-before=<FILE-BEFORE> Original document
--file-after=<FILE-AFTER> Current document
-t, --timeout=<TIMEOUT> Execution timeout value in seconds
-a, --api=<API> Sanity API Version to use
--dataset=<DATASET> The Sanity dataset to use
--project-id=<PROJECT-ID> Sanity Project ID to use
--document-id=<DOCUMENT-ID> Document to fetch and send to function
--document-id-before=<DOCUMENT-ID-BEFORE>Original document
--document-id-after=<DOCUMENT-ID-AFTER>Current document
--with-user-token Prime access token from CLI config
--media-library-id=<MEDIA-LIBRARY-ID>Sanity Media Library ID to use
DESCRIPTION
Executes a function locally with the provided payload, simulating how it would run when deployed. Use this to test your function logic before deploying.
Provide test data via --data (inline JSON), --file (JSON file), or --document-id (fetch from Sanity). For update events, use the before/after flag pairs to simulate document changes.
EXAMPLES
sanity functions test <name> --data '{ "id": 1 }'
sanity functions test <name> --file 'payload.json'
sanity functions test <name> --data '{ "id": 1 }' --timeout 60
sanity functions test <name> --event update --data-before '{ "title": "before" }' --data-after '{ "title": "after" }'