# Authentication (/docs/authentication)

> Authenticate API requests with a secret API key, choose its mode and permission, and rotate or revoke it safely.

Every request to the Druckling API is authenticated with a secret API key. A key belongs to one workspace and decides three things: which workspace the request acts in, whether it runs in live or test mode, and what it's allowed to do.

## Send your API key [#send-your-api-key]

Send the key in the `Authorization` header with the `Bearer` scheme:

```bash tab="Authorization"
curl https://druckling-nine.vercel.app/v1/documents \
  -H "Authorization: Bearer $DRUCKLING_API_KEY"
```

```bash tab="x-api-key"
curl https://druckling-nine.vercel.app/v1/documents \
  -H "x-api-key: $DRUCKLING_API_KEY"
```

Both headers work the same way. If a request has both, the `Authorization: Bearer` header is used. The SDKs send `Authorization: Bearer` and read the key from the `DRUCKLING_API_KEY` environment variable unless you pass one explicitly. See [SDKs](/docs/sdks).

The same keys authenticate the hosted [MCP server](/docs/guides/mcp), with the same permissions.

## Key format [#key-format]

A key is a mode prefix followed by 64 random letters:

| Prefix | Mode |
| --- | --- |
| `dr_live_` | Live: production renders that count toward your plan's quota |
| `dr_test_` | Test: free renders, kept apart from live data |

The prefix is the only thing that decides the mode: there's no header or parameter to switch it. See [Test mode](/docs/concepts/test-mode) for what's separated between the two.

Druckling stores a hash of the key, not the key itself, so the full key is shown only once, when you create it. The dashboard identifies keys by their name and their first characters, for example `dr_live_xKpQ••••`. Keys don't expire; they work until you revoke them.

## Permissions [#permissions]

Each key has one of two permission levels. Pick **Render only** for services that only render documents, and **Full access** only where you need templates, webhooks or the embedded editor.

| | Render only | Full access |
| --- | --- | --- |
| Scopes | `documents:create`, `documents:read` | `documents:create`, `documents:read`, `templates:read`, `templates:write`, `webhooks:manage` |
| [Documents](/docs/api-reference/documents/create): render, retrieve, list | Yes | Yes |
| [Batches](/docs/api-reference/batches/create): start, retrieve, list, list documents, cancel | Yes | Yes |
| [Share links](/docs/api-reference/share-links/create): create, list, revoke | Yes | Yes |
| [Templates](/docs/api-reference/templates/list): list, retrieve with JSON Schema | No | Yes |
| [Webhook endpoints](/docs/api-reference/webhooks/create): create, list, retrieve, delete | No | Yes |
| [Embedded editor sessions](/docs/api-reference/embedded-editor/create): create, revoke | No | Yes |
| MCP server: render and read documents | Yes | Yes |
| MCP server: list, read, create, edit and publish templates | No | Yes |

A render-only key can render any published template, as long as you know its slug. It just can't list templates or read their schemas.

## Keys and workspaces [#keys-and-workspaces]

A key works in the workspace it was created in. Everything it creates, such as documents, batches and webhook endpoints, belongs to that workspace, and it can't reach any other. If you belong to several workspaces, create a key in each.

A key isn't tied to the person who created it. It keeps working when that person leaves the workspace.

## Manage keys [#manage-keys]

Keys are managed in the dashboard under **Developers › API keys**. Owners and admins can create and revoke keys. Members can see the list, with each key's name, mode, permission, when it was last used and when it was created, but can't change it.

Keys can't be renamed or edited. To change a key's mode or permission, create a new key and revoke the old one.

### Create a key [#create-a-key]

1. Go to **Developers › API keys** and click **Create API key**.
2. Name the key after the service that uses it, for example `billing-service`. Keep the name to 32 characters or fewer.
3. Choose the mode, **Test** or **Live**, and the permission, **Render only** or **Full access**.
4. Click **Create key**, copy the key and store it in your server's environment or secret manager. You won't be able to see it again.

### Rotate a key [#rotate-a-key]

There's no in-place rotation. Both keys work at the same time, so rotate without downtime:

1. Create a new key with the same mode and permission.
2. Deploy it everywhere the old key is used.
3. Check that the old key's **Last used** time no longer changes.
4. Revoke the old key.

### Revoke a key [#revoke-a-key]

Open the key's actions menu in the list, choose **Revoke key** and confirm. Requests that use it fail with `401` right away. Revoking can't be undone.

## Keep keys secret [#keep-keys-secret]

API keys are server-side secrets:

- Keep them in environment variables or a secret manager, never in source control.
- Never ship a key to a browser or a mobile app. Anyone who has it can act in your workspace: render documents (on your quota, with a live key) and, with full access, read your templates and manage your webhooks and editor sessions.
- To let your users edit templates in your product, create a short-lived [embedded editor session](/docs/guides/embedded-editor) on your server and give its URL to the browser. A session can only work on one template and expires after at most an hour.
- To give someone a rendered document, send them a [share link](/docs/concepts/share-links) or the document's signed `url`. Neither needs a key.

If a key leaks, revoke it, create a replacement, and look through **Developers › Logs** for requests you don't recognize. The Logs page shows the dashboard's current mode, so switch to test mode to see a test key's requests.

## Authentication errors [#authentication-errors]

Authentication errors have the same shape as every other [error](/docs/concepts/errors):

```json
{
  "defined": false,
  "code": "UNAUTHORIZED",
  "status": 401,
  "message": "Invalid or revoked API key",
  "data": { "code": "unauthorized", "field": null }
}
```

| Status | `data.code` | Message | Cause |
| --- | --- | --- | --- |
| 401 | `unauthorized` | Missing API key. Send it as `x-api-key` or `Authorization: Bearer`. | No key in the request. |
| 401 | `unauthorized` | Invalid or revoked API key | The key is mistyped, incomplete or revoked. |
| 403 | `forbidden` | This API key does not have the templates:read permission | A render-only key called `GET /v1/templates` or `GET /v1/templates/{slug}`. |
| 403 | `forbidden` | This API key does not have the webhooks:manage permission | A render-only key listed or retrieved webhook endpoints. |
| 403 | `forbidden` | Only owners and admins can manage webhooks | A render-only key created or deleted a webhook endpoint. |
| 403 | `forbidden` | Creating editor sessions needs a full-access API key (templates:write) | A render-only key created or revoked an embedded editor session. |
| 429 | `rate_limited` | Too many requests for this API key | The key sent too many requests in a short time. See [Plans and limits](/docs/concepts/plans-and-limits). |

Requests that fail with the `401` and `429` errors above aren't recorded under **Developers › Logs**. Requests rejected with `403` are.
