Authentication, Security and Rate Limits

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.

The credential model

HeaderScopeWhere it lives
X-Api-TokenIdentifies your game — required on every endpointPublic: safe in shipped clients
X-Game-Player-TokenOne player's session — rooms, matchmaking, friends, dataOn the player's device/backend
X-Api-Private-TokenAdmin: player list, bans, game-data writesServer-side only, always
X-Realtime-TokenOne realtime connection, 5-minute mint windowEphemeral — 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.

Player tokens: lifecycle

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.

Realtime tokens

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 limits and error responses

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-After
4xx/5xx — always {"success": false, "error": "…"}; full catalogue in Errors

Real-time channel authorization

The 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.

Checklist: secrets hygiene

  1. Ship only the API token + player tokens in client builds. If the private token appears in a game binary, web bundle or public repo, rotate it in the cabinet immediately.
  2. Server-side admin calls come from your own backend holding the private token in env vars — never derive it client-side.
  3. Store player tokens like session cookies; rotate on suspicion.
  4. Never log full headers or tokens — treat them like passwords in telemetry.
  5. Mint realtime tokens per session; don't cache them across launches.