# Create an API key.

> Session only: a key cannot create keys.
> https://koi.dog/docs/api/api-keys/create-api-key/

`POST /api/v1/api-keys` · Key: `session only`

`scopes` must be listed; a key has `approve` only when it is in that list. The answer carries the token once; Koidog keeps only its hash. To call it outside the dashboard, send the dashboard's `koidog_session` cookie and `Origin: https://app.koi.dog`. No path or query parameters.

## Headers.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | `string` | no | Makes a retry safe: the same key with the same request returns the first answer and does nothing else. |

## Body.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | `string` | yes | A name for the key, shown in the dashboard and on the timeline. 1 to 80 characters. |
| `scopes` | `array` | yes | Listed explicitly: there is no wildcard and no scope implies another. Any of `read`, `crews`, `config`, `approve`. At least 1 item. |
| `rateLimitPerMinute` | `1–6000` | no | Requests a minute this key may make; past it the answer is `429 rate_limited`. Default `600`. |
| `expiresAt` | `string` | no | When the key stops working, as an ISO 8601 time in UTC (`2027-01-01T00:00:00Z`). Left out, it does not expire. Matches `^(?:(?:\d\d[2468][048]\|\d\d[13579][26]\|\d\d0[48]\|[02468][048]00\|[13579][26]00)-02-29\|\d{4}-(?:(?:0[13578]\|1[02])-(?:0[1-9]\|[12]\d\|3[01])\|(?:0[469]\|11)-(?:0[1-9]\|[12]\d\|30)\|(?:02)-(?:0[1-9]\|1\d\|2[0-8])))T(?:(?:[01]\d\|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z))$`. |

## Response.

201 Created: a JSON object. The example has placeholder values.

- **422 validation_failed**: The body does not match its schema; each problem is in `details`.

## Request.

```sh
# The dashboard session only: an API key is refused (403 session_required).
curl -X POST https://app.koi.dog/api/v1/api-keys \
    -H "Cookie: koidog_session=$KOIDOG_SESSION" \
    -H "Origin: https://app.koi.dog" \
    -H "Idempotency-Key: $(uuidgen)" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "CI",
      "scopes": [
        "read",
        "crews"
      ]
    }'
```

```js
// The dashboard session only: an API key is refused (403 session_required).
const res = await fetch("https://app.koi.dog/api/v1/api-keys", {
  method: "POST",
  headers: {
    Cookie: `koidog_session=${process.env.KOIDOG_SESSION}`,
    Origin: "https://app.koi.dog",
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "CI",
    scopes: [
      "read",
      "crews",
    ],
  }),
});
console.log(await res.json());
```

```python
import os
import uuid

import requests

# The dashboard session only: an API key is refused (403 session_required).
r = requests.post(
    "https://app.koi.dog/api/v1/api-keys",
    headers={
        "Cookie": f"koidog_session={os.environ['KOIDOG_SESSION']}",
        "Origin": "https://app.koi.dog",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "name": "CI",
        "scopes": [
            "read",
            "crews",
        ],
    },
)
r.raise_for_status()
print(r.json())
```

## Response example.

```json
{
  "key": {
    "id": "string",
    "name": "string",
    "prefix": "string",
    "scopes": [],
    "rateLimitPerMinute": 0,
    "state": "active",
    …
  },
  "token": "string"
}
```
