> ## Documentation Index
> Fetch the complete documentation index at: https://docs.akua.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Create an API token

Creates an API token that acts as you, optionally limited to one Workspace and to a ceiling of IAM roles. Requires a signed-in session; an API token cannot create another. The full plaintext token is returned once and never shown again - store it securely.

`POST https://api.akua.dev/v1/api_tokens`

## Authorizations

- `Authorization: Bearer <token>` (BearerAuth): Bearer token for the authenticated caller.

## Body

Required.

`application/json`: `CreateApiTokenRequest`

- `name` (string, required): Human-readable name for the token. Required string length: `1 - 100`. Example: `"My CLI token"`.
- `expires_at` (integer | null): Unix timestamp (seconds) when the token expires. Omit or pass null for no expiry. Required range: `x >= 0`. Example: `1742169600`.
- `workspace_id` (string | null): The only Workspace the token may act in. Omitted or null: every Workspace the person may use. Required string length: `2 - 53`. Example: `"ws_j572abc123def4"`.
- `roles` (string[] | null): A ceiling of IAM roles: the token may do only what one of these roles includes, and only what the person may. Omitted or null: the rights of the person. Required array length: `1 - 16 elements`. Example: `["roles/viewer"]`.

## Response

### 201: Token created. The `token` field is shown only once.

`application/json`: `CreatedApiToken`

- `id` (string, required): API token ID. Required string length: `3 - 54`. Example: `"tok_3k9x2mfq8a1b7c4d5e6f"`.
- `name` (string, required): Required string length: `1 - 100`. Example: `"My CLI token"`.
- `prefix` (string, required): The first 12 characters of the token, for recognizing it. Never the secret. Example: `"sk_akua_a3B2"`.
- `workspace_id` (string | null, required): The only Workspace the token may act in. Omitted or null: every Workspace the person may use. Required string length: `2 - 53`. Example: `"ws_j572abc123def4"`.
- `roles` (string[] | null, required): A ceiling of IAM roles: the token may do only what one of these roles includes, and only what the person may. Omitted or null: the rights of the person. Required array length: `1 - 16 elements`. Example: `["roles/viewer"]`.
- `created_at` (integer, required): Unix timestamp (seconds). Required range: `x >= 0`.
- `last_used_at` (integer | null, required): Unix timestamp (seconds) of last use, or null if never used. Required range: `x >= 0`.
- `expires_at` (integer | null, required): Unix timestamp (seconds) when the token expires, or null if never. Required range: `x >= 0`.
- `token` (string, required): The full token. This is shown only once - store it securely. Example: `"sk_akua_a3B2c4D5e6F7g8H9i0J1k2L3m4N5o6P73jZRME"`.

### 400: The request is malformed.

`application/json`: `ApiBadRequest`

- `_tag` (enum<string>, required): Available options: `"Validation"`.
- `success` (enum<boolean>, required): Available options: `false`.
- `errors` (object[], required): Minimum array length: `1 element`.
  - `code` (integer, required): Machine-readable error code. Required range: `-9007199254740991 <= x <= 9007199254740991`. Example: `7002`.
  - `message` (string, required): Human-readable error message. Example: `"Resource not found"`.
  - `path` (string[]): Field path that caused the error, when applicable. Example: `["body","name"]`.
  - `metadata` (record<string, string>)
- `result` (object, required)

### 401: Missing, invalid or expired credential.

`application/json`: `ApiUnauthorized`

- `_tag` (enum<string>, required): Available options: `"Unauthorized"`.
- `success` (enum<boolean>, required): Available options: `false`.
- `errors` (object[], required): Minimum array length: `1 element`.
  - `code` (integer, required): Machine-readable error code. Required range: `-9007199254740991 <= x <= 9007199254740991`. Example: `7002`.
  - `message` (string, required): Human-readable error message. Example: `"Resource not found"`.
  - `path` (string[]): Field path that caused the error, when applicable. Example: `["body","name"]`.
  - `metadata` (record<string, string>)
- `result` (object, required)

### 403: The credential may not act in this workspace.

`application/json`: `ApiForbidden`

- `_tag` (enum<string>, required): Available options: `"Forbidden"`.
- `success` (enum<boolean>, required): Available options: `false`.
- `errors` (object[], required): Minimum array length: `1 element`.
  - `code` (integer, required): Machine-readable error code. Required range: `-9007199254740991 <= x <= 9007199254740991`. Example: `7002`.
  - `message` (string, required): Human-readable error message. Example: `"Resource not found"`.
  - `path` (string[]): Field path that caused the error, when applicable. Example: `["body","name"]`.
  - `metadata` (record<string, string>)
- `result` (object, required)

### 422: The request is well-formed but cannot be processed.

`application/json`: `ApiUnprocessable`

- `_tag` (enum<string>, required): Available options: `"Unprocessable"`.
- `success` (enum<boolean>, required): Available options: `false`.
- `errors` (object[], required): Minimum array length: `1 element`.
  - `code` (integer, required): Machine-readable error code. Required range: `-9007199254740991 <= x <= 9007199254740991`. Example: `7002`.
  - `message` (string, required): Human-readable error message. Example: `"Resource not found"`.
  - `path` (string[]): Field path that caused the error, when applicable. Example: `["body","name"]`.
  - `metadata` (record<string, string>)
- `result` (object, required)

## Example request

```bash
curl --request POST \
  --url 'https://api.akua.dev/v1/api_tokens' \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "name": "My CLI token",
    "expires_at": 1742169600,
    "workspace_id": "ws_j572abc123def4",
    "roles": [
      "roles/viewer"
    ]
  }'
```

## Example response

`201` `application/json`

```json
{
  "id": "tok_3k9x2mfq8a1b7c4d5e6f",
  "name": "My CLI token",
  "prefix": "sk_akua_a3B2",
  "workspace_id": "ws_j572abc123def4",
  "roles": [
    "roles/viewer"
  ],
  "created_at": 123,
  "last_used_at": 123,
  "expires_at": 123,
  "token": "sk_akua_a3B2c4D5e6F7g8H9i0J1k2L3m4N5o6P73jZRME"
}
```
