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

Configure @sanity/client

Next

Common patterns

Was this page helpful?

On this page

  • Create a function
  • Develop locally
  • Add an environment variable
  • Additional env commands
FunctionsLast updated January 29, 2026

Add environment variables to functions

Learn to add environment variables to Functions.

Environment variables allow you to keep secrets, like tokens or API keys, hidden and out of version control. Functions allow you to manage environment variables from the CLI so they are available to your deployed functions.

In this guide, you'll learn to add environment variables and access them from within your function code.

Prerequisites:

  • Complete the Functions quick start, or be comfortable creating and deploying a function.
  • sanity CLI v3.88.1 or higher is required to interact with Blueprints and Functions. You can always run the latest CLI commands with npx sanity@latest.

Create a function

If you don't already have a Blueprint and function set up, create them now.

Initialize a Blueprint:

npx sanity@latest blueprints init
pnpm dlx sanity@latest blueprints init
yarn dlx sanity@latest blueprints init
bunx sanity@latest blueprints init

Add a function:

npx sanity blueprints add function
pnpm dlx sanity blueprints add function
yarn dlx sanity blueprints add function
bunx sanity blueprints add function

In this example, we'll set the function to trigger on Document Publish, use TypeScript, and set the name to envExample.

✔ Enter function name: envExample
✔ Choose function type: Document Publish
✔ Choose function language: TypeScript

This creates a function in the functions/envExample directory.

Develop locally

Environment variables aren't available locally, but you can simulate them by appending the variable and value to your CLI commands.

Start by updating the function to display the variable. For this example we'll reference a variable called SANITY_SECRET_SAUCE.

import { documentEventHandler } from '@sanity/functions'

export const handler = documentEventHandler(async ({ context, event }) => {
  console.log(`The secret: ${process.env.SANITY_SECRET_SAUCE}`)
})
export async function handler({context, event}) {
  console.log(`The secret: ${process.env.SANITY_SECRET_SAUCE}`)
}

All environment variables are accessible on process.env.

To test the function's access to a variable, we'll append it to the CLI command.

SANITY_SECRET_SAUCE="content operating system" npx sanity functions test envExample

If everything worked, you'll see this output:

Logs:
The secret: content operating system

Now that we know it works locally, let's make it work on Sanity's infrastructure.

Add an environment variable

Before you can add environment variables, you need to deploy the blueprint.

npx sanity blueprints deploy
pnpm dlx sanity blueprints deploy
yarn dlx sanity blueprints deploy
bunx sanity blueprints deploy

With the blueprint deployed, you can add environment variables to the function.

Add them with the functions env add <function-name> <variable-name> <variable-value> command.

npx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
pnpm dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
yarn dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
bunx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system

Now make changes to your documents, and check the logs for the function. You should see a similar log to the one from the local test.

npx sanity functions logs envExample
pnpm dlx sanity functions logs envExample
yarn dlx sanity functions logs envExample
bunx sanity functions logs envExample

You've now deployed and accessed an environment variable from a function.

As with other examples, if you're done with this function and blueprint, it's a good practice to destroy it to prevent unexpected billing.

npx sanity blueprints destroy
pnpm dlx sanity blueprints destroy
yarn dlx sanity blueprints destroy
bunx sanity blueprints destroy

Additional env commands

As we saw with the command earlier, environment variables are linked to individual functions. In addition to add, you can use the following commands to interact with them:

  • sanity functions env list <function-name>: List all environment variables for the given function.
  • sanity functions env remove <function-name> <variable-name>: removes the variable from the deployed function.

For additional usage information, add --help after each CLI command. You can read more about the CLI in the Functions CLI reference.

npx sanity@latest blueprints init
pnpm dlx sanity@latest blueprints init
yarn dlx sanity@latest blueprints init
bunx sanity@latest blueprints init
npx sanity@latest blueprints init
pnpm dlx sanity@latest blueprints init
yarn dlx sanity@latest blueprints init
bunx sanity@latest blueprints init
npx sanity blueprints add function
pnpm dlx sanity blueprints add function
yarn dlx sanity blueprints add function
bunx sanity blueprints add function
npx sanity blueprints add function
pnpm dlx sanity blueprints add function
yarn dlx sanity blueprints add function
bunx sanity blueprints add function
✔ Enter function name: envExample
✔ Choose function type: Document Publish
✔ Choose function language: TypeScript
import { documentEventHandler } from '@sanity/functions'

export const handler = documentEventHandler(async ({ context, event }) => {
  console.log(`The secret: ${process.env.SANITY_SECRET_SAUCE}`)
})
export async function handler({context, event}) {
  console.log(`The secret: ${process.env.SANITY_SECRET_SAUCE}`)
}
SANITY_SECRET_SAUCE="content operating system" npx sanity functions test envExample
npx sanity blueprints deploy
pnpm dlx sanity blueprints deploy
yarn dlx sanity blueprints deploy
bunx sanity blueprints deploy
npx sanity blueprints deploy
pnpm dlx sanity blueprints deploy
yarn dlx sanity blueprints deploy
bunx sanity blueprints deploy
npx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
pnpm dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
yarn dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
bunx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
npx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
pnpm dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
yarn dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
bunx sanity functions env add envExample SANITY_SECRET_SAUCE content operating system
npx sanity functions logs envExample
pnpm dlx sanity functions logs envExample
yarn dlx sanity functions logs envExample
bunx sanity functions logs envExample
npx sanity functions logs envExample
pnpm dlx sanity functions logs envExample
yarn dlx sanity functions logs envExample
bunx sanity functions logs envExample
npx sanity blueprints destroy
pnpm dlx sanity blueprints destroy
yarn dlx sanity blueprints destroy
bunx sanity blueprints destroy
npx sanity blueprints destroy
pnpm dlx sanity blueprints destroy
yarn dlx sanity blueprints destroy
bunx sanity blueprints destroy