# Replace an API key.

> Session only.
> https://koi.dog/docs/api/api-keys/rotate-api-key/

`POST /api/v1/api-keys/{id}/rotate` · Key: `session only`

Makes a new key with the old one's name, scopes and limits, and revokes the old one, at once or after `graceMinutes`. The answer carries the new token once. Who may: as for `revokeApiKey`; a service key (as the old key or the new one) is the owner's alone. Works on a key revoked for rotation (`rotationRequired`). Use the `koidog_session` cookie at `https://app.koi.dog/api/v1` with `Origin: https://app.koi.dog`.

## Path parameters.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | The API key's id. |

## Headers.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | `string` | no | Retry the same request with the same key. [Retry rules](https://koi.dog/docs/api/#retries). |

## Body.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `kind` | `enum` | no | The new key's kind. Default: `service` for a service key, `person` for any other (an unproven key becomes the rotating person's). |
| `expiresAt` | `string` | no | When the new key ends. Default: the old key's end when that is still ahead, else none. 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))$`. |
| `graceMinutes` | `0–1440` | no | Minutes the old key keeps working (0 to 1440), to switch its users over; 0 (the default) revokes it at once. Default `0`. |

## Response.

201 Created · JSON object

- **422 validation_failed**: Each schema error appears 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/$ID/rotate" \
    -H "Cookie: koidog_session=$KOIDOG_SESSION" \
    -H "Origin: https://app.koi.dog" \
    -H "Idempotency-Key: $(uuidgen)" \
    -H "Content-Type: application/json" \
    -d '{}'
```

```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/${process.env.ID}/rotate`, {
  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({}),
});
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(
    f"https://app.koi.dog/api/v1/api-keys/{os.environ['ID']}/rotate",
    headers={
        "Cookie": f"koidog_session={os.environ['KOIDOG_SESSION']}",
        "Origin": "https://app.koi.dog",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={},
)
r.raise_for_status()
print(r.json())
```

## Response example.

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