How the API Worker receives, dispatches, validates, and responds to browser and internal-service requests.
Every request enters src/index.ts. The Worker first associates the request
with its Cloudflare ExecutionContext so Better Auth background work can use
waitUntil. handleWorkerRequest then handles preflight policy and contains
uncaught errors.
src/config/client.config.ts./api/auth and every path below it before the application
route table is consulted.405; an unknown path is
404.500.
Provider details and secrets are not returned to the browser.The exact public route families remain in
Route reference; src/routes/index.ts is the final
authority when routes change.
A service function normally performs these steps in order:
Moving authorization after a provider call would expose timing and consume provider capacity for a request that should have been rejected locally.
Bounded list endpoints use the helpers in src/pagination/cursor.ts. The
cursor is an opaque, versioned base64url payload containing the last sort key.
The parser bounds page size and rejects malformed cursors. The paginator sorts
by a stable key, returns only the requested window, and emits the next cursor
when more data remains. Callers must treat cursors as opaque; changing their
payload is a versioned contract change.
callGithubApp adds the internal secret and a correlation ID, applies a
10-second default timeout, and prefers the private service binding when one is
present. It recognizes versioned and plain error bodies returned by the
service, then maps transport failures to:
| Condition | API response |
|---|---|
| upstream timeout | 504 with github_app_timeout |
| network or binding failure | 502 with github_app_unavailable |
| non-JSON successful response | 502 with github_app_invalid_response |
| upstream non-success | same status with a sanitized message and code |
Application handlers never expose GITHUB_APP_INTERNAL_SECRET or construct
GitHub credentials.
When adding a route, update the route table, validate at the service boundary, reuse a shared schema when data crosses workspaces, add success and rejection characterization tests, and update API contracts in the same commit.