
# Create a directory token

`POST /v1/directories/{directory_id}/tokens` · permission `directories:write`

Issues a new SCIM token for rotation. A directory has at most 2 valid tokens: give the new one to the identity provider, then revoke the old one with `DELETE /v1/directories/{directory_id}/tokens/{token_id}`. `token` is returned only in this response, once (`Cache-Control: no-store`); a response replayed for the same `Idempotency-Key` omits it and carries `secret_redacted: true`. An empty object (`{}`) uses the defaults. The creation is audited as `directory.token_created`.

## Request

| Field | Type | Required | Description |
|---|---|---|---|
| `directory_id` | path | yes | SCIM directory ID (`dir_…`). |

| Field | Type | Required | Description |
|---|---|---|---|
| `label` | string | no | 1–100 characters; default `Token`. |
| `expires_in_days` | integer | no | 30–730; default 365. |

## Response

`201 Created` with the token's metadata and the one-time `token`.

```json
{
  "id": "0192a3b5-0a1b-7c2d-8e3f-4a5b6c7d8e9f",
  "label": "rotation",
  "created_at": "2026-10-19T09:40:00.123456Z",
  "expires_at": "2027-01-17T09:40:00.123456Z",
  "revoked_at": null,
  "last_used_at": null,
  "token": "idp_scim_<token>"
}
```

## Errors

| HTTP | code | When |
|---|---|---|
| 404 | `not_found` | The directory does not exist in this environment. |
| 409 | `conflict` | The directory already has 2 valid tokens; revoke one first. |
| 422 | `validation_failed` | `label` is empty or too long, or `expires_in_days` is outside 30–730. |
| 401 | `unauthenticated` | The credential is missing, invalid or revoked. |
| 403 | `insufficient_permissions` | The credential lacks the required permission. |
| 429 | `rate_limited` | The rate limit was exceeded; retry after the `Retry-After` period. |
| 503 | `service_unavailable` | Directory sync is temporarily unavailable or not configured in this deployment; retry after the `Retry-After` period. |

## Example

```bash
curl -i -X POST "https://api.eu.keenauth.com/v1/directories/dir_01j9xm2n4p6q8r0s2t4v6w8y0z/tokens" \
  -H "Authorization: Bearer $KEENAUTH_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"label":"rotation","expires_in_days":90}'
```
