Skip to main content

OpenAPI

This is guide to serve the document generated by @ts-rest/open-api through swagger ui with @fastify/swagger and @fastify/swagger-ui.

Document generated by @ts-rest/open-api is in json format that can be serve as static asset without swagger ui.

Installation

pnpm add @fastify/swagger @fastify/swagger-ui

Generating a OpenAPI Document

import { myContract } from './my-api';
import { generateOpenApi } from '@ts-rest/open-api';

const openApiDocument = generateOpenApi(myContract, {
info: {
title: 'Posts API',
version: '1.0.0',
},
});

See @ts-rest/open-api for more information.

Register fastify plugin

import fastify from 'fastify'
import fastifySwagger from '@fastify/swagger'
import fastifySwaggerUI from '@fastify/swagger-ui'

import { openApiDocument } from './document'

const app = fastify()
.register(fastifySwagger, {
transformObject: () => openApiDocument
})
.register(fastifySwaggerUI)