# HTTP — `/v1/apiKeys`

API keys authenticate `POST /v1/exchange`, the write WebSocket, the
market-data WebSocket, and `GET /v1/rateLimits`. Key management uses a Para
wallet session, not an API key. An API key on a key-management route fails
with `401 unauthorized` and the detail `wallet_auth_required`.

Each key acts for one account: the master account or one
[sub-account](/concepts/sub-accounts) of the owner. The key selects the
account, and no request field selects another one. Each account has at most
one active key.

```http
Authorization: Bearer <para-jwt>
X-Tessera-Wallet: 0x... # required when the JWT has multiple EVM wallets
```

Keep the raw API key in a server-side secret store. Browser clients must never
receive it.

## Create a key

```http
POST /v1/apiKeys
Content-Type: application/json
```

```json
{ "keyPrefix": "desk", "expiresAtMs": 1787776000000, "account": "0x9f3c5d0e7a41b2c86f1d94e03a5b7c2d18e6f0a1" }
```

All fields are optional. Send `{}` to create a key for the master account
with the defaults.

| Field | Default | Rule |
| --- | --- | --- |
| `keyPrefix` | `tessera` | Must match `[A-Za-z0-9][A-Za-z0-9_-]{0,31}`. |
| `expiresAtMs` | 30 days after the server time | Unix milliseconds. Must be later than the server time and at most 365 days after it. |
| `account` | The master account | The address of the account that the key acts for. It must be an account of the session owner, or the request fails with `403 forbidden` and the detail `account_not_owned`. |

The server accepts an `expiresAtMs` up to 5 minutes past the 365-day limit. A
client that computes the expiry from a clock running slightly ahead still gets
its 365-day key. The response returns the requested `expiresAtMs` to the
millisecond.

The response returns the raw `apiKey` once. Creating a key revokes the previous
active key of the same account. The keys of the other accounts stay active. To
extend a key, create a new one.

