Skip to Content
hono-bento

OpenAPI

Generate an OpenAPI 3.1 document from the same specs you mount.

generate-openapi.ts
import { writeFileSync } from 'node:fs';

import { generateOpenApiDocument } from 'hono-bento/openapi';

import { specs } from './specs';

const document = generateOpenApiDocument(specs, {
  info: { title: 'Todos API', version: '1.0.0' },
  servers: [{ url: 'https://api.example.com' }],
  securitySchemes: {
    bearerAuth: { type: 'http', scheme: 'bearer', bearerFormat: 'JWT' },
    apiKeyAuth: { type: 'apiKey', in: 'header', name: 'X-Api-Key' },
  },
  // Which scheme each strategy implements, so each route lists only the ones it accepts.
  securityByStrategy: { jwt: 'bearerAuth', apiKey: 'apiKeyAuth' },
});

writeFileSync('openapi.json', `${JSON.stringify(document, null, 2)}\n`);

Commit the output and check it in CI. Then a spec change that alters the public API shows up in review as a diff to openapi.json.

What's generated

  • Paths come from each route's full path, with :id and :id{[0-9]+} converted to {id}. Path parameters are documented from validation.params, or as strings.
  • Operation ids are <collection>_<operation>, such as todos_create.
  • Request bodies come from validation.body, and query parameters from validation.query. Bodies validated strictly (the default) are documented with additionalProperties: false. A contentType: 'form-data' body is documented as multipart/form-data.
  • Responses come from responses, with their Zod schemas. Each route also lists the framework's own errors, using a shared ErrorResponse schema: 400 when it validates input, 401 when it requires authentication, 403 when it requires scopes. Statuses the route documents itself are kept. A response without a schema is documented by its content: an object for JSON, a string for text (text/csv), and binary data for anything else (application/pdf, image/*), which documentation tools offer as a download. A stream is described item by item, with itemSchema: each server-sent event ({ event, data, id?, retry? }) or JSON line, and the error sent if it fails once started. itemSchema is OpenAPI 3.2, so pass openapi: '3.2.0' for an API with streams.
  • Tags default to the spec's name.
  • Security applies to every route that isn't allowAnonymous, including routes without auth. With securityByStrategy, a route limited to auth: { strategies: ['jwt'] } lists only bearerAuth; without it, every route lists every scheme.
  • Components. Any schema with .openapi('Name') becomes components.schemas.Name. Each spec's main object is also registered: the 200 schema of its retrieve route, named from its object literal or the spec's model.

Options

Prop

Type

Splitting documents

Set group on specs and call generateOpenApiDocumentsByGroup(specs, options) to get one document per group, such as a public API and an internal one. Specs without a group are collected under default.

Serving docs

Serve the document from the app and point a viewer such as Scalar at it:

const document = generateOpenApiDocument(specs, openApiOptions);
app.get('/openapi.json', (c) => c.json(document));

On this page