# HTTP reference

Everything the SDKs do is plain HTTPS and JSON, so any engine or language can use it: C++ (Unreal, custom engines), GDScript, Lua, Rust, Python. Base URL: `https://yumina.io`.

## Sign the player in

Your game is a public client (no secret). Use Authorization Code + PKCE (S256).

**Authorize URL** — open it in a browser, or hand it to the QR relay below:

```
GET /api/auth/oauth2/authorize
  ?response_type=code&client_id=<client_id>
  &redirect_uri=<redirect>
  &scope=openid profile offline_access ai:chat wallet:read cards:read
  &code_challenge=<base64url(sha256(verifier))>&code_challenge_method=S256
  &state=<random, 16+ chars>
  &resource=https://yumina.io/api/ai/v1
```

`redirect` is `http://127.0.0.1:<47613–47617>/callback` (registered for every game), Yumina's relay `https://yumina.io/api/apps/link/callback`, or an https page you added.

**Token**

```
POST /api/auth/oauth2/token        (application/x-www-form-urlencoded)
grant_type=authorization_code&code=…&redirect_uri=…&client_id=…&code_verifier=…&resource=https://yumina.io/api/ai/v1
grant_type=refresh_token&refresh_token=…&client_id=…&resource=https://yumina.io/api/ai/v1
```

Returns `access_token` (15 minutes), `refresh_token` (rotates on every use — always keep the newest; reusing a spent one revokes the sign-in), `expires_in`. Revoke with `POST /api/auth/oauth2/revoke` (`token`, `token_type_hint=refresh_token`, `client_id`).

**Scan to sign in**

```
POST /api/apps/link   { "client_id": "…", "authorize_url": "<authorize URL with the relay as redirect_uri>" }
→ { "user_code": "ACDE-2345", "verification_uri": "https://yumina.io/l",
    "verification_uri_complete": "https://yumina.io/l/ACDE2345",
    "qr": { "size": 29, "rows": ["1111111010…", …] },
    "poll_token": "…", "interval": 2, "expires_in": 600 }
```

The authorize URL must carry `prompt=consent`: a scan-to-sign-in always shows the consent screen, with the code your game displays, so a QR code someone else posted can't quietly connect a player.

Draw `qr.rows[r][c] == "1" as dark squares with a light border of 4 modules. Then poll every `interval` seconds:

```
POST /api/apps/link/poll   { "poll_token": "…" }
→ { "status": "pending" } | { "status": "ready", "code": "…", "redirect_uri": "…" } | { "status": "denied" }   (410 when expired)
```

Exchange the code at the token endpoint with your verifier and the relay `redirect_uri`. The code is handed out once.

## Character turns

All calls below take `Authorization: Bearer <access_token>`.

```
POST /api/ai/v1/turns
{
  "character": "abigail",                         // your id for who is speaking; one memory per character per save
  "card": { "id": "<published card id>" },        // or { "world": { …a Yumina card… } }
  "text": "Morning! Anything warm?",              // what the player said, or a [stage direction]
  "inputs": { "hearts": 3, "weather": "foggy" },  // your manifest's inputs
  "allow": ["invite", { "id": "give_item", "params": { "item": { "enum": ["Bandage"] } } }],
                                                  // optional: actions your rules allow this turn; an object narrows
                                                  // a param for this turn ("enum" for strings, "min"/"max" for numbers)
  "blocked": { "give_gold": "Her purse is empty." },  // optional: why an action is out right now; the character declines with it
  "period": "y1-spring-3",                        // optional: for per-period limits
  "memory": { "head": "<head from the save, or null>" },
  "conversation": "<id from the last turn>",      // optional: continue it; omit to start a new one
  "results": [{ "turnId": "…", "action": "give_item", "applied": true, "note": "got a coffee" }],
  "language": "en",                               // optional: "en", "zh-Hans", "ja"… or a language name
  "model": "…",                                   // optional: an id from GET /models
  "manifest": { … },                              // optional: overrides the registered manifest
  "stream": true
}
```

Send one turn at a time per player and game, each with the head the previous one returned; a second one while the first is still answering gets `429 request_in_progress` (the SDKs queue them for you).

Without `stream`, the answer is `{ "turn": Turn }`. With `stream: true` it is server-sent events:

| Event | Data |
| --- | --- |
| `text` | `{ "delta": "…" }` — append it; or `{ "replace": "…" }` — replace what you showed. Never contains bookkeeping. |
| `line` | `{ "text": "…" }` — the whole line, final: show it now. The game's check of what the line promised still runs, so act on nothing until `turn`. |
| `turn` | `{ "turn": Turn }` — the final, authoritative turn. |
| `error` | `{ "error": { "message", "code" } }` |

```json
Turn = {
  "id": "…", "character": "abigail", "conversation": "…",
  "text": "Up since four? You look it.",
  "choices": ["Thanks, Mira.", "Extra strong is right.", "What do I owe you?"],
  "outputs": { "friendship_delta": 5 },
  "action": { "id": "give_item", "params": { "item": "coffee" }, "status": "accepted" },
  "memory": { "head": "…" },
  "model": "…",
  "yumina": { "cost": 0.3, "balance": 1999.7 }
}
```

`action` is `null` when there is none. A refused one has `"status": "rejected"`, a `code` (`limit`, `not_allowed`, `invalid_params`, `unknown_action`, `said_not_done`), a `reason` in the request's `language`, the `params` the character asked for, and for `said_not_done` a `said`; never apply it. `"allow": []` allows no action this turn. The character's next turn is told it did not happen.

`card.world` is your game's own card, built at runtime: see [Characters your game builds](./cards).

## Other calls

| Call | Scope | Returns |
| --- | --- | --- |
| `GET /api/ai/v1/me` | wallet:read | name, username, balance, plan, defaultModel, plansUrl |
| `GET /api/ai/v1/models` | ai:chat | the models the player can pick, in yumina.io's order |
| `GET /api/ai/v1/cards` | cards:read | published cards made for your game; the ones this player added on Game Mods come first, with `picked: true` |
| `GET /api/ai/v1/memory?character=…&head=…` | ai:chat | what a character remembers in that save, newest first |
| `GET /api/ai/v1/manifest` | ai:chat | the manifest your game runs with |
| `POST /api/ai/v1/chat/completions` | ai:chat | OpenAI-compatible chat, when you build prompts yourself |

## Errors

Errors are `{ "error": { "message", "type", "code" } }`. The codes you will want to handle:

| Code | Status | Do this |
| --- | --- | --- |
| `invalid_token`, `missing_token` | 401 | Refresh; if that fails, sign the player in again. |
| `insufficient_balance` | 402 | Tell the player and open `plansUrl` from `/me`. |
| `app_in_development` | 403 | Only you and your testers can sign in until the game is live. |
| `unknown_head` | 409 | The save's memory is from another account: send no head to start fresh. |
| `rate_limit_exceeded`, `request_in_progress` | 429 | Wait `Retry-After` seconds. One turn at a time per player. |
| `invalid_manifest`, `invalid_request` | 400 | The message says which field. |
| `app_not_reviewed` | 403 | Until your game is live, it plays only cards you or your testers published. |
