# Core API authentication

The Core API token: how an owner creates, rotates and revokes one, where to keep it, when it expires, and what each of its thirteen scopes allows.

Source: https://docs.omazy.ai/reference/core-api/authentication/

Every Core API request carries one header:

```http
Authorization: Bearer cai_<prefix>.<rest>
```

## The token

A token looks like `cai_` followed by 12 hexadecimal characters, a dot, and 48
more. The part before the dot is a public prefix that identifies the token. The
part after it is what proves you hold it.

A token belongs to one workspace. The workspace is never sent in a request: it
is read from the token, so a call cannot reach another workspace by asking for
it. An id from somewhere else simply answers `404`.

## Getting one

A workspace owner creates a token. Only an owner can: a token spends the
workspace's budget, and whoever creates one chooses its scopes.

In the console, open **Settings**, then **API tokens**, and choose **New
token**. Give it a name, pick its scopes and a lifetime. The token is shown
once, with a `whoami` request to check it. The same screen rotates a token,
sets where its events go and revokes it.

The console API does the same, signed in as the owner (see
[Authentication](/reference/api/authentication/) for that sign-in). Use it
when you create tokens from a script.

```
POST https://mw.omazy.ai/api/v1/workspace/{handle}/installs
```

```json
{
  "app_slug": "backoffice",
  "name": "Back office server",
  "scopes": ["ai:complete", "kb:search"],
  "expires_in_days": 90
}
```

