Fastify

Install

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

Setup

// server.ts
import Fastify from 'fastify'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUi from '@fastify/swagger-ui'

const fastify = Fastify()

await fastify.register(fastifySwagger, {
  openapi: {
    info: {
      title: 'My API',
      version: '1.0.0',
    },
  },
})

await fastify.register(fastifySwaggerUi, {
  routePrefix: '/docs',
})

fastify.get(
  '/items/:id',
  {
    schema: {
      operationId: 'getItem',
      summary: 'Get a single item',
      params: {
        type: 'object',
        properties: {
          id: { type: 'integer' },
        },
      },
      response: {
        200: {
          type: 'object',
          properties: {
            id: { type: 'integer' },
            name: { type: 'string' },
          },
        },
      },
    },
  },
  async (request) => {
    const { id } = request.params as { id: number }
    return { id, name: 'Widget' }
  }
)

await fastify.ready()
await fastify.listen({ port: 3000 })

The raw spec is available at:

http://localhost:3000/docs/json

Expose at /openapi.json

Add a route that serves the spec at a clean path:

fastify.get('/openapi.json', async () => {
  return fastify.swagger()
})

Make it publicly accessible

Deploy your app or use a tunnel for local testing:

ngrok http 3000
# then use: https://<random>.ngrok.io/openapi.json

Verify

Visit /openapi.json in your browser. You should see a valid OpenAPI 3.x JSON document.

Paste this URL into Automiel’s import step.