import { InstallTabs } from '@site/src/components/InstallTabs';

# 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

<InstallTabs packageName="@fastify/swagger @fastify/swagger-ui" />

## Generating a OpenAPI Document

```typescript
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`](/open-api.mdx) for more information.

## Register fastify plugin

```typescript
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)
```
