BFF Proxy Routes
Every browser request goes through one of these routes. Most are a 4-line
delegation to proxyGet / proxyPost / proxyPut / proxyPatch /
proxyDelete / proxySse from src/lib/api/proxy.ts. No business logic
lives here — the proxy exists only to keep credentials off the browser and
collapse two upstreams to one origin (see
ARCHITECTURE.md).
Exception: the two manifest routes below use proxyGetVerified, not
proxyGet. They forward the upstream body unchanged but run it through
the aitp SDK server-side first, attaching the result as a sibling
_verification key ({checked, ok, code} / {checked: false, reason}) —
never re-serializing the upstream bytes, so the response the browser sees
is byte-identical to what was verified. Both
(well-known/aitp-manifest, registry/agents/[aid]/manifest) verify with
verifyManifestJson. This is the one case in this
table with real logic in the proxy layer: rendering a CP-signed artifact as
trusted without checking it is exactly the defect
plans/cp-signed-artifact-verification.md exists to fix. See
ARCHITECTURE.md.
This table is the console's routing contract: console route → upstream path. The meaning of each upstream endpoint is owned by its service — follow the links to the playground and CP API docs for request/response semantics.
Status codes and bodies pass through unchanged. On connection failure the
proxy synthesizes a 502 (Upstream unreachable) and on timeout a
504 (Upstream timeout), both with shape
{ error, target, upstream_status }. target is the upstream path and
query only (e.g. /api/health): the upstream origin never reaches the
browser. Raw error detail and the full upstream URL are logged
server-side, never sent to the browser.
Playground (PLAYGROUND_URL, default http://localhost:8000)
| Method | Console route | Upstream | Access |
|---|---|---|---|
| GET | /api/playground/health | /healthz | public |
| GET | /api/playground/capabilities | /capabilities | public |
| GET | /api/playground/agents | /agents | public |
| GET | /api/playground/metrics | /metrics | admin |
| GET | /api/playground/packs | /packs | public |
| GET | /api/playground/scenarios | /scenarios | public |
| GET | /api/playground/scenarios/[...ref] | /scenarios/<pack>/<scenario>@<version> | public |
| GET | /api/playground/scenario-templates?ref=… | /scenarios/<pack>/<scenario>@<version>/templates | public |
| GET | /api/playground/runs | /runs | public |
| POST | /api/playground/runs | /runs (accepts template, variant, fault_injection) | public (launch) |
| GET | /api/playground/runs/[id] | /runs/:id | public |
| GET | /api/playground/runs/[id]/status | /runs/:id/status | public |
| POST | /api/playground/runs/[id]/cancel | /runs/:id/cancel | admin |
| GET | /api/playground/runs/[id]/narrate | /runs/:id/narrate | public |
| GET | /api/playground/runs/[id]/cp-audit | /runs/:id/cp-audit | public |
| GET | /api/playground/runs/[id]/cp-sessions | /runs/:id/cp-sessions | public |
| GET | /api/playground/runs/[id]/deliveries | /runs/:id/cp-deliveries | public |
| SSE | /api/playground/runs/[id]/events | /runs/:id/events | public (stream) |
| GET, POST | /api/playground/hosted-agents | /hosted-agents | GET public · POST admin |
| GET, DELETE | /api/playground/hosted-agents/[id] | /hosted-agents/:id | GET public · DELETE admin |
| POST | /api/playground/hosted-agents/[id]/invoke | /hosted-agents/:id/invoke | admin |
| POST | /api/playground/hosted-agents/[id]/resolve-and-handshake | /hosted-agents/:id/resolve-and-handshake | admin |
Why
scenario-templatesis a query-param route, not a child of/scenarios/[...ref]: Next.js does not allow child segments under a catch-all route. The console keeps a flat/api/playground/scenario-templates?ref=<pack>/<scenario>@<version>route and forwards it to the nested upstream/templatespath.
Control Plane (CP_URL, default http://localhost:4000)
| Method | Console route | Upstream | Access |
|---|---|---|---|
| GET | /api/cp/health | /api/health | public |
| GET | /api/cp/readyz | /api/readyz | public |
| GET | /api/cp/dashboard | /api/dashboard/overview | public |
| GET | /api/cp/dashboard/agents | /api/dashboard/agents | public |
| GET | /api/cp/registry/agents | /api/registry/agents | public |
| POST | /api/cp/registry/enroll | /api/registry/enroll | admin |
| GET | /api/cp/registry/agents/[aid] | /api/registry/agents/:aid | public |
| DELETE | /api/cp/registry/agents/[aid] | /api/registry/agents/:aid | admin |
| GET | /api/cp/registry/agents/[aid]/manifest | /api/registry/agents/:aid/manifest (verifying — see note above) | public |
| SSE | /api/cp/events/stream | /api/events/stream (cap-aware) | public (stream) |
| GET | /api/cp/events/history | /api/events/history | public |
| GET | /api/cp/sessions | /api/sessions | public |
| GET | /api/cp/sessions/[sessionId] | /api/sessions/:sessionId | public |
| GET | /api/cp/sessions/[sessionId]/replay | /api/sessions/:sessionId/replay | public |
| GET | /api/cp/sessions/[sessionId]/export | /api/sessions/:sessionId/export | public |
| GET | /api/cp/audit | /api/audit (admin-action log; the Audit screen uses events/history above) | admin |
| GET | /api/cp/metrics | /api/metrics | admin |
| GET | /api/cp/tcts | /api/tcts | public |
| GET | /api/cp/delegations | /api/delegations | public |
| POST | /api/cp/revocation/entries | /api/revocation/entries (POST-only; the body carries iss) | admin |
| GET | /api/cp/revocation/issuers | /api/revocation/issuers (issuers that have entries, with counts) | public |
| GET | /api/cp/revocations | /api/revocations?iss=<aid> (one issuer's entries, unsigned) | admin |
| GET, POST | /api/cp/trust-anchors | /api/trust-anchors | GET public · POST admin |
| GET, PATCH, DELETE | /api/cp/trust-anchors/[id] | /api/trust-anchors/:id | GET public · PATCH, DELETE admin |
| GET, POST, DELETE | /api/cp/pinned-keys | /api/pinned-keys (DELETE takes ?namespace=&aid=) | GET public · POST, DELETE admin |
| GET, POST | /api/cp/webhooks | /api/webhooks | admin |
| PUT, DELETE | /api/cp/webhooks/[id] | /api/webhooks/:id | admin |
| GET, POST | /api/cp/webhooks/[id]/circuit-breaker | /api/webhooks/:id/circuit-breaker | admin |
| POST | /api/cp/webhooks/[id]/circuit-breaker/reset | /api/webhooks/:id/circuit-breaker/reset | admin |
| GET | /api/cp/well-known/aitp-manifest | /.well-known/aitp-manifest (verifying — see note above) | public |
Adding a new proxy
- Create the file under
src/app/api/<service>/<path>/route.ts. - Pick the right helper:
import { NextRequest } from 'next/server'; import { proxyGet } from '@/lib/api/proxy'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; export async function GET(req: NextRequest) { return proxyGet('cp', '/api/your/path', req); } - For routes with path params, await
params(Next 16 passes it as a Promise) andencodeURIComponentuser-controlled segments. - For SSE, use
proxySseand make sure the upstream setsContent-Type: text/event-stream. The proxy auto-rewritesCache-ControlandX-Accel-Buffering. 4a. If the route serves a CP-signed artifact, useproxyGetVerifiedinstead ofproxyGet, passing averify(bodyText) => Verdictfunction. Never verify in the browser — write the verify function in a small dedicated module the route imports (seesrc/lib/api/verify-manifest.ts), not inline in the route, so unit tests underjsdom(which cannot load a native addon) never transitively import it. - Add a row to this table.
- Add a hit to
src/test/proxies.integration.test.ts(live services) and a mock-upstream case tosrc/test/bff-routes.integration.test.ts(runs in CI) so the contract is exercised end-to-end.
The full "add a route end-to-end" recipe (proxy → types → hook →
component → page → tests) lives in the repo's contributor docs:
internal_docs/DEVELOPMENT.md
(not published here).
Auth
The route handlers build every upstream request's headers from scratch;
nothing the browser sends (Authorization, x-api-key, Cookie,
X-AITP-Client-IP) is forwarded. The keys never reach the browser — they
live only in the route-handler process.
| Upstream | Credential | When |
|---|---|---|
| Playground | x-api-key: <PLAYGROUND_API_KEY> | every call, when set |
Control plane, keyless routes (/api/health, /api/readyz, the manifest) | none | always |
| Control plane, read routes (registry, sessions, events, trust, revocation issuers, …) | Authorization: Bearer <CP_READ_API_KEY> | when set; otherwise CP_API_KEY |
| Control plane, admin routes (audit, webhooks, metrics, every write) | Authorization: Bearer <CP_API_KEY> | always |
Which class a route is in comes from the allowlist in
src/lib/server/allowlist.ts; a path not in it is treated as admin.
Access column. With AITP_CONSOLE_PUBLIC_MODE off (the default), every
route needs an operator session, whatever the column says. With it on,
public routes also serve anonymous visitors, charged against the per-IP
limits (read; launch adds the 5-per-10-minutes run budget; stream
takes one of 4 concurrent stream slots per IP); admin routes still need an
operator session (anonymous → 403 CONSOLE_OPERATOR_SESSION_REQUIRED). See
ARCHITECTURE.md § Access model.
Read-key fallback. With public mode on, an operator's read routes use
CP_API_KEY directly, so operators never share the read key's budget with
visitors. With it off, for an operator, a read route falls back to
CP_API_KEY, retrying once, when CP_READ_API_KEY is unset, when the CP
answers 401, or when it answers 403 with {"code":"READ_ONLY_KEY"}. Each
fallback reason logs one console.warn line at most every 10 minutes. Any
other 403 is relayed unchanged. An anonymous caller (only possible with
AITP_CONSOLE_PUBLIC_MODE=on) never gets the admin key: an unset or rejected
read key answers 503 cp_read_key_unavailable.
Client IP. Every upstream call carries X-AITP-Client-IP when the
console knows the caller's IP (on Vercel, from x-real-ip; elsewhere, from
the header named by CONSOLE_CLIENT_IP_HEADER). When it doesn't, the
header is omitted rather than guessed.
Error metadata. A non-2xx upstream response is relayed with its status,
body, Content-Type and Retry-After, including on the SSE routes, so a
playground 429 reaches the browser intact.
Leave both blank for local development; both services accept
unauthenticated requests when their own API_KEYS are unset.
SSE behaviour
proxySse streams the upstream Response.body straight back to the
browser with three header overrides:
Content-Type: text/event-streamCache-Control: no-cache, no-transformX-Accel-Buffering: no(disables nginx response buffering if a proxy ever fronts the console)
There is no per-frame parsing in the proxy — frames are forwarded as
opaque bytes. Browser-side parsing happens in useSse.
CP SSE capacity handling
The control plane caps live SSE listeners and returns 503 when at
capacity. The client useSse hook takes an optional capacityProbePath
— when supplied it issues a quick GET to that path before opening the
EventSource. A 503 surfaces as a distinct at-capacity state so the UI
can display a "control plane at capacity" banner rather than the generic
"reconnecting" state, with a longer backoff. The CP-side cap and its 503
behaviour are documented in the
CP operations docs.
Enrollment-token flow
POST /api/cp/registry/enroll takes a signed AITP manifest envelope and
returns a short-lived, single-use bearer token. The console surfaces this
in Registry → New enrollment with an expiry countdown and a
reveal/copy control — see FEATURES.md.
The token's lifetime, JTI single-use semantics, and redemption against
POST /api/registry/agents are the control plane's; see the
CP API.