CLI reference
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.
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>]
Start the Sanity Function emulator
Options
--port <port> Port to start emulator on
Examples
# Start dev server on default port
sanity functions dev
# Start dev server on specific port
sanity functions dev --port 3333
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]]
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
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 --name echo --json
# Delete all logs for Sanity Function
sanity functions logs --name echo --delete
test
usage: npx sanity functions test <name> [--data <json>] [--file <filename>] [--timeout <seconds>] [--api <version>] [--dataset <name>] [--project-id] <id>]
Invoke a local Sanity Function
Arguments
<name> The name of the Sanity Function
Options
--data <data> Data to send to the function
--file <file> Read data from file and send to the function
--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
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
Was this page helpful?