# Create an API token

`POST /api/v1/workspaces/{workspaceId}/api-tokens`

Creates a personal API token with the given scopes, optionally for one project and with an expiry. The token can never do more than its creator can at the time of each request. The value is in this answer only.

Sign-in: a dashboard session only. API tokens and OAuth sign-ins get `403 token.not_allowed`.

Section: API tokens. Operation id in openapi.json: `create_api_token`.

## Parameters

| Name | In | Required | Type | Description |
|---|---|---|---|---|
| `workspaceId` | path | yes | string (uuid) | The workspace's id. |
| `Idempotency-Key` | header | no | string | Any 1–255 characters, for example a UUID per intended action. A retry with the same key and the same request answers the stored response and creates nothing again; the same key with another request answers `422 request.idempotency_key_reused`. Keys are kept for 24 hours. |

## Request body

Required: yes.

`application/json`: ApiTokenInput

| Field | Type | Required | Description |
|---|---|---|---|
| `expiresAt` | string \| null | no | RFC 3339, in the future; absent: the token does not expire. |
| `name` | string | yes | 1–100 characters, for telling tokens apart. |
| `projectId` | string (uuid) \| null | no | Confines the token to one project of the workspace; absent: every project its creator sees. |
| `scopes` | array of Scope | yes | At least one; each at most once. |

## Responses

Errors are `application/problem+json` (`Problem`); branch on `type`.

### 201

The token's value and its record.

`application/json`: ApiTokenCreated

| Field | Type | Required | Description |
|---|---|---|---|
| `apiToken` | ApiTokenView | yes |  |
| `token` | string | yes | `mly_…`; send it as `Authorization: Bearer <token>`. |

### 400

Bad Request.

- `request.malformed`: the body or a parameter is not of the expected shape
- `request.idempotency_key_invalid`: the key is empty or longer than 255 characters
- `auth.credentials_conflict`: the request carries both a session cookie and a token

### 401

Unauthorized.

- `auth.unauthenticated`, `auth.session_expired`: no session, or it ended
- `token.invalid`: the token is malformed, unknown, revoked or expired, or its creator left the workspace

### 403

Forbidden.

- `auth.forbidden`: your role is too low for this
- `token.not_allowed`: this operation needs a signed-in session; tokens cannot call it
- `request.origin_mismatch`, `request.header_required`: a changing request with a session comes from another site or lacks `X-Monitorly-Request: 1`

### 404

Not Found.

- `request.not_found`: it does not exist, or you cannot see it

### 409

Conflict.

- `quota.api_tokens_reached`: the workspace has 20 active API tokens

### 413

Payload Too Large.

- `request.too_large`: the body is larger than 256 KiB

### 422

Unprocessable Entity.

- `field.required`, `field.too_long`, `field.invalid`: a field breaks a rule; `errors` points at it

### 429

Too Many Requests.

- `session.rate_limited`: more than 5 changes a second from your sessions

Schemas named above are listed with their fields in [the schema list](https://docs.monitorly.cloud/api/reference/schemas.md).