Each wallet can create 10 keys per hour, for all its accounts together.
Revealing or rotating a [venue cloid key](/api/optimizing-latency#get-your-key)
spends the same budget. Revoking a key does not.

```jsonc
{
  "status": "ok",
  "data": {
    "id": "8f20b2e6-8b7a-4a0b-bf3d-6fb70c622f76",
    "keyPrefix": "desk",
    "createdAtMs": 1780000000000,
    "expiresAtMs": 1787776000000,
    "revokedAtMs": null,
    "status": "active",
    "account": "0x9f3c5d0e7a41b2c86f1d94e03a5b7c2d18e6f0a1",
    "apiKey": "desk_..."
  },
  "server_time_ms": 1780000000100
}
```

## List keys

```http
GET /v1/apiKeys
GET /v1/apiKeys?account=0x9f3c5d0e7a41b2c86f1d94e03a5b7c2d18e6f0a1
```

Without `account`, the list has the keys of every account of the owner: the
master account first, then the sub-accounts by slot. For each account, it has
the 20 most recent keys, newest first. With `account`, the list has the keys
of that account only. `account` must be an account of the session owner, or
the request fails with `403 forbidden` and the detail `account_not_owned`.

Each row has `account`, the address of the account that the key acts for. No
row has `apiKey`. Each account has at most one `active` key. `data` is an
empty array when the owner has never created a key.

```jsonc
{
  "status": "ok",
  "data": [
    { "id": "8f20b2e6-8b7a-4a0b-bf3d-6fb70c622f76", "keyPrefix": "desk", "createdAtMs": 1780000000000, "expiresAtMs": 1787776000000, "revokedAtMs": null, "status": "active", "account": "0x0000000000000000000000000000000000000001" },
    { "id": "2b1d6c0e-54a3-4f6e-9a55-0c8a3f1e7d21", "keyPrefix": "tessera", "createdAtMs": 1779000000000, "expiresAtMs": 1781592000000, "revokedAtMs": 1780000000000, "status": "revoked", "account": "0x0000000000000000000000000000000000000001" },
    { "id": "d3a9e7f4-1c62-4b8e-8f0d-5e4b2a9c6f10", "keyPrefix": "tessera", "createdAtMs": 1776000000000, "expiresAtMs": 1776604800000, "revokedAtMs": 1779000000000, "status": "expired", "account": "0x0000000000000000000000000000000000000001" },
    { "id": "5c0e9a7b-3d21-4f88-b6e4-2a9d1c7f0e35", "keyPrefix": "mm", "createdAtMs": 1790000000000, "expiresAtMs": 1792592000000, "revokedAtMs": null, "status": "active", "account": "0x9f3c5d0e7a41b2c86f1d94e03a5b7c2d18e6f0a1" }
  ],
  "server_time_ms": 1780000000100
}
```

`status` reports the first event that ended the key.

| `status` | Meaning |
| --- | --- |
| `active` | The key is not revoked and `expiresAtMs` is in the future. It authenticates. |
| `revoked` | A rotation or `DELETE` revoked the key before it expired. |
| `expired` | The key reached `expiresAtMs` before any revocation. |

A rotation also sets `revokedAtMs` on a key that had already expired. That key
stays `expired`, like the last row above.

## Revoke a key

```http
DELETE /v1/apiKeys/{keyId}
```

`keyId` is the `id` of the key, from the list or create response. The key can
belong to any account of the session owner.

```jsonc
{ "status": "ok", "data": { "revoked": true }, "server_time_ms": 1780000000100 }
```

`revoked` is `true` if `keyId` was the unrevoked key of its account and is now
revoked. An unrevoked key that already expired counts too, and its `status`
stays `expired`. If the key was already revoked, or a rotation replaced it,
`revoked` is `false` and nothing changes. So a page that still shows an old
key cannot revoke the key that replaced it.

If no account of the session owner holds the key, the request fails with
`404 invalid_request` and the detail `api_key_not_found`. A key of another
owner and an unknown id get the same answer.

`DELETE /v1/apiKeys` without a key id was removed and returns `405`.

## Errors

| HTTP | `code` | `detail` | Meaning |
| --- | --- | --- | --- |
| `429` | `rate_limited` | `rate_limited` | The wallet exceeded its key creation budget, or the client IP exceeded the HTTP budget. |
| `400` | `invalid_request` | `wallet_required` | The JWT holds several EVM wallets and `X-Tessera-Wallet` is missing. |
| `400` | `invalid_request` | `bad_request` | `X-Tessera-Wallet` or `account` is not an EVM address, `keyPrefix` does not match the pattern, or `expiresAtMs` is not in the future or is more than 365 days away. |
| `400` | `invalid_request` | `invalid_path` | `keyId` is not a UUID. |
| `404` | `invalid_request` | `api_key_not_found` | `DELETE`: no account of the session owner holds `keyId`. |
| `422` | `invalid_request` | `invalid_request` | A body field has the wrong type, for example a fractional `expiresAtMs`. |
| `401` | `unauthorized` | `unauthorized` | Missing bearer, or the token is not a valid Para JWT. |
| `401` | `unauthorized` | `wallet_auth_required` | The bearer is an API key. Key management takes the wallet session. |
| `403` | `forbidden` | `not_whitelisted` | The wallet is not allowlisted. |
| `403` | `forbidden` | `wallet_required` | The JWT holds no EVM wallet. |
| `403` | `forbidden` | `wallet_not_connected` | `X-Tessera-Wallet` names a wallet the JWT does not hold. |
| `403` | `forbidden` | `account_not_owned` | `account` is not an account of the session owner. |

A rejected create leaves the active key in place.

Revocation, rotation and expiry close the open exchange and market-data
WebSockets of the invalidated key. An operation admitted before that close may
still complete.

## What a key can do

A key places, modifies, and cancels orders on its own account only. It reads
the request budget of its account on [`GET /v1/rateLimits`](/api/rate-limits).
Only the key of the master account can request a deposit. The key of a
sub-account gets `403 forbidden` with the detail `master_account_required`. No
key can withdraw or [transfer](/api/exchange#transfer) cash. Those actions take the wallet
session.
