All framework guides

Node.js (JavaScript or TypeScript)

Get an OpenAPI spec from Fastify

Register @fastify/swagger before your routes. The spec is served at /documentation/json.

Tool
@fastify/swagger
Time
About 5 minutes
You get
Live at /documentation/json, plus an optional openapi.json file

How it works

@fastify/swagger collects every route you register after it, and uses each route's schema (body, querystring, params, response) for the inputs.

Routes without a schema still appear with their path and method, but without fields.

Step by step

  1. Step 1: Install the plugins

    @fastify/swagger builds the spec. @fastify/swagger-ui serves it and a browsable page.

    Terminal
    npm install @fastify/swagger @fastify/swagger-ui
  2. Step 2: Register them before your routes

    Order matters: routes registered before @fastify/swagger are not included.

    server.js
    const fastify = require('fastify')({ logger: true });
    
    async function start() {
      await fastify.register(require('@fastify/swagger'), {
        openapi: {
          info: { title: 'My API', description: 'What this API does.', version: '1.0.0' },
          servers: [{ url: 'https://api.example.com' }],
        },
      });
      await fastify.register(require('@fastify/swagger-ui'), { routePrefix: '/documentation' });
    
      // your routes go here, after the two plugins
      fastify.register(require('./routes/bookings'));
    
      await fastify.listen({ port: 3000 });
    }
    start();

    Using ES modules? Write import swagger from '@fastify/swagger' and import swaggerUi from '@fastify/swagger-ui', then register(swagger, {...}) and register(swaggerUi, {...}).

  3. Step 3: Give routes a schema (recommended)

    This is how fields, types and required inputs get into the spec.

    routes/bookings.js
    module.exports = async function (fastify) {
      fastify.post('/bookings', {
        schema: {
          summary: 'Create a booking',
          description: 'Books an appointment in a free slot.',
          body: {
            type: 'object',
            required: ['service_id', 'start_time'],
            properties: {
              service_id: { type: 'string' },
              start_time: { type: 'string', format: 'date-time' },
            },
          },
        },
      }, async (request, reply) => {
        // ...
      });
    };
  4. Step 4: Get the spec

    Start the app and open /documentation/json. On your live server: https://api.example.com/documentation/json.

    To save a file instead, add these lines after all routes are registered (for example right before fastify.listen):

    server.js
    await fastify.ready();
    require('fs').writeFileSync('openapi.json', JSON.stringify(fastify.swagger(), null, 2));
    Terminal
    curl http://localhost:3000/documentation/json -o openapi.json

Check that it worked

  • Open http://localhost:3000/documentation/json. Every route should be under "paths".
  • Routes with a schema should show their fields.

Give it to MCPEasy

Have a link? Paste it (best)

In MCPEasy, create a server, choose OpenAPI URL and paste the link, for example https://api.example.com/openapi.json. Later changes to your API can be picked up with a sync.

Have a file? Upload it

Choose Upload file and drop in the .json or .yaml file (up to 5 MB). MCPEasy turns each endpoint into a tool, and you review them all before anything goes live.

If something goes wrong

Some routes are missing.
They were registered before @fastify/swagger. Move the two register calls above your routes, and await them.
Routes have no fields.
Add a schema to those routes (step 3). Fastify cannot see fields you read in the handler without one.

Still stuck, or no time to set this up? In MCPEasy choose From code (GitHub): it reads your routes straight from the repository, read-only, and tests every tool against your live API before it goes live.

Official documentation

Other frameworks