HBForge/Examples
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.

forge/serverNodeBunDenoCF Workers
Code
hono-openapi.js
const { WebApp, generateOpenAPI31 } =
  require(class="tk-str">'@hyperbridge/forge/server');

const app = new WebApp();

app.get(class="tk-str">'/health',     (c) => c.json({ ok: true }));
app.get(class="tk-str">'/users/:id',  (c) => c.json({ id: c.req.param(class="tk-str">'id') }));
app.post(class="tk-str">'/users',     (c) => c.json({ created: true }, 201));

app.get(class="tk-str">'/openapi.json', (c) => c.json(
  generateOpenAPI31(app, {
    title:       class="tk-str">'My API',
    version:     class="tk-str">'1.0.0',
    description: class="tk-str">'Generated from WebApp routes',
  })
));

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