# Update API key

> Replaces the key's name, scopes and spending limit. `spending_limit` is a decimal string in USD, sent with its `spending_limit_period`; `null` removes the limit. The secret does not change, so live traffic keeps working.

`PUT /api-keys/{id}`

**Auth:** API key or OAuth token

## Path parameters

- `id` (required, string) — The API key to update. Example `"ak_01KE7XWWEQ4MCGWKBQKJ1G47RP"`.

## Request body

- `name` (string | null) — ≥ 1 characters, ≤ 256 characters.
- `scopes` (required, string[]) — ≥ 1 items.
- `spending_limit` (required, string | null) — Most this key can spend per period, in USD. `null` removes the limit. A limit of `0` blocks every request. matches `^(0|[1-9]\d{0,7})(\.\d{1,2})?$`. Example `"25.00"`.
- `spending_limit_period` (string | null) — When the limit resets, at the start of each UTC day, week or month. Required with a limit; leave it out, or null, without one. One of: `"day"`, `"week"`, `"month"`.

## Response

### 200 — API key updated

- `data` (required, object)
  - `id` (required, string)
  - `name` (required, string | null)
  - `key_preview` (required, string)
  - `user_id` (required, string)
  - `scopes` (required, string[])
  - `spending_limit` (required, string | null)
  - `spending_limit_period` (required, string | null) — One of: `"day"`, `"week"`, `"month"`.
  - `created_at` (required, string) — format: date-time.
  - `updated_at` (required, string) — format: date-time.

```json
{
  "data": {
    "id": "string",
    "name": "string",
    "key_preview": "string",
    "user_id": "string",
    "scopes": [
      "generate"
    ],
    "spending_limit": "string",
    "spending_limit_period": "day",
    "created_at": "string",
    "updated_at": "string"
  }
}
```

### 400 — The request was rejected before anything was queued. `error.message` says what to change. A field that failed the schema, or one the endpoint does not take, is also listed in `error.issues`.

- `error` (required, object)
  - `code` (required, string) — `validation_error` for a body that failed validation, `invalid_json` for one that is not JSON. One of: `"validation_error"`, `"invalid_json"`.
  - `message` (required, string) — Every problem, in one sentence each, led by the field it is about.
  - `issues` (object[]) — One entry per field that failed the schema, and one per field the endpoint does not take.
    - `path` (required, (string | integer)[]) — Where the problem is, e.g. `["inputs", 0, "source"]`. Empty for the body as a whole.
    - `message` (required, string)

```json
{
  "error": {
    "code": "validation_error",
    "message": "count: Too big: expected number to be <=20. colour: Unknown field.",
    "issues": [
      {
        "path": [
          "count"
        ],
        "message": "Too big: expected number to be <=20"
      },
      {
        "path": [
          "colour"
        ],
        "message": "Unknown field."
      }
    ]
  }
}
```

## Request samples

### cURL

```bash
curl -X PUT https://api.mynth.io/api-keys/ak_01KE7XWWEQ4MCGWKBQKJ1G47RP \
  -H "Authorization: Bearer $MYNTH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "scopes": [
      "generate"
    ],
    "spending_limit": "25.00"
  }'
```

### JavaScript

```ts
const response = await fetch("https://api.mynth.io/api-keys/ak_01KE7XWWEQ4MCGWKBQKJ1G47RP", {
  method: "PUT",
  headers: {
    Authorization: `Bearer ${process.env.MYNTH_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "scopes": [
      "generate"
    ],
    "spending_limit": "25.00"
  }),
});

const { data } = await response.json();
```

The full schema for this endpoint is in the [OpenAPI document](https://api.mynth.io/openapi.json).
