Swagger/OpenAPI generation for TypeScript: best tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

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-codegen


Usage example:

npx openapi-typescript-codegen --input ./spec.yaml --output ./generated --client axios


Generated 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-cli


Usage example:

npx openapi-generator-cli generate \    -i spec.yaml \    -g typescript-axios \    -o ./generated


Generated 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-api


Usage example:

npx swagger-typescript-api -p ./spec.yaml -o ./generated -n api.ts


Generated

Recommendations