Was this page helpful?
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:
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.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.
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 systemNow that we know it works locally, let's make it work on Sanity's infrastructure.
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
env commandsAs 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 initpnpm dlx sanity@latest blueprints inityarn dlx sanity@latest blueprints initbunx sanity@latest blueprints initnpx sanity@latest blueprints initpnpm dlx sanity@latest blueprints inityarn dlx sanity@latest blueprints initbunx sanity@latest blueprints initnpx sanity blueprints add functionpnpm dlx sanity blueprints add functionyarn dlx sanity blueprints add functionbunx sanity blueprints add functionnpx sanity blueprints add functionpnpm dlx sanity blueprints add functionyarn dlx sanity blueprints add functionbunx sanity blueprints add function✔ Enter function name: envExample
✔ Choose function type: Document Publish
✔ Choose function language: TypeScriptimport { 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 envExamplenpx sanity blueprints deploypnpm dlx sanity blueprints deployyarn dlx sanity blueprints deploybunx sanity blueprints deploynpx sanity blueprints deploypnpm dlx sanity blueprints deployyarn dlx sanity blueprints deploybunx sanity blueprints deploynpx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systempnpm dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systemyarn dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systembunx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systemnpx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systempnpm dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systemyarn dlx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systembunx sanity functions env add envExample SANITY_SECRET_SAUCE content operating systemnpx sanity functions logs envExamplepnpm dlx sanity functions logs envExampleyarn dlx sanity functions logs envExamplebunx sanity functions logs envExamplenpx sanity functions logs envExamplepnpm dlx sanity functions logs envExampleyarn dlx sanity functions logs envExamplebunx sanity functions logs envExamplenpx sanity blueprints destroypnpm dlx sanity blueprints destroyyarn dlx sanity blueprints destroybunx sanity blueprints destroynpx sanity blueprints destroypnpm dlx sanity blueprints destroyyarn dlx sanity blueprints destroybunx sanity blueprints destroy