Swagger/OpenAPI generation for TypeScript: Complete guide to tools
Developing modern web applications in TypeScript is inextricably linked to creating type-safe API clients. Manually writing interfaces for each endpoint is a time-consuming and error-prone task. Fortunately, there is a powerful approach — automatic generation of TypeScript code from OpenAPI (Swagger) specifications. In this article, we will take a detailed look at the best tools for this task, their capabilities, and usage examples.
Why use generation from OpenAPI?
OpenAPI (formerly Swagger) is a standard for describing REST APIs. Using a YAML or JSON specification file, you get:
- 100% typing — TypeScript interfaces that exactly match the request and response schemas
- Backend synchronization — when the API changes, just regenerate the code
- Reduced errors — the TypeScript compiler catches type mismatches at development time
- IDE autocompletion — IntelliSense suggests fields and types
Top tools for generating TypeScript from OpenAPI
1. openapi-typescript-codegen (recommended)
This tool has gained popularity due to its simplicity and modern approach. It generates clean, well-structured code with support for axios or fetch.
Installation:
npm install -D openapi-typescript-codegenUsage example:
npx openapi-typescript-codegen --input ./spec.yaml --output ./generated --client axiosGenerated code:
// generated/models/User.tsexport interface User { id: number; name: string; email: string; role: 'admin' | 'user';}
// generated/services/UserService.tsexport class UserService { public async getUsers(): Promise<User[]> { return this.httpRequest.request({ method: 'GET', url: '/users', }); }}Advantages:
- Support for OpenAPI 3.0 and 3.1
- Generation of models, services, and types for requests
- Optional generation of hooks for React Query
- Customizable templates via Handlebars
2. OpenAPI Generator (openapi-generator)
The official generator from the OpenAPI community. Supports over 50 languages, including TypeScript (multiple generators).
Installation:
npm install -D @openapitools/openapi-generator-cliUsage example:
npx openapi-generator-cli generate \ -i spec.yaml \ -g typescript-axios \ -o ./generatedGenerated code:
// generated/api.tsexport const UsersApiAxiosParamCreator = { getUsers: async (options?: AxiosRequestConfig): Promise<RequestArgs> => { const localVarPath = `/users`; return { url: localVarPath, method: 'GET', }; }}Features:
- Generators: typescript-axios, typescript-fetch, typescript-angular
- Authorization support (OAuth2, API Key)
- Generation of documentation and tests
- Works via CLI or Maven/Gradle plugins
3. swagger-typescript-api
A lightweight and fast generator focused on clean TypeScript without unnecessary dependencies.
Installation:
npm install -D swagger-typescript-apiUsage example:
npx swagger-typescript-api -p ./spec.yaml -o ./generated -n api.tsGenerated
Also in library
Elasticsearch client for C++: elasticlient and complete guide
Elasticsearch C++ Client: Complete Guide to Working with the Search Engine
Go (Golang) Application Monitoring: Libraries and Tools
Java QR Code Generator: ZXing (Zebra Crossing) Library
NHibernate for C# (.NET): Complete Guide with Examples