# Authentication and scopes

Create API keys, send them with each request, and give each key only the scopes it needs.

Every request needs an API key in the `Authorization` header:

```http
GET /v1/conversations HTTP/1.1
Host: api.chatwise.so
Authorization: Bearer <your API key>
```

A key belongs to one workspace, so no request names a workspace.

## Create a key

Workspace admins create keys in the dashboard, under **Settings → Developer → API Keys** ([open it](https://app.chatwise.so/settings/api-keys)). Choose a name, the permissions and when it expires: in at most 365 days, or never.

The key is shown once. Chatwise stores only a hash of it and can't show it again.

Keep keys on your server. Don't put one in a web page, a mobile app or a repository: anyone who has it can use it.

## Scopes

A key can do only what its scopes allow.

| Scope | In the dashboard | What it allows |
| --- | --- | --- |
| `conversations:read` | Read conversations | List, find and read conversations. |
| `messages:read` | Read messages | List, search and read messages. Also needed for anything that would give the key a message by webhook, see below. |
| `messages:send` | Send messages | Send approved WhatsApp templates. |
| `templates:read` | Read templates | List and read WhatsApp templates. |
| `templates:write` | Create templates | Create WhatsApp templates and submit them to Meta. |
| `webhooks:read` | Read webhook endpoints | List webhook endpoints, their deliveries and the event types. |
| `webhooks:write` | Manage webhook endpoints | Create, change, test and delete webhook endpoints, rotate their secrets and redeliver. |

One scope doesn't include another: `webhooks:write` alone can't list endpoints. Each page of the [API reference](https://chatwise.so/docs/api.md) names the scope its route needs.

You can't change a key's scopes. Create another key and revoke the old one.

### When `messages:read` is needed too

Webhooks carry messages, so a key must not get a message by webhook that it couldn't read through the API. With `message.*` events, these need `messages:read` as well:

| What you do | Scope | Also |
| --- | --- | --- |
| [Create an endpoint](https://chatwise.so/docs/api/create-webhook-endpoint.md) with `message.*` events | `webhooks:write` | `messages:read` |
| [Change an endpoint](https://chatwise.so/docs/api/update-webhook-endpoint.md) that has them, or add them | `webhooks:write` | `messages:read` |
| [Rotate the secret](https://chatwise.so/docs/api/rotate-webhook-secret.md) of an endpoint that has them | `webhooks:write` | `messages:read` |
| [Read a delivery](https://chatwise.so/docs/api/get-webhook-delivery.md) of a `message.*` event | `webhooks:read` | `messages:read` |
| [Redeliver](https://chatwise.so/docs/api/redeliver-webhook-delivery.md) a `message.*` event | `webhooks:write` | `messages:read` |

[Testing](https://chatwise.so/docs/api/test-webhook-endpoint.md) and [deleting](https://chatwise.so/docs/api/delete-webhook-endpoint.md) an endpoint, and [listing its deliveries](https://chatwise.so/docs/api/list-webhook-deliveries.md), don't need it: they return no messages.

## Rotate a key

1. Create a key with the same scopes.
2. Deploy it.
3. Make a request with it.
4. Revoke the old key in the dashboard.

A revoked or expired key stops working right away: it answers `401`.

## When a key is refused

| Status | Message | What to do |
| --- | --- | --- |
| 401 | A valid Bearer API key is required | Send `Authorization: Bearer` and the key. |
| 401 | Invalid or revoked API key | Check the key. It may have been revoked, or have expired. |
| 401 | Invalid API key | Chatwise couldn't check the key. Try again. |
| 403 | API key does not have the required scope: … | Use a key with the scope the message names. |
| 400 | Message events need an API key with the messages:read scope too. | Subscribe with a key that also has `messages:read`. |
| 403 | Changing an endpoint that receives message events needs an API key with the messages:read scope too. | The same, for changing an endpoint or rotating its secret. |
| 403 | Reading a message event's delivery needs an API key with the messages:read scope too. | The same, for reading or redelivering a delivery. |
| 429 | API key rate limit exceeded | [Wait](https://chatwise.so/docs/rate-limits.md), then go on. |
