Part V — The Modern Experience Layer · Chapter 28 · Lesson 3 of 3
OpenAPI 3.0 generation and typed SDKs
The last serious ergonomic gap in hand-rolled APIs is the spec drifting away from the routes. PeaLang closes it: the spec is generated from the app object you already have.
Generate the spec
let spec = web.openapi(app, {
"title": "Orders API",
"version": "1.2.0",
"description": "Internal order service"
})
web.openapi walks the registered routes — paths, methods, path parameters, response helpers with known codes — and emits an OpenAPI 3.0 document. Path params in :id form become OpenAPI {id} form automatically; route groups appear as path prefixes; static and 404 routes are excluded.
Serve it
app.get("/openapi.json", fn(req) {
return web.json(spec)
})
Or use the CLI: pea openapi (and its alias web-openapi) generate the document from a source file without running the service — ideal for CI.
From spec to typed clients
The code generators read the same route table or the emitted spec:
generate_typescript_client(spec)— a fetch-based, fully typed client.generate_react_hooks(spec)—useOrders()-style hooks with loading and error states (v1.4.0).sse_endpoint_source/websocket_handler_source— streaming endpoints for live dashboards (v1.5.0).jwt_middleware_source,rate_limiter_source,api_versioning_source— the glue you would otherwise copy-paste into every service.
Versioning without pain
app.group("/v1") → /v1/orders
app.group("/v2") → /v2/orders
api_versioning_source generates the router sketch for header- or path-based versioning. Pick path-based; header-based versioning is a liability in cached proxies and support tickets alike.
Guided practice
- Generate
openapi.jsonfrom a two-route app and validate it with any OpenAPI validator; note what the generator could not infer (response schemas you should add by hand). - Generate a TypeScript client and call one endpoint from a scratch script; check the types flow (try a wrong id type — your editor should complain).
- Add a breaking change on a v2 route group and confirm both specs emit cleanly side by side.
Rule of thumb. If your spec is written by hand, it is already wrong. Generate it, diff it in CI, and treat it as a build artifact.