# The API stays on the app's host

The repository's architectural decision, rationale and consequences.

# The API stays on the app's host

**Status:** accepted · **Date:** 2026-11-28

Every client reaches the backend at `/api/*` on the host that serves the app: the browser through same-origin requests, the native app through `EXPO_PUBLIC_SERVER_URL`. That value is compiled into every installed binary and is part of the app's cache scopes and query keys (`apps/mobile/src/platform/offline/snapshot-scope.ts`, `platform/rpc/orpc.ts`, `platform/auth/auth-client.ts`). A binary that is installed keeps calling the host it was built with until the person updates it.

## Decision [#decision]

1. `/api/*` and the `.well-known` discovery routes stay on the app's host, whatever serves them. If the API ever runs in its own Worker ([ADR-0020](0020-one-worker-and-four-seams.md)), it is attached by a route pattern on the same host, not published on `api.<host>` or any other origin.
2. `EXPO_PUBLIC_SERVER_URL` is the one server origin of the native app, and its production value never changes for a shipped app. No second origin variable is added for the API.
3. Web requests to the backend stay same-origin. The CSRF guard keeps refusing `sec-fetch-site: same-site` and `cross-site` writes (`apps/web/src/platform/rpc/request-origin.server.ts`), the session cookie stays host-only, and no credentialed CORS is configured for the app's own clients.

## Consequences [#consequences]

Splitting the API later costs the native app nothing: no release, no proxy for old binaries, no cache invalidation. Cookies, `trustedOrigins` and the CSRF guard need no change either. The price is that the API cannot be scaled, cached or rate-limited by hostname; route patterns and bindings do that work instead. A project that must expose a public, versioned API on its own host adds it as a second surface with token authentication and leaves `/api/*` for its own clients where it is.

Nothing checks this decision mechanically; a second origin would show up as a new `EXPO_PUBLIC_*` variable in `packages/env/src/native.ts` or as CORS configuration, both visible in review.

## Alternatives [#alternatives]

- `api.<host>` from the start. It needs cross-subdomain cookies, credentialed CORS and a relaxed same-site refusal today, for a split that may never happen.
- Decide when splitting. By then binaries with the old origin are installed, and the old host has to proxy the API forever.
