Skip to main content
The fastify plugin generates type-safe route handler definitions for Fastify servers, ensuring your route handlers match your OpenAPI specification at compile time.

Installation

Install OpenAPI TypeScript:
The Fastify plugin generates types only. You’ll need the fastify package for your runtime server.

Configuration

Add the fastify plugin to your OpenAPI TypeScript configuration:
openapi-ts.config.ts

What Gets Generated

The plugin generates a RouteHandlers type that maps each operation to a Fastify RouteHandler type with proper typing for:
  • Request body (Body)
  • Query parameters (Querystring)
  • Path parameters (Params)
  • Headers (Headers)
  • Response data (Reply)

Example Generated Code

Given this OpenAPI operation:
openapi.yaml
The plugin generates:
fastify.gen.ts

Usage

Implement your route handlers with full type safety:
handlers.ts

Integration with Fastify

Use the generated types with popular Fastify plugins like fastify-openapi-glue:
server.ts

Type Safety Benefits

Request Parameter Typing

The plugin extracts parameter types from your OpenAPI spec:

Response Type Safety

Return types are validated against your API spec:

Error Response Typing

Error responses from your OpenAPI spec are included in the Reply type:

Configuration Options

The fastify plugin has minimal configuration:
boolean
default:false
Whether to export the RouteHandlers type from the main index.ts entry file.
openapi-ts.config.ts

Complete Example

1

Define your OpenAPI specification

openapi.yaml
2

Configure OpenAPI TypeScript

openapi-ts.config.ts
3

Generate types

4

Implement handlers

src/handlers.ts
5

Create server

src/server.ts

Generated Code Structure

The plugin generates a single file:

Dependencies

The fastify plugin declares a dependency on @hey-api/typescript for type generation:

How It Works

For each operation in your OpenAPI spec, the plugin:
  1. Extracts operation metadata - Reads operationId, parameters, request body, and responses
  2. Maps to Fastify types - Converts OpenAPI parameters to Fastify’s type system:
    • parameters[in=path] → Params
    • parameters[in=query] → Querystring
    • parameters[in=header] → Headers
    • requestBody → Body
    • responses → Reply
  3. Generates handler signature - Creates a RouteHandler<T> type with all request/response types
  4. Combines into RouteHandlers - Exports a single type mapping operation IDs to handler types

Best Practices

Use with fastify-openapi-glue - This plugin works perfectly with fastify-openapi-glue to automatically route requests to your handlers based on the OpenAPI spec.
1

Keep handlers and spec in sync

Run npx @hey-api/openapi-ts after updating your OpenAPI specification to regenerate types.
2

Leverage TypeScript strict mode

Enable strict: true in tsconfig.json to catch type mismatches at compile time.
3

Use operationId consistently

Ensure every operation in your OpenAPI spec has a unique operationId - this becomes your handler function name.
4

Handle all response types

The Reply type includes all possible responses from your spec. Handle success and error cases appropriately.

Limitations

  • The plugin generates types only, not runtime validation
  • Default responses are omitted from the Reply type to avoid overly broad types
  • Only operations with operationId are included in the RouteHandlers type

Migration from Untyped Handlers

Before:
After:
TypeScript will immediately flag any mismatches between your handlers and your OpenAPI specification.