@fastify/swagger
OpenAPI/Swagger documentation plugin for Fastify — auto-generate API specs from your route schemas with zero extra config.
@fastify/swagger
FastifyOpenAPI/Swagger documentation plugin for Fastify — auto-generate API specs from your route schemas with zero extra config.
Fit
Bundle (gzip)
8.4 kB
25.0 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Keeping API documentation in sync with your actual route implementations requires manual maintenance. In Fastify, route schemas already describe request/response shapes — wiring these to OpenAPI output should be automatic.
What It Does
@fastify/swagger reads the JSON Schema definitions you already write for Fastify routes and converts them into an OpenAPI 3 spec. Pair it with @fastify/swagger-ui to serve interactive documentation at /documentation. Zero extra configuration when schemas are already defined.
Installation
npm install @fastify/swagger @fastify/swagger-uiUsage Example
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: 'My API', version: '1.0.0' } },
});
await app.register(swaggerUi, { routePrefix: '/docs' });
app.get('/users/:id', {
schema: {
params: { type: 'object', properties: { id: { type: 'string' } } },
response: { 200: { type: 'object', properties: { name: { type: 'string' } } } },
},
}, async (req) => ({ name: 'Alice' }));Related packages
Auto-load Fastify plugins from a directory — organize routes and plugins as files and let the framework discover them automatically.
@fastify/cookieFastifyPlugin for Fastify that adds support for reading and setting cookies, including signed cookies.
@fastify/corsFastifyCORS plugin for Fastify — configure cross-origin resource sharing with full support for preflight requests and dynamic origins.
@fastify/helmetFastifySecurity headers plugin for Fastify — sets HTTP headers that protect against common web vulnerabilities like XSS, clickjacking, and MIME sniffing.
@fastify/jwtFastifyJWT authentication plugin for Fastify — sign, verify, and decode tokens with a decorator-based API that integrates with Fastify's request lifecycle.