HBForge/Examples
08 · Docs / OpenAPI

Zod-style schemas → OpenAPI 3.1

HBForge's schema builder is Zod-compatible by API — generateOpenAPI31() walks the router and produces a full spec from it.

forge/serverforge/schemaNodeBunDenoCF Workers
Code
zod-openapi.js
const {
  WebApp, ObjectSchema, StringSchema, NumberSchema,
  generateOpenAPI31, swaggerUI,
} = require(class="tk-str">'@hyperbridge/forge/server');

const app  = new WebApp();
const Post = new ObjectSchema({
  id:    new NumberSchema().int(),
  title: new StringSchema().min(1),
});

app.get(class="tk-str">'/posts/:id', (c) => c.json({
  id: +c.req.param(class="tk-str">'id'),
  title: class="tk-str">'hello',
}));

app.get(class="tk-str">'/openapi.json', (c) => c.json(
  generateOpenAPI31(app, {
    title:   class="tk-str">'Posts API',
    version: class="tk-str">'1.0',
  })
));
app.get(class="tk-str">'/docs', (c) => c.html(
  swaggerUI({ url: class="tk-str">'/openapi.json' })
));

app.listen(3000);
How it works

No decorators, no plugin chain — schemas attached to routes via metadata produce a spec automatically.

Output is OpenAPI 3.1 (the JSON Schema 2020-12 dialect), which is the format Scalar, ReDoc, and modern Swagger UI all consume.

Saves the spec to disk by piping the JSON to fs.writeFileSync — useful for git-tracked, diffable API contracts.

Try it
Quickstart
open http://localhost:3000/docs
Related modules