Game Design/Platform
API Conventions
Last updated 2026-10-29
Standard patterns for API routes in A House Divided.
Admin Routes#
- Auth: Use
requireAdmin()from@/lib/api/requireAdminat the start of handlers. - Status code: Return 403 (Forbidden) when the user is not an admin.
- Pattern:
const auth = await requireAdmin(); if (!auth.ok) return auth.response; const { admin } = auth; // when you need admin.userId, admin.username, etc. - Wrapper alternatives:
withAdminAuth(handler)from@/lib/api/withAdminAuthruns the same check and passes the narrowedauthas the handler's first argument; it does not add error handling.withAdminRoute(handler)in the same file also wraps the handler in the standard error boundary.
Error Handling#
- Pattern: Wrap the handler body in
try/catchand callhandleRouteError(error)from@/lib/api/errorsin thecatchblock. There is nowithRouteErrorwrapper; each route writes its own try/catch.export async function GET(request: Request) { try { // ... handler body } catch (error) { return handleRouteError(error); } } - Expected errors: Throw
ApiError(or its helpersbadRequest,unauthorized,forbidden,notFound,conflict,internalError) from@/lib/api/errorsfor 4xx/5xx.handleRouteErrorrecognizesApiErrorand returns itsstatus/JSON body directly. Expected 4xx errors are intentionally not sent to Sentry. - Uncaught errors: Anything that isn't an
ApiErroris tagged, sent to Sentry viaSentry.captureExceptionandalertOps, logged, and returned as a generic 500 (internalError().toJson()). Pass{ request, route, extra }as the optional second argument to tag the Sentry event with the endpoint, method and entity ids.
Cron Routes#
- Auth: Use
requireCron(request)from@/lib/api/requireCron. - Header:
Authorization: Bearer ${CRON_SECRET} - Return value:
requireCronreturns a boolean, not a response. The route returns 401 itself when it is false. It is false whenCRON_SECRETis unset or the header does not match.
Status Codes#
| Code | Use case |
|---|---|
| 400 | Bad request (badRequest) |
| 401 | Not authenticated (no valid token) |
| 403 | Authenticated but forbidden (e.g. not admin) |
| 404 | Resource not found |
| 409 | Conflict (conflict) |
| 429 | Rate limited |
| 500 | Internal error (internalError) |
Cron vs Admin#
Some routes (e.g. /api/elections/snapshot) allow both cron and admin auth. Check cron first with requireCron(request); if false, fall back to requireAdmin().
Admin or API Key#
Scripts can call some admin routes with an X-Api-Key header instead of a session. Use requireAdminOrApiKey(request) from @/lib/api/requireAdminOrApiKey. A wrong key returns 401, a missing session returns 403, and a server without INTERNAL_API_KEY configured returns 500.
Last checked: 2026-10-29, version 1.18.0
Connected pages
References →
None← Referenced by
docsModule boundaries and layering
A House Divided
Corporate Warfare
Grand Century
Margin of Victory
Desktop client