# Mint an API key

POST

/v1/tokens

```sh
curl --request POST \
  --url https://example.com/v1/tokens \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{ "for": { "kind": "user", "id": "example" }, "role": "admin", "capabilities": [ "example" ], "label": "example", "expiresAt": "2026-04-15T12:00:00Z", "projectId": "2489E9AD-2EE2-8E00-8EC9-32D5F69181C0", "readOnly": true }'
```

An API key acts for one principal (`for`: a person or a service account; default the caller), with that principal’s grants. Its `role` is a ceiling under them and its `projectId` a limit. Returns the plaintext token exactly once. A person or a service account’s key mints its own keys; only a tenant admin mints for someone else, or an `admin` key. A caller can only grant capabilities it holds, and a key limited to a project mints only keys limited to it. A key that expires mints only keys that expire no later than it, and a session opened by token sign-in mints as the key it was opened with. A key minted with a key (`mintedWith`: the caller’s, or the one a token sign-in session was opened with) stops when that key is revoked. Only mounted when the deployment supplies a `TokenAdmin`.

## Authorizations

- **[bearerAuth](https://docs.kindgi.com/v0.8.1/reference/api.md#bearerauth)**

## Parameters

### Header Parameters

**Idempotency-Key**

string

\>= 1 characters

Caller-supplied idempotency key, scoped to the caller: the same key from someone else in the workspace is their own request (from 0.1.5). Retries with the same key return the original response byte-identical (per `docs/API-ROUTE-CONVENTIONS.md` §3.1). A retry sent while the first request still runs is answered `409 idempotency-key-in-flight` with `Retry-After`, on a runtime whose store holds keys (from 0.1.5); retry after it, and you get the first request’s answer. An answer that carries a secret (a new API key or public run token, a session token, a generated signing secret) isn’t kept: a retry gets `409 idempotency-key-replay-withheld`, with the first request’s status and when it succeeded (from 0.1.5). A request whose key store can’t be read before it runs is answered `503 idempotency-store-unavailable` with `Retry-After`: nothing ran, so retry with the same key (from 0.3.0).

## Request Body

application/json

A new API key. `for` is whom it acts for: the caller by default; only a tenant admin mints for someone else.

object

**for**

Whom an API key acts for: a person, or a service account.

object

**kind**

required

string

Allowed values: user service-account

**id**

required

The user id, or the service account id.

string

\>= 1 characters

**role**

The most the key may do, under its principal’s grants: an `admin` key may administer the workspace when its principal is a tenant admin; a `member` key takes no admin action on the workspace, whoever it’s for; below it, its principal’s roles hold (a project admin’s member key administers that project). Default `member`; `admin` needs a tenant admin minting it.

string

Allowed values: admin member

**capabilities**

Framework capabilities the key carries (`env:write`, `secrets:write`, …). A caller can only grant capabilities it holds. Default none.

Array\<string>

**label**

Optional human-readable label.

string

**expiresAt**

ISO 8601 timestamp. Absent: the key never expires. When the minting key expires (or the key a token sign-in session was opened with), it’s required and no later than that key’s (`token-expiry-exceeds-minter`).

string format: date-time

**projectId**

Limit the key to this project: a request naming another project is refused (`key-project-mismatch`). A key limited to a project mints only keys limited to it.

string format: uuid

**readOnly**

`true`: the key only reads. Every request it makes but a `GET` or a `HEAD` is refused (`read-only-key`), apart from an agent’s pin check (`POST /v1/agents/{agentId}/pinned/check`), which writes nothing. It mints and revokes no keys. Default `false`. A runtime whose key store can’t keep the field makes no key: `501 read-only-unsupported`.

boolean

## Responses

### 201

Token minted.

application/json

The new key, plus its secret.

object

**tokenId**

required

string format: uuid

**principal**

Whom an API key acts for: a person, or a service account.

object

**kind**

required

string

Allowed values: user service-account

**id**

required

The user id, or the service account id.

string

\>= 1 characters

**role**

required

The most the key may do, under its principal’s grants: an `admin` key may administer the workspace when its principal is a tenant admin; a `member` key takes no admin action on the workspace, whoever it’s for; below it, its principal’s roles hold (a project admin’s member key administers that project).

string

Allowed values: admin member

**capabilities**

required

Framework capabilities the key carries (`env:write`, `secrets:write`, …). A caller can only grant capabilities it holds.

Array\<string>

**label**

string

**projectId**

The project the key is limited to.

string format: uuid

**readOnly**

Present and `true` when the key only reads.

boolean

**createdBy**

Who minted it: `user:<id>` or `service_account:<id>`.

string

**createdAt**

required

string format: date-time

**expiresAt**

string format: date-time

**revokedAt**

Set once revoked; a revoked key never authenticates again.

string format: date-time

**lastUsedAt**

When the key last authenticated a request (updated at most once a minute).

string format: date-time

**mintedWith**

The key it was minted with: the minter’s key, or the key a token sign-in session was opened with. It stops when that key is revoked. Absent when none was, and for a key minted before keys recorded it.

string format: uuid

**token**

required

Plaintext bearer token. Returned exactly once at mint time.

string

##### Example

```json
{
  "principal": {
    "kind": "user"
  },
  "role": "admin"
}
```

### 400

Malformed request body, or the body’s `projectId` isn’t a project id (a UUID).

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 401

Missing / malformed / expired / revoked bearer token.

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 403

Not a tenant admin where one is needed, or a capability the caller does not hold (`permission-denied`); an `admin` key for a principal that isn’t a tenant admin (`role-exceeds-principal`); a key limited to a project minting for another (`key-project-mismatch`); a key that would outlive the key minting it, with an `expiresAt` later than that key’s or none (`token-expiry-exceeds-minter`, its `details.latestExpiresAt` the latest allowed); a session limited to a project or a role, which mints no keys (`limited-session-cannot-mint`); a key for a person a site signs in, whose access is only the sessions their site gives (`site-person-grants-managed`); a key that only reads, which mints no keys (`read-only-key`); an operator’s capability, which is a system key’s: a system scope (`system:*`) anywhere, and `kindgi:system` on a runtime serving many tenants or on a key limited to a project (`capability-not-mintable`, its `details.capabilities` the ones refused).

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 404

The body’s `projectId` names no project of this workspace (`project-not-found`), or `for` names no person or service account (`principal-not-found`).

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 409

`for` names a project’s own service account, which has no keys (`service-account-project-owned`). Or Idempotency-Key reused with a different body (`idempotency-key-body-mismatch`), or a retry of a request that succeeded: its answer carried a secret, which isn’t kept (`idempotency-key-replay-withheld`, with its `status` and `at`). Or `idempotency-key-in-flight` (with `Retry-After`): a request with the same `Idempotency-Key` still runs, so this one wasn’t run; send it again after `Retry-After` to get that request’s answer.

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

#### Headers

**Retry-After**

integer

\>= 1

Whole seconds to wait before sending the request again, at least 1. Sent with the codes the answer names as sending it, so it may be absent.

### 413

The request body is over the API’s limit (`request-too-large`; `details.maxBytes` says how much).

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 429

`rate-limit-exceeded` with `details.limit: "api-requests"`, on a runtime that sets workspace limits (Kindgi Cloud): this workspace’s limit on API requests a minute is reached, so the request wasn’t run; `Retry-After` says when to send it again.

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

#### Headers

**Retry-After**

integer

\>= 1

Whole seconds to wait before sending the request again, at least 1. Sent with the codes the answer names as sending it, so it may be absent.

### 500

Server error (unmapped domain code or framework crash).

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 501

`readOnly` asked of a runtime whose key store can’t keep it (`read-only-unsupported`): no key was made.

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

### 503

The `Idempotency-Key` store (`idempotency-store-unavailable`) or the authorization store (`authz-backend-unavailable`) couldn’t be reached before the request ran, or the database was too busy for its authorization check (`database-busy`), so nothing was changed; `Retry-After` (whole seconds) says when to try again with the same key. Or `tenant-state-unavailable` (with `Retry-After`), on a runtime serving many workspaces: the workspace’s state couldn’t be read, so nothing was changed.

application/json

object

**error**

required

object

**code**

required

Stable machine-readable discriminant. Values match domain error codes (see `docs/API-ROUTE-CONVENTIONS.md` §4.3).

string

**message**

required

string

**details**

Optional, kind-specific.

object

**_key_**

additional properties

any

**requestId**

required

Server-assigned request id; also echoed via `X-Request-Id` header.

string

##### Examplegenerated

```json
{
  "error": {
    "code": "example",
    "message": "example",
    "details": {},
    "requestId": "example"
  }
}
```

#### Headers

**Retry-After**

integer

\>= 1

Whole seconds to wait before sending the request again, at least 1. Sent with the codes the answer names as sending it, so it may be absent.

## See also

- [`GET /v1/tokens`: List API keys](https://docs.kindgi.com/v0.8.1/reference/api/operations/tokenslist.md)
- [People, API keys and service accounts](https://docs.kindgi.com/v0.8.1/deploy/people-and-keys.md)
- [Kindgi 0.7.0](https://docs.kindgi.com/v0.8.1/releases/0.7.0.md)
- [Error code `authz-backend-unavailable`](https://docs.kindgi.com/v0.8.1/reference/errors.md#authz-backend-unavailable)
- [Error code `capability-not-mintable`](https://docs.kindgi.com/v0.8.1/reference/errors.md#capability-not-mintable)
- [Error code `database-busy`](https://docs.kindgi.com/v0.8.1/reference/errors.md#database-busy)
- [Error code `idempotency-key-body-mismatch`](https://docs.kindgi.com/v0.8.1/reference/errors.md#idempotency-key-body-mismatch)
- [Error code `idempotency-key-in-flight`](https://docs.kindgi.com/v0.8.1/reference/errors.md#idempotency-key-in-flight)
- [Error code `idempotency-key-replay-withheld`](https://docs.kindgi.com/v0.8.1/reference/errors.md#idempotency-key-replay-withheld)
- [Error code `idempotency-store-unavailable`](https://docs.kindgi.com/v0.8.1/reference/errors.md#idempotency-store-unavailable)
- [Error code `key-project-mismatch`](https://docs.kindgi.com/v0.8.1/reference/errors.md#key-project-mismatch)
- [Error code `limited-session-cannot-mint`](https://docs.kindgi.com/v0.8.1/reference/errors.md#limited-session-cannot-mint)
- [Error code `permission-denied`](https://docs.kindgi.com/v0.8.1/reference/errors.md#permission-denied)
- [Error code `principal-not-found`](https://docs.kindgi.com/v0.8.1/reference/errors.md#principal-not-found)
- [Error code `project-not-found`](https://docs.kindgi.com/v0.8.1/reference/errors.md#project-not-found)
- [Error code `rate-limit-exceeded`](https://docs.kindgi.com/v0.8.1/reference/errors.md#rate-limit-exceeded)
- [Error code `read-only-key`](https://docs.kindgi.com/v0.8.1/reference/errors.md#read-only-key)
- [Error code `read-only-unsupported`](https://docs.kindgi.com/v0.8.1/reference/errors.md#read-only-unsupported)
- [Error code `request-too-large`](https://docs.kindgi.com/v0.8.1/reference/errors.md#request-too-large)
- [Error code `role-exceeds-principal`](https://docs.kindgi.com/v0.8.1/reference/errors.md#role-exceeds-principal)
- [Error code `service-account-project-owned`](https://docs.kindgi.com/v0.8.1/reference/errors.md#service-account-project-owned)
- [Error code `site-person-grants-managed`](https://docs.kindgi.com/v0.8.1/reference/errors.md#site-person-grants-managed)
- [Error code `tenant-state-unavailable`](https://docs.kindgi.com/v0.8.1/reference/errors.md#tenant-state-unavailable)
- [Error code `token-expiry-exceeds-minter`](https://docs.kindgi.com/v0.8.1/reference/errors.md#token-expiry-exceeds-minter)
- [Every error code](https://docs.kindgi.com/v0.8.1/reference/errors.md)
