Define a robot token with Blueprints
Blueprints allow you to create robot tokens alongside other resources for use in the blueprint.
Robot tokens let your code make authenticated requests to Sanity. When created with Blueprints, they’re often used to provide explicit access in Functions.
In this guide, you’ll define a robot token resource with Blueprints and deploy the blueprint to Sanity.
Experimental feature
This article describes an experimental Sanity feature. The APIs and behavior may change at any time. Follow the changelog for updates.
Prerequisites:
- The latest version of
sanityCLI is recommended to interact with Blueprints. You can always run the latest CLI commands withnpx sanity@latest. - An existing project and a role with permission to edit robot tokens (requires the
sanity-project-tokenspermission). - Robot token support was first introduced in
@sanity/blueprintsv0.11.0. We recommend using the latest version of the library.
Initialize a new blueprint
To initialize a blueprint in the current directory, run the command below. Replace the project ID with your own. Skip to the next section if you already have a blueprint set up.
npx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
pnpm dlx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
yarn dlx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
bunx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
Configure the robot token
Use the defineRobotToken helper to define a robot token resource.
import { defineBlueprint, defineRobotToken } from "@sanity/blueprints"
export default defineBlueprint({
resources: [
defineRobotToken({
name: 'editor-robot',
memberships: [
{
resourceType: 'project',
resourceId: 'your-project-id',
roleNames: ['editor']
}
]
})
],
})A full list of available configuration options is available in the reference documentation.
Use the token in a function
Reference the token in your function definition to make it available to your custom functions. This makes the function use the custom-scoped robot token instead of the default token with broad read/write permissions.
import {defineBlueprint, defineRobotToken, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineRobotToken({
name: 'editor-robot',
memberships: [
{
resourceType: 'project',
resourceId: 'your-project-id',
roleNames: ['editor']
}
]
}),
defineDocumentFunction({
name: 'my-function',
// Replace `editor-robot` with your token name
robotToken: '$.resources.editor-robot.token',
// ... rest of config
}),
]
})Deploy the blueprint
Next, deploy the blueprint.
npx sanity blueprints deploy
pnpm dlx sanity blueprints deploy
yarn dlx sanity blueprints deploy
bunx sanity blueprints deploy
Once the deployment finishes, the robot token is active.
If you need to make changes, update the blueprint file (sanity.blueprint.ts) and run the deploy command again.
Remove the robot token resource
To remove a resources created with a blueprint, you need to either:
- Remove the definition from the blueprint, and run the
deploycommand again. - Destroy the blueprint with the
destroycommand.
Destroy will “undeploy” the blueprint and remove the stack, leaving only your local files.
Redeploy a destroyed blueprint
When you run blueprints destroy, it's as if you never used blueprints init during setup. The only difference is you still have all the files in your directory. To use this blueprint again and redeploy it, you'll need to let Sanity know about it. You can do this by running init again:
npx sanity blueprints init
pnpm dlx sanity blueprints init
yarn dlx sanity blueprints init
bunx sanity blueprints init
This launches an editing interface that lets you reconfigure the blueprint, if needed, and it reconnects the blueprint to Sanity. Now you can add more functions or redeploy. Keep in mind that any environment variables added before destroying the blueprint will not carry over.