# POST /api/v1/tokens

> Create token

- **Tag:** api-tokens
- **Operation ID:** `create_token`

## Description

Create a new API token. Requires two-factor authentication on the account and a recent verification of both factors in the current session; must be called from the DayZero web app, not with an API token.

## Authentication

Bearer token in `Authorization` header.
Required header: `x-business-id: <business uuid>`.

## Request body

Schema: `APITokenCreate`

- `name` (string · required) — Token name
- `description` (string) — Optional description
- `expires_in_days` (integer) — Expiration in days (1-365, default: 365)
- `business_ids` (array · string) — Limit the token to these businesses (UUIDs you can access). Omit for every business you can reach. A limited token gets 403 `AUTH_070` on any other business, and cannot elevate around it.
- `program_id` (string) — Limit the token to one credit program. Needs a `credit` scope and `business_ids` naming exactly the business that operates the program (`400` `CREDIT_904` otherwise). The token then gets `403` `CREDIT_903` on another program's programs, lines, loans, assessments, imports and events, and its lists show this program only.
- `scopes` (array · string) — Token surface. Omit for full access. `read_only` blocks every mutating endpoint. `credit` limits the token to the partner-credit API; `credit:read` to its GET endpoints; `credit:payments` to POST /api/v1/credit/payments and its settlement/reversal paths, plus GET on a line's payoff-quote and payments. `credit:mapping` to Payliance payer and Brex card mapping, which it may also write on a live business; no other scope grants that.
- `allowed_ips` (array · string) — When set, this token is refused (403 AUTH_028) unless the request comes from one of these IPv4/IPv6 addresses or CIDR ranges, or from a named cloud preset: `preset:google` (Google services, where Apps Script and Google Sheets call from) or `preset:aws:<region>` (Amazon EC2 in that region). Presets are shared by every customer of that cloud, so they are much weaker than your own addresses. Omit or leave empty to allow any IP. Set at minting; mint a new token to change the list.
- `rate_limit_per_minute` (integer) — Requests per minute this token may make (sliding window). Omit for the platform default. Over budget the API answers 429 AUTH_058 and the token's owner is alerted.

## Example request

```json
{
  "allowed_ips": [
    "203.0.113.10",
    "198.51.100.0/24"
  ],
  "description": "Read-only token for the reporting assistant",
  "expires_in_days": 90,
  "name": "Month-end close agent",
  "scopes": [
    "read_only"
  ]
}
```

## Responses

### 201 — Successful Response

Schema: `SuccessEnvelope_APITokenCreateResponse_`

- `success` (boolean)
- `message` (string)
- `code` (string)
- `data` (APITokenCreateResponse)

### 400 — Bad Request - Invalid input

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 401 — Unauthorized - Missing or expired token, an MCP OAuth token used against the REST API, or no access to this business

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 403 — Forbidden - Insufficient permissions

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 422 — Validation Error

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

## Code samples

### cURL

```bash
curl -X POST 'https://api.ondayzero.com/api/v1/tokens' \
  -H 'Authorization: Bearer dz_your_token' \
  -H 'x-business-id: YOUR_BUSINESS_ID' \
  -H 'Content-Type: application/json' \
  -d '{
  "allowed_ips": [
    "203.0.113.10",
    "198.51.100.0/24"
  ],
  "description": "Read-only token for the reporting assistant",
  "expires_in_days": 90,
  "name": "Month-end close agent",
  "scopes": [
    "read_only"
  ]
}'
```

### JavaScript

```javascript
const response = await fetch('https://api.ondayzero.com/api/v1/tokens', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer dz_your_token',
    'x-business-id': 'YOUR_BUSINESS_ID',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "allowed_ips": [
    "203.0.113.10",
    "198.51.100.0/24"
  ],
  "description": "Read-only token for the reporting assistant",
  "expires_in_days": 90,
  "name": "Month-end close agent",
  "scopes": [
    "read_only"
  ]
}),
});
const data = await response.json();
```

### Python

```python
import httpx

headers = {
    "Authorization": "Bearer dz_your_token",
    "x-business-id": "YOUR_BUSINESS_ID",
}

payload = {'allowed_ips': ['203.0.113.10', '198.51.100.0/24'],
 'description': 'Read-only token for the reporting assistant',
 'expires_in_days': 90,
 'name': 'Month-end close agent',
 'scopes': ['read_only']}

response = httpx.post("https://api.ondayzero.com/api/v1/tokens", headers=headers, json=payload)
data = response.json()
```

## See also

- HTML version: https://www.ondayzero.com/docs/reference/api-tokens/create-token
- OpenAPI slice: https://www.ondayzero.com/docs/reference/api-tokens/create-token/openapi.json
- Other endpoints in **api-tokens**: https://www.ondayzero.com/docs/reference/api-tokens (markdown bundle: https://www.ondayzero.com/docs/reference/api-tokens.md, OpenAPI: https://www.ondayzero.com/docs/reference/api-tokens/openapi.json)
- Endpoint catalog: https://www.ondayzero.com/docs/reference/index.md
