# Create an API token for scripts, MCP clients, or agents.

Issuance is atomic. Retry unchanged settings with the same Idempotency-Key after an uncertain outcome. Replay confirms the existing credential without returning its one-time secret; deliberately rotat

Canonical: https://commentary.dev/docs/reference/api/create-api-token

`POST /api/v1/tokens`

Issuance is atomic. Retry unchanged settings with the same Idempotency-Key after an uncertain outcome. Replay confirms the existing credential without returning its one-time secret; deliberately rotate it in Account Developer access for recovery.

## Access

Use a credential with the authentication scheme documented in the API contract.

Provider access, workspace membership, and agent grants are enforced separately. A token does not expand access to a repository or resource. [Set up developer access](/docs/build/developer-access).

## Parameters

| Name | Location | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `Idempotency-Key` | header | string | No | Stable key for one issue. Omission preserves legacy non-idempotent requests. |

## Request body

Required. Supported content types: `application/json`.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `label` | string | No |  |
| `scopes` | string or array of [ExternalScope](/docs/reference/schemas/external-scope) | No |  |
| `target` | [TokenTarget](/docs/reference/schemas/token-target) | No |  |
| `expiresAt` | string or null | No | Format: date-time. |

## Call the endpoint

Replace path placeholders and your credential. For body operations, create `request.json` using the required fields above; this command does not invent resource identifiers or consent.

```bash
curl --request POST \
  'https://commentary.dev/api/v1/tokens' \
  --header 'Authorization: Bearer <your-token>' \
  --header 'Content-Type: application/json' \
  --data-binary @request.json
```

## Responses

| Status | Meaning | Payload |
| --- | --- | --- |
| `200` | Successful response. | object |
| `400` | The request was invalid. | [ExternalError](/docs/reference/schemas/external-error) |
| `401` | Authentication is required. | [ExternalError](/docs/reference/schemas/external-error) |
| `403` | The token or session does not have permission for this resource. | [ExternalError](/docs/reference/schemas/external-error) |
| `404` | The resource was not found. | [ExternalError](/docs/reference/schemas/external-error) |
| `409` | The issuance key was reused with different settings or the connected account changed. | [ExternalError](/docs/reference/schemas/external-error) |
| `429` | The request was rate limited. | [ExternalError](/docs/reference/schemas/external-error) |

### Successful response fields

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `ok` | `true` | Yes |  |
| `id` | string | Yes |  |
| `grantId` | string | Yes |  |
| `replayed` | boolean | No |  |
| `token` | string | No | One-time secret, present only for the original successful issue. |
| `tokenHint` | string | Yes |  |

## Recover from errors

Use the returned status and error body. Correct invalid fields before retrying; reconnect or narrow the target for authentication and permission errors. For conditional writes, reload the latest version before preparing a new request. Respect rate-limit headers and retry only when the operation’s idempotency contract permits it.

## Contract and related guidance

Operation ID: `createApiToken`. [Download the OpenAPI contract](/docs/reference/openapi.json) for complete constraints and code generation. [Browse endpoints](/docs/reference/api-operations).
