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 thefastify plugin to your OpenAPI TypeScript configuration:
openapi-ts.config.ts
What Gets Generated
The plugin generates aRouteHandlers 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
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 likefastify-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 theReply type:
Configuration Options
Thefastify 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
Thefastify plugin declares a dependency on @hey-api/typescript for type generation:
How It Works
For each operation in your OpenAPI spec, the plugin:- Extracts operation metadata - Reads
operationId, parameters, request body, and responses - Maps to Fastify types - Converts OpenAPI parameters to Fastify’s type system:
parameters[in=path]→Paramsparameters[in=query]→Querystringparameters[in=header]→HeadersrequestBody→Bodyresponses→Reply
- Generates handler signature - Creates a
RouteHandler<T>type with all request/response types - 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
Replytype to avoid overly broad types - Only operations with
operationIdare included in theRouteHandlerstype