# HTTP API

The HTTP API manages short-lived verification tickets and redeems one-time codes issued over the Minecraft port. The machine-readable schema lives in [openapi.yaml](https://docs.sculk.network/nightly/oauthmc/openapi.md). For TypeScript and JavaScript backends, use the [official client SDK](https://docs.sculk.network/nightly/oauthmc/sdk/js.md).

## Authentication and tenancy

Every `/v1/*` endpoint requires an `Authorization` header:

```text
Authorization: Bearer <key>
```

The `Bearer` scheme is matched case-insensitively per RFC 7235. Configure keys with `OAUTHMC_API_KEYS` (comma-separated, at least 32 characters each). Keys are compared in constant time, and each key defines an isolated tenant: tickets created with key A are visible, redeemable, and deletable only with key A. Any other key receives `404 not_found`.

`GET /healthz` requires no authentication.

## Limits and error format

- Request bodies are capped at 16 KiB (`413 body_too_large`).
- Body reads time out after `OAUTHMC_API_BODY_TIMEOUT_SECONDS` (default `10` s, returning `408 body_timeout`). Unreadable bodies return `400 invalid_body`.
- Header reads time out after `OAUTHMC_API_HEADER_TIMEOUT_SECONDS` (default `10` s). Concurrent API connections are capped at `OAUTHMC_MAX_API_CONNECTIONS` (default `256`).
- All error responses return `Content-Type: application/json`:

```json
{
  "error": "unauthorized",
  "message": "missing or invalid bearer token"
}
```

Two errors carry an extra field: `attempts_left` on `422 wrong_code` and `status` on `409 not_redeemable`.

## Endpoints

### `POST /v1/tickets`

Creates a verification ticket in `pending` state.

Request body:

```json
{
  "name": "HatsuneMiku",
  "mode": "link",
  "link_url": "https://example.com/account/minecraft/verify#ticket={ticket}&code={code}",
  "ttl_seconds": 900,
  "max_attempts": 5,
  "exclusive": false,
  "messages": {
    "code": "Your code: {code} (valid {minutes_left} min)",
    "unverified": "Could not verify {name}."
  },
  "metadata": {
    "user_id": "42"
  }
}
```

Only `name` is required. Unknown fields return `400 invalid_json`.

| Field | Required | Rules | Default | Cap |
| --- | --- | --- | --- | --- |
| `name` | Yes | 1–16 ASCII characters matching `[A-Za-z0-9_]` | — | 16 chars |
| `mode` | No | `"code"` or `"link"` (any other value, including `"auto"`, returns `422 unsupported_mode`) | `"code"` | — |
| `link_url` | When `mode` is `"link"` | Template URL (1–512 bytes) containing both `{ticket}` and `{code}`. Must start with `https://` (`http://` is accepted only when `OAUTHMC_ALLOW_HTTP_LINKS=1`). After substituting both placeholders, the URL must have a valid host (no userinfo `@`) and contain only RFC 3986 URI characters (unreserved, reserved, or `%HH`; `[` and `]` are allowed in the query string only, and at most one `#`). Omitting `link_url` in link mode or sending it in code mode returns `422 invalid_link_url`. | — | 512 bytes |
| `ttl_seconds` | No | Whole integer `>= 1` (rejects `0`, negatives, decimals, and scientific notation such as `1e3`) | `OAUTHMC_TICKET_TTL` (`900`) | `OAUTHMC_MAX_TTL` (`3600`) |
| `max_attempts` | No | Whole integer `>= 1` | `OAUTHMC_MAX_ATTEMPTS` (`5`) | `OAUTHMC_MAX_ATTEMPTS_CAP` (`10`) |
| `exclusive` | No | Boolean. When `true`, returns `409 name_busy` if any open ticket exists for `name` (case-insensitive) across any tenant. | `OAUTHMC_EXCLUSIVE` (`false`) | — |
| `messages` | No | Overrides for `code`, `unverified`, `dialog_title`, `dialog_code`, `link`, `open_link`, `copy_code`, `verified`, `expired`, `locked`, `replaced` (1–512 bytes each). Applied only when all open tickets for `name` belong to the same tenant. | Server templates | 512 bytes per key |
| `metadata` | No | JSON object or `null`, at most 1024 serialized bytes, echoed on `GET /v1/tickets/{id}`. | `null` | 1024 bytes |

Response `201 Created`:

```json
{
  "address": "verify.example.com",
  "exclusive": false,
  "expires_at": "2026-11-26T10:32:09Z",
  "id": "tkt_823fc8dac7451029a38794aef229ea24",
  "max_attempts": 5,
  "metadata": null,
  "mode": "code",
  "name": "HatsuneMiku",
  "status": "pending"
}
```

`address` equals `OAUTHMC_PUBLIC_ADDRESS`, or `null` when unset. Ticket IDs are `tkt_` followed by 32 lowercase hex characters (128 bits of OS randomness).

### `GET /v1/tickets/{id}`

Returns the current ticket status for the calling tenant. Never returns the player's UUID, code, or filled `link_url`.

Response `200 OK`:

```json
{
  "attempts_left": 5,
  "expires_at": "2026-11-26T10:32:09Z",
  "id": "tkt_823fc8dac7451029a38794aef229ea24",
  "metadata": null,
  "mode": "code",
  "name": "HatsuneMiku",
  "status": "joined"
}
```

Ticket statuses:

- `pending`: ticket created; player has not completed online-mode login yet.
- `joined`: player completed online-mode login and received a code.
- `redeemed`: code redeemed; ticket is consumed.
- `expired`: TTL elapsed before redemption. Remains queryable for `OAUTHMC_EXPIRED_GRACE_SECONDS` before sweep.
- `locked`: wrong codes exhausted `max_attempts`.

Returns `404 not_found` for unknown IDs, malformed IDs, or tickets owned by another tenant.

### `POST /v1/tickets/{id}/redeem`

Redeems a one-time code. OAuthMC trims whitespace, ignores `-` characters, and matches ASCII case-insensitively (`N6AN-46TW` and `n6an46tw` are equivalent).

Request body:

```json
{
  "code": "N6AN-46TW"
}
```

Response `200 OK`:

```json
{
  "name": "HatsuneMiku",
  "uuid": "a0c4b8c5-a04f-4a6f-9d02-1dea6154a9f6"
}
```

`uuid` is lowercase with standard hyphens. `name` is Mojang's canonical username from `hasJoined`. The ticket transitions to `redeemed`, and any held link-mode client disconnects with the `verified` message.

Response `422 Unprocessable Entity` (`wrong_code`):

```json
{
  "attempts_left": 4,
  "error": "wrong_code",
  "message": "verification code does not match"
}
```

Each wrong code decrements `attempts_left`. Reaching `0` transitions the ticket to `locked` and disconnects any held link-mode client with the `locked` message.

Response `409 Conflict` (`not_redeemable`):

```json
{
  "error": "not_redeemable",
  "message": "ticket cannot be redeemed in its current status",
  "status": "redeemed"
}
```

Returned when the ticket is `pending`, `redeemed`, `expired`, or `locked`.

### `DELETE /v1/tickets/{id}`

Cancels and removes a ticket owned by the calling tenant. If a link-mode client is held for the ticket, it disconnects with the `expired` message.

- `204 No Content`: ticket deleted.
- `404 not_found`: unknown ID or ticket owned by another tenant.

### `GET /healthz`

Unauthenticated liveness probe.

Response `200 OK`:

```json
{
  "ok": true
}
```

## Error codes

Every error code returned by `api.rs`:

| HTTP status | `error` | Cause |
| --- | --- | --- |
| `400` | `invalid_body` | Failed to read the request body. |
| `400` | `invalid_json` | Malformed JSON, wrong field type, or unknown field. |
| `401` | `unauthorized` | Missing or invalid `Authorization: Bearer <key>` header. |
| `404` | `not_found` | Unknown route, invalid ticket ID format, unknown ticket ID, or ticket owned by another tenant. |
| `405` | `method_not_allowed` | Unsupported HTTP method on `/v1/tickets`, `/v1/tickets/{id}`, or `/v1/tickets/{id}/redeem`. |
| `408` | `body_timeout` | Request body did not finish within `OAUTHMC_API_BODY_TIMEOUT_SECONDS`. |
| `409` | `name_busy` | `exclusive: true` requested while an open ticket exists for the same username. |
| `409` | `not_redeemable` | Ticket is not in `joined` state. Includes `status`. |
| `413` | `body_too_large` | Request body exceeds 16 KiB. |
| `422` | `invalid_name` | `name` is not 1–16 characters of `[A-Za-z0-9_]`. |
| `422` | `unsupported_mode` | `mode` is not `"code"` or `"link"`. |
| `422` | `invalid_link_url` | `link_url` missing in link mode, present in code mode, over 512 bytes, missing placeholders, non-`https://` (unless `OAUTHMC_ALLOW_HTTP_LINKS=1`), invalid host, or non-RFC 3986 characters. |
| `422` | `invalid_ttl_seconds` | `ttl_seconds` is not an integer in `1..=OAUTHMC_MAX_TTL`. |
| `422` | `invalid_max_attempts` | `max_attempts` is not an integer in `1..=OAUTHMC_MAX_ATTEMPTS_CAP`. |
| `422` | `invalid_messages` | A per-ticket message override is empty or exceeds 512 bytes. |
| `422` | `invalid_metadata` | `metadata` is not a JSON object (or `null`), or serializes to more than 1024 bytes. |
| `422` | `wrong_code` | Submitted code does not match. Includes `attempts_left`. |
| `503` | `ticket_limit` | Stored tickets (including expired tickets in grace) reached `OAUTHMC_MAX_TICKETS` or `OAUTHMC_MAX_TICKETS_PER_KEY`. |

For configuration variables and message templates, see [Configuration](https://docs.sculk.network/nightly/oauthmc/configuration.md).
