@fastify/swagger

OpenAPI/Swagger documentation plugin for Fastify — auto-generate API specs from your route schemas with zero extra config.

@fastify/swagger

Fastify

OpenAPI/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

bash
npm install @fastify/swagger @fastify/swagger-ui

Usage Example

typescript
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