πŸ“… You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP β†’
Socket
Sign inDemoInstall
Socket

@fastify/swagger

Package Overview
Dependencies
Maintainers
19
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/swagger

Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation

9.5.0
latest
Source
npm
Version published
Maintainers
19
Created

What is @fastify/swagger?

@fastify/swagger is a Fastify plugin that integrates Swagger/OpenAPI documentation generation and UI into Fastify applications. It allows developers to automatically generate API documentation from their Fastify routes and provides a user-friendly interface to explore and test the API endpoints.

What are @fastify/swagger's main functionalities?

Generate OpenAPI Documentation

This feature allows you to generate OpenAPI documentation for your Fastify routes. The code sample demonstrates how to set up the @fastify/swagger plugin and define a route with a schema that will be included in the generated documentation.

const fastify = require('fastify')();
const swagger = require('@fastify/swagger');

fastify.register(swagger, {
  routePrefix: '/documentation',
  swagger: {
    info: {
      title: 'Test API',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    },
    host: 'localhost:3000',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json']
  },
  exposeRoute: true
});

fastify.get('/example', {
  schema: {
    description: 'Example endpoint',
    tags: ['example'],
    summary: 'An example endpoint',
    response: {
      200: {
        description: 'Successful response',
        type: 'object',
        properties: {
          hello: { type: 'string' }
        }
      }
    }
  }
}, async (request, reply) => {
  return { hello: 'world' };
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Serve Swagger UI

This feature allows you to serve a Swagger UI for your API documentation. The code sample demonstrates how to set up the @fastify/swagger and @fastify/swagger-ui plugins to serve the Swagger UI at the '/docs' route.

const fastify = require('fastify')();
const swagger = require('@fastify/swagger');
const swaggerUi = require('@fastify/swagger-ui');

fastify.register(swagger, {
  swagger: {
    info: {
      title: 'Test API',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    }
  }
});

fastify.register(swaggerUi, {
  routePrefix: '/docs',
  swagger: {
    info: {
      title: 'Test API',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    }
  },
  exposeRoute: true
});

fastify.get('/example', async (request, reply) => {
  return { hello: 'world' };
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/swagger

Keywords

fastify

FAQs

Package last updated on 13 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts