> ## Documentation Index
> Fetch the complete documentation index at: https://docs.routa.chat/llms.txt
> Use this file to discover all available pages before exploring further.

# API keys

> How to authenticate your requests to the Routa API, which scopes each route requires and how to keep your keys safe.

> [!TIP]
>
> The API key is the access credential of your project. Without a valid key, the API accepts no request.

Every request to the public API (`/v1`) is authenticated with an API key sent in the `Authorization` header, in the `Bearer` scheme:

```http theme={null}
Authorization: Bearer rt_live_...
```

Each key belongs to **a single project**. There is no "all projects" key: everything the key reads or writes stays inside that project. To access another project, use its key.

## Key format

```text theme={null}
rt_{live|test}_{secret}
```

| Prefix | Environment |
| - | - |
| `rt_live_` | Live project. |
| `rt_test_` | Test project. See [Environments](https://docs.routa.chat/en/environments.md). |

> [!NOTE]
>
> A partner key, in the format `rt_partner_…`, is **not** a project key. It only works on the `/v1/partner/*` routes and never sends messages. On any project route it gets `401 api_key_invalid`, and a project key does not work on partner routes either. See [Partner keys](https://docs.routa.chat/en/partners/partner-keys.md).

The key is generated with a cryptographic generator and **shown only once**, when it is created. Routa stores only a hash of it, so it cannot be recovered later. If you lose the key, create another one and revoke the old one.

## What you can do with your keys

* Create one or more keys per project, each with the minimum scopes the integration needs.
* Set an expiry date and an IP allowlist for the key.
* Rotate a key without downtime: the new key is created and the old one stays valid until you revoke it.
* Revoke a compromised key. Revocation takes effect in about a second.
* See when each key was last used and spot the idle ones.

> [!NOTE]
>
> Creating, rotating and revoking keys are dashboard actions, on the **API keys** page, in the **Developer** group of the sidebar, authenticated by a user session. They do not use an API key.

## Scopes

Each key carries a closed set of **scopes**. A new key starts with the minimum you asked for, never with full access. If a route requires a scope the key lacks, the API responds `403` with the `insufficient_scope` code.

| Scope | Allows |
| - | - |
| `messages:write` | Send messages and mark inbound messages as read. |
| `messages:read` | Retrieve and list messages. |
| `events:read` | List events (`GET /v1/events`). |
| `webhooks:write` | Manage webhook endpoints, secrets and deliveries. It is the only scope for this resource, reads included. |
| `templates:write` | Create templates and upload template header media. |
| `templates:read` | Retrieve and list templates. |
| `media:write` | Upload or host media. |
| `media:read` | Retrieve a media object. |
| `usage:read` | Query the metered usage of the project. |
| `channels:read`, `channels:write` | Reserved for a future channel route authenticated by API key. No route requires them today. |

### Scope required by each route

| Route | Scope |
| - | - |
| `POST /v1/messages` | `messages:write` |
| `GET /v1/messages`, `GET /v1/messages/{id}` | `messages:read` |
| `POST /v1/messages/{id}/read` | `messages:write` |
| `GET /v1/events` | `events:read` |
| `/v1/webhook_endpoints` and sub-routes | `webhooks:write` |
| `POST /v1/templates`, `POST /v1/templates/media` | `templates:write` |
| `GET /v1/templates`, `GET /v1/templates/{id}` | `templates:read` |
| `POST /v1/media` | `media:write` |
| `GET /v1/media/{id}` | `media:read` |
| `GET /v1/usage` | `usage:read` |
| `GET /v1/whoami` | Any valid key. |

> [!NOTE]
>
> Each endpoint page in the [API reference](https://docs.routa.chat/en/api-reference/introduction.md) shows the required scope.

## Check a key

`GET /v1/whoami` has no side effects. It returns the organization, the project, the key `id` and the scopes the key has.

```bash cURL theme={null}
curl https://api.routa.chat/v1/whoami \
  -H "Authorization: Bearer $ROUTA_API_KEY"
```

```ts Node.js theme={null}
const identity = await routa.whoami()
console.log(identity.projectId, identity.scopes)
```

```json Response theme={null}
{
  "organization_id": "org_01J8...",
  "project_id": "proj_01J8...",
  "api_key_id": "key_01J8...",
  "scopes": ["messages:read", "messages:write"]
}
```

## Troubleshooting

> [accordion] 401 Unauthorized: what to check
>
> The API responds `401` with `type: "authentication_error"` and the `api_key_invalid` code. Check, in this order:
>
> 1. The header is exactly `Authorization: Bearer <key>`, with the word `Bearer` and one space.
> 2. The whole key was copied, with no spaces or line breaks at the start or end.
> 3. The key was not revoked and has not expired.
> 4. The request comes from an allowed IP, if the key has an IP allowlist.
> 5. The environment variable holding the key is set in the process that makes the call.
>
> To test, call the identity endpoint:
>
> ```bash theme={null}
> curl -i https://api.routa.chat/v1/whoami \
>   -H "Authorization: Bearer $ROUTA_API_KEY"
> ```

> [accordion] 403 insufficient\_scope: missing scope
>
> The key is valid but lacks the scope the route requires. The error message says which one. The scopes of a key do not change after it is created: create a new key with the scope you need, swap it into your integration and revoke the old one.

> [accordion] I get 404 for a resource that exists
>
> A resource of another project responds `404`, never `403`, so its existence is not revealed. Use `GET /v1/whoami` to confirm that the key belongs to the right project.

> [accordion] Live or test key: which one am I using?
>
> Look at the prefix. `rt_live_` is live and `rt_test_` is test. See [Environments](https://docs.routa.chat/en/environments.md).

## Security best practices

> [card] Protect your keys
>
> * Read the key from an environment variable or a secrets manager.
> * Never publish the key in repositories, front ends, mobile apps or logs.
> * Use one key per integration, with the minimum scopes.
> * Rotate keys periodically and revoke any exposed key right away.

The SDK does not read environment variables on its own. Pass the key explicitly:

```ts theme={null}
import { Routa } from '@routa-chat/sdk'

const apiKey = process.env['ROUTA_API_KEY']
if (!apiKey) {
  throw new Error('ROUTA_API_KEY is not set')
}

const routa = new Routa({ apiKey })
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.