@fastify/swagger
Generates an OpenAPI specification from route schemas.
Syntax
app.register(swagger, { openapi }) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | object | No | OpenAPI document info and mode (dynamic or static). |
Returns
Promise<void> — Resolves when the plugin is registered.
Examples
import Fastify from 'fastify';
import swagger from '@fastify/swagger';
import swaggerUi from '@fastify/swagger-ui';
const app = Fastify();
await app.register(swagger, {
openapi: { info: { title: 'Cats API', version: '1.0.0' } },
});
await app.register(swaggerUi, { routePrefix: '/docs' });
app.get('/cats', {
schema: {
response: { 200: { type: 'array', items: { type: 'string' } } },
},
}, async () => ['kitty']);
Notes
The spec is generated from each route's JSON Schema, so define request
and response schemas. Pair with @fastify/swagger-ui to serve interactive
documentation.