Functions

Use robot tokens with Functions

Learn how to authenticate Sanity Functions using robot tokens defined with Blueprints.

Robot tokens enable your functions to authenticate API calls to Sanity without managing credentials manually. You can choose to create a token yourself as shown in this guide, or one will be created when you deploy your blueprint.

In this guide, you'll define a robot token with Blueprints, assign it to a function, and deploy the blueprint.

Prerequisites:

  • Functions run on Node.js v24.x by default.
  • The latest version of the Sanity CLI is recommended. Run commands with npx sanity@latest.
  • A Sanity project ID where you have permission to create robots and deploy functions. Creating robot tokens requires the sanity-project-tokens permission.
  • @sanity/blueprints v0.12.0 or later, which introduced defineRobotToken.

Robot tokens

Robots are service accounts that provide authentication tokens for automated access. When you define a token in a blueprint:

  • The token is created during deployment with the specified roles. Sanity generates and manages the token value.
  • Functions reference the token with $.resources.ROBOT_NAME.token, where ROBOT_NAME is the name of your robot token resource.
  • The token is injected into your function to be used at function runtime.

These are similar to robot tokens defined in your project or organization settings, but they are managed by the blueprint instead.

Define a robot token

Use the defineRobotToken helper to define a token in your blueprint configuration. Replace <your-project-id> with your project ID.

You can find a list of available roles for your project with the Access API, or in your project's settings at sanity.io/manage.

A complete list of configuration options is available in the reference documentation.

Use the token in a function

Reference the robot token in your function definition:

When configured like this, your function receives the token as part of the context.clientOptions and can be used to configure a Sanity client.

Deploy the blueprint

Deploy the blueprint to create the robot token and assign it to your function:

Define custom roles

This is a paid feature

This feature is available on certain Enterprise plans. Talk to sales to learn more.

You can define custom roles in the blueprint, then use them to define a robot token. This example defines a role, function-user, then defines a robot as a member of that role, and finally assigns that robot token to the function.

Best practices

Apply least privilege

Create custom roles with only the permissions your function needs, as shown in "Define custom roles."

Keep credentials out of source control

The actual token value is managed by Blueprints and never stored in your repository.

Match Node.js versions

Use Node.js v24.x locally to match the Functions runtime and avoid unexpected behavior.

Troubleshooting

Validation error: robotToken must be a string

Set robotToken to a string reference such as $.resources.ROBOT_NAME.token, not an object or function call.

Function token is undefined at runtime

@sanity/blueprints only checks that robotToken is a string. It doesn't check that the reference names a robot token in your blueprint, so a typo in the name isn't caught when the blueprint is validated. If context.clientOptions.token is undefined when your function runs, check that the name in $.resources.ROBOT_NAME.token matches the name of a defineRobotToken resource.

Permission denied at runtime

The robot's roles don't permit the operation. Review memberships.roleNames and ensure the assigned roles grant the necessary permissions.

Next steps

Was this page helpful?