09 · Docs / OpenAPI
Auto-OpenAPI 3.1 from a running app
One function call generates the spec by reflecting on the router. No DSL, no annotations.
Code
hono-openapi.js
const { WebApp, generateOpenAPI31 } = require(class="tk-str">39;@hyperbridge/forge/server39;); const app = new WebApp(); app.get(class="tk-str">39;/health39;, (c) => c.json({ ok: true })); app.get(class="tk-str">39;/users/:id39;, (c) => c.json({ id: c.req.param(class="tk-str">39;id39;) })); app.post(class="tk-str">39;/users39;, (c) => c.json({ created: true }, 201)); app.get(class="tk-str">39;/openapi.json39;, (c) => c.json( generateOpenAPI31(app, { title: class="tk-str">39;My API39;, version: class="tk-str">39;1.0.039;, description: class="tk-str">39;Generated from WebApp routes39;, }) )); app.listen(3000);
How it works
generateOpenAPI31(app, info) walks the trie, emits one operation per (method, path) pair, and attaches schemas if you've registered them.
Use it at runtime (live /openapi.json endpoint) or at build time (CI step writes the spec to a file for git tracking).
Compatible with the entire OpenAPI 3.1 ecosystem: Scalar, ReDoc, Swagger UI, Postman, Stoplight.
Try it
Quickstart
curl http://localhost:3000/openapi.json | jq
Related modules
This is HBForge's port of Hono's example. Read the original at hono.dev/examples/hono-openapi.