Four credential types with deliberately different powers. The model is simple once you see what each one unlocks — and the rules for storing them are the difference between a shipped game and a leaked admin key.
| Header | Scope | Where it lives |
|---|---|---|
| X-Api-Token | Identifies your game — required on every endpoint | Public: safe in shipped clients |
| X-Game-Player-Token | One player's session — rooms, matchmaking, friends, data | On the player's device/backend |
| X-Api-Private-Token | Admin: player list, bans, game-data writes | Server-side only, always |
| X-Realtime-Token | One realtime connection, 5-minute mint window | Ephemeral — mint per session |
Credentials travel in headers only. Tokens in URLs (?api_token= and friends) are not accepted — URLs leak through proxies, referers and logs. The one deliberate exception is the realtime WebSocket handshake, which accepts ?token= because browser WebSockets can't set headers; prefer the header on every platform that supports it.
register returns private_key once — the API never shows it again, so the client must persist it (localStorage, keychain, your own session store). Losing it means losing the account; rotate it with POST /api/game_players.php/rotate when a device may be compromised, which returns a fresh credential and retires the old one.
Player tokens are scoped to the game that issued them — a token from game A returns 403 on game B's endpoints. Session state rides on login, periodic heartbeat calls, and logout; see Players for intervals and offline transitions.
The realtime channel uses a separate short-lived credential. POST /api/realtime.php/token (API token + player token) returns a token plus the relay coordinates — one live token per player, ~5-minute connect window, invalidated the moment a new one is minted or the socket authenticates elsewhere. This is why sharing a game session across tabs "steals" the realtime connection.
curl -X POST "https://api.michitai.com/api/realtime.php/token" \
-H "X-Api-Token: API_TOKEN" \
-H "X-Game-Player-Token: PLAYER_TOKEN"
Rate limiting is per-endpoint and per-principal (player or token). Over-limit calls return HTTP 429 with a standard error body and a Retry-After hint — back off rather than retry immediately. Limits are sized for real gameplay (e.g. realtime token minting at 10/min/player); heartbeat-style calls are what poorly-looping clients most often trip on.
401 — token missing or invalid (wrong header name, stale player token)403 — valid credential, wrong scope (player token from another game, private op without private token)429 — rate limited; honor Retry-AfterThe realtime relay enforces a room ACL on top of token auth: only the room host may broadcast or target other players — non-host sends are restricted to the host target (action submissions, inputs). Membership is re-checked against the database, so a player removed via REST loses their room channel promptly even if the socket is still open.