> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Define a robot token with Blueprints

Blueprints let you create robot tokens alongside other resources for use in the blueprint.

[Robot tokens](https://www.sanity.io/docs/content-lake/http-auth) let your code make authenticated requests to Sanity. When created with Blueprints, they’re often used to provide explicit access in [Functions](https://www.sanity.io/docs/functions/robot-tokens-with-functions).

In this guide, you’ll define a robot token resource with Blueprints and deploy the blueprint to Sanity.

Prerequisites:

- The latest version of the `sanity` CLI is recommended to interact with Blueprints. You can always run the latest CLI commands with `npx sanity@latest`.
- An existing project and [a role with permission](https://www.sanity.io/docs/content-lake/roles-concepts) to edit robot tokens (requires the `sanity-project-tokens` permission).
- The `defineRobotToken` helper requires `@sanity/blueprints` v0.12.0 or later. We recommend using the latest version of the library.

## Initialize a new blueprint

If you already have a blueprint, skip to the next section. To initialize a blueprint and a project-scoped stack in the current directory, run:

**npm**

```shell
npx sanity@latest blueprints init . --type ts --project-id YOUR_PROJECT_ID --stack-name production
```

**pnpm**

```shell
pnpm dlx sanity@latest blueprints init . --type ts --project-id YOUR_PROJECT_ID --stack-name production
```

**yarn**

```shell
yarn dlx sanity@latest blueprints init . --type ts --project-id YOUR_PROJECT_ID --stack-name production
```

**bun**

```shell
bunx sanity@latest blueprints init . --type ts --project-id YOUR_PROJECT_ID --stack-name production
```

The command creates a `sanity.blueprint.ts` file in the current directory and a new stack in your project:

- `YOUR_PROJECT_ID`: Your project ID. To list your projects and their IDs, run `npx sanity@latest projects list`.
- `--type ts`: The blueprint file format. Use `js` or `json` for a JavaScript or JSON file.
- `--stack-name production`: The name of the new stack. Use any name.



## Configure the robot token

Use the `defineRobotToken` helper to define a robot token resource. Replace `YOUR_PROJECT_ID` with your project ID:

**sanity.blueprint.ts**

```typescript
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](https://reference.sanity.io/_sanity/blueprints/index/defineRobotToken/).

## 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.

**sanity.blueprint.ts**

```typescript
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:

**npm**

```shell
npx sanity@latest blueprints deploy
```

**pnpm**

```shell
pnpm dlx sanity@latest blueprints deploy
```

**yarn**

```shell
yarn dlx sanity@latest blueprints deploy
```

**bun**

```shell
bunx sanity@latest 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.

## Destroy a deployed blueprint

Sometimes you want to remove a deployed resource so it won't run anymore or affect any future usage quotas. 

To remove a resources created with a blueprint, you need to either:

1. Remove the definition from the blueprint, and run the `deploy` command again.
2. Destroy the blueprint with the `destroy` command.

The `blueprints destroy` command removes, or undeploys*,* the blueprint and all of its resources from Sanity's infrastructure. It does not remove your local files. 

**npm**

```shell
npx sanity blueprints destroy
```

**pnpm**

```shell
pnpm dlx sanity blueprints destroy
```

**yarn**

```shell
yarn dlx sanity blueprints destroy
```

**bun**

```shell
bunx sanity blueprints destroy
```

To remove the resource from the blueprint locally, you can remove it from the `resources` array in the `sanity.blueprint.ts` file, then delete any associated files.

### Redeploying 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:

**npm**

```shell
npx sanity blueprints init
```

**pnpm**

```shell
pnpm dlx sanity blueprints init
```

**yarn**

```shell
yarn dlx sanity blueprints init
```

**bun**

```shell
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.



## Learn more about robot tokens

#### Explore robot tokens

[Use robot tokens with Functions](https://www.sanity.io/docs/functions/robot-tokens-with-functions)
Learn how to authenticate Sanity Functions using robot tokens defined with Blueprints.

[Authentication and tokens](https://www.sanity.io/docs/content-lake/http-auth)
How to create tokens and make authenticated requests.

[Roles](https://www.sanity.io/docs/user-guides/roles)
Sanity enforces user access control with roles. Roles help control resource access to datasets and documents.