| Field | Type | |
|---|---|---|
| `app_slug` | string | Required. Your name for what holds the token: 2 to 40 characters of `a-z`, `0-9` and hyphen. One live token per slug |
| `name` | string | A label for people. At most 120 characters |
| `scopes` | string[] | Required. At least one, from the [list below](#scopes) |
| `expires_in_days` | number | The token's lifetime, 1 to 366 days. Leave it out or send `0` for a token that never expires |

```json
{
  "success": true,
  "data": {
    "token": "cai_<prefix>.<rest>",
    "install": {
      "id": "0d6c3a7e-3f3f-4b1b-9d0c-1f5a2f1c9a11",
      "app_slug": "backoffice",
      "name": "Back office server",
      "scopes": ["ai:complete", "kb:search"],
      "state": "active",
      "token_prefix": "cai_<prefix>",
      "expires_at": "2027-02-17T12:14:11Z",
      "last_used_at": null
    }
  }
}
```

The full token is in this response and nowhere else. Omazy keeps only a hash
of it, so nobody can read it back to you later, including us. If you lose it,
rotate.

A second create with the same `app_slug` answers `409 CONFLICT`. That is
deliberate: creating it again would replace the token a running server is
using. Rotate instead.

| Route | Does |
|---|---|
| `GET /workspace/{handle}/installs` | Lists the workspace's tokens with their scopes, state, expiry and last use. Never a token or a hash |
| `GET /workspace/{handle}/installs/scopes` | Lists every scope and what it grants |
| `POST /workspace/{handle}/installs` | Creates a token |
| `POST /workspace/{handle}/installs/{id}/rotate` | Replaces a token, keeping the old one for an overlap |
| `PATCH /workspace/{handle}/installs/{id}` | Sets the token's `events_url`, where [knowledge events](/reference/core-api/knowledge-documents/#events) are sent. An empty string turns them off |
| `DELETE /workspace/{handle}/installs/{id}` | Revokes a token |

## Expiry and rotation

A token with an expiry stops working at that moment with `401 UNAUTHORIZED`
and the message `core token expired; rotate the install to get a new one`.
Every response to a token that has a deadline says when:

```http
X-Omazy-Token-Expires-At: 2027-02-17T12:14:11Z
```

Rotation replaces a token without an outage. The old token keeps working for
an overlap window, so you can put the new one in your server's configuration
and deploy at your own pace.

```
POST https://mw.omazy.ai/api/v1/workspace/{handle}/installs/{id}/rotate
```

```json
{ "overlap_hours": 24 }
```

| Field | Type | |
|---|---|---|
| `overlap_hours` | number | How long the old token keeps working. Default `24`, at most `168`. `0` stops it at once |
| `expires_in_days` | number | The new token's lifetime. Left out, a token that had an expiry gets 90 days and one that had none still has none. `0` removes the expiry |

The response has the same shape as a create, with the new token shown once and
a `previous_token` block saying until when the old one is accepted.

While your server is still on the old token, every response tells it so:

```http
X-Omazy-Token-Use: previous
X-Omazy-Token-Expires-At: 2026-11-20T12:14:11Z
```

`GET /whoami` reports the same as `"token": "previous"`. Alert on it, and you
find the server that missed the new token before the overlap ends.

Only one old token is ever accepted. Rotating twice inside the window stops
the oldest one at once.

## Keeping it

:::caution[This token is for a server]
Never ship it in a browser bundle or a mobile app. Anyone who can read your
JavaScript or unpack your app can read the token, and then they can spend your
workspace's budget and reach whatever its scopes allow. A browser or app should
call your own server, and your server should call Omazy.
:::

Keep it where your other server credentials live: an environment variable or a
secrets manager, not the repository and not a log line.

If a token leaks, rotate it with `"overlap_hours": 0`. The leaked token stops
on its next request and you hold the new one. To turn a token off for good,
revoke it: `DELETE /workspace/{handle}/installs/{id}`.

## Scopes

A scope is one permission. A token carries a list of them, fixed when it is
issued, and each route asks for exactly one. One key per door: a token that may
search your knowledge base cannot run up a bill on open-ended completions, and
nothing short of `voice:calls` can make a phone ring.

| Scope | Allows |
|---|---|
| `ai:complete` | `POST /ai/complete`, a model call through the gateway |
| `kb:search` | `POST /knowledge/search`, passages from the app's knowledge base |
| `kb:read` | `GET /knowledge/documents`, `GET /knowledge/sources` and `GET /knowledge/export`: list and read documents, and export what changed |
| `kb:write` | `POST`, `PATCH` and `DELETE` on `/knowledge/documents`, `POST /knowledge/import` and `POST /knowledge/sources`: write the knowledge base |
| `agent:run` | `POST /agent/turn`, one turn of the app's agent |
| `agent:configure` | `GET` and `PATCH /agent/settings`: the agent's model, reply cap, temperature and loop switches |
| `voice:speech` | `POST /voice/speak` and `POST /voice/transcribe` |
| `voice:calls` | `POST /voice/calls` and `GET /voice/calls/{ref}`, outbound phone calls |
| `voice:tasks` | `POST /voice/jobs` and `GET /voice/jobs/{id}`, long transcriptions and meeting notes |
| `core:read` | `GET /customers/{id}`, read a customer |
| `memory:write` | `PATCH /customers/{id}`, merge custom fields onto a customer |
| `core:write` | `POST /conversations/{id}/messages`, post a message as the agent |
| `channels:publish` | `POST /channels/linkedin/publish`, publish to a connected page |
| `sessions:credentials` | `PUT` and `DELETE /sessions/{sid}/mcp-credentials/{server}`, bind a signed-in user's token to their chat |

`GET /whoami` needs no scope. It returns the scopes a token holds, which is the
quickest way to find out why a call was refused.

Scopes are fixed when a token is created, and rotating keeps them. To change
them, revoke the token and create it again under the same `app_slug` with the
new list.

## When a request is refused

Two different failures, with two different fixes.

**`401 UNAUTHORIZED`** means we do not know who is calling. The header is
missing, the token is malformed, it was revoked, it expired, or it is wrong.
The response carries `WWW-Authenticate: Bearer`.

```json
{
  "success": false,
  "error": { "code": "UNAUTHORIZED", "message": "missing core token" }
}
```

The message says which: `missing core token`, `malformed core token`,
`unknown or revoked install`, `invalid core token`, or `core token expired;
rotate the install to get a new one`. Check that the header
reads `Authorization: Bearer` followed by the whole token, dot included, and
that the value did not pick up a trailing newline on its way into your
environment.

**`403 FORBIDDEN`** means we know exactly who is calling and the token lacks the
scope the route needs. The message names the scope.

```json
{
  "success": false,
  "error": { "code": "FORBIDDEN", "message": "missing scope: ai:complete" }
}
```

Trying again does not help. The token needs that scope added.

Branch on `code`, never on `message`. The full list of codes is in
[Errors and limits](/reference/core-api/errors-and-limits/).
