openapi-typescript vs @hey-api/openapi-ts vs swagger-typescript-api
OpenAPI TypeScript Code Generators Comparison
1 Year
openapi-typescript@hey-api/openapi-tsswagger-typescript-api
What's OpenAPI TypeScript Code Generators?

OpenAPI TypeScript code generators are tools that facilitate the generation of TypeScript types and API client code from OpenAPI specifications. They streamline the process of integrating APIs into TypeScript applications by automating the creation of type-safe interfaces and client functions, thereby reducing boilerplate code and minimizing the risk of runtime errors. These packages cater to different needs and preferences in terms of customization, ease of use, and output structure, making it essential to choose the right one based on project requirements and developer familiarity.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
openapi-typescript1,694,1277,034801 kB169a month agoMIT
@hey-api/openapi-ts424,2192,7266.84 MB27817 hours agoMIT
swagger-typescript-api337,709-990 kB-8 days agoMIT
Feature Comparison: openapi-typescript vs @hey-api/openapi-ts vs swagger-typescript-api

Customization Options

  • openapi-typescript:

    Openapi-typescript provides extensive customization capabilities, enabling developers to tailor the generated code to their specific needs, including options for naming conventions, output structure, and more.

  • @hey-api/openapi-ts:

    This package offers basic customization options, allowing users to specify certain parameters during code generation, but it is primarily designed for simplicity and ease of use.

  • swagger-typescript-api:

    Swagger-typescript-api includes a variety of customization options, such as the ability to define custom request/response types and modify the generated API methods, making it suitable for complex projects.

Ease of Use

  • openapi-typescript:

    While more customizable, openapi-typescript may require a steeper learning curve due to its numerous configuration options, which could be overwhelming for beginners.

  • @hey-api/openapi-ts:

    Designed for quick integration, this package is user-friendly and requires minimal setup, making it ideal for developers who want to get started quickly without extensive configuration.

  • swagger-typescript-api:

    This package strikes a balance between functionality and ease of use, providing a straightforward setup process while still offering advanced features for more experienced developers.

Type Safety

  • openapi-typescript:

    Focuses heavily on type safety, providing comprehensive type definitions that leverage TypeScript's capabilities to ensure robust type checking across the application.

  • @hey-api/openapi-ts:

    Generates TypeScript types that ensure type safety for API responses and requests, helping to catch errors at compile time rather than runtime.

  • swagger-typescript-api:

    Also emphasizes type safety, generating types that align closely with the OpenAPI specification, thus ensuring that developers work with accurate and reliable type definitions.

Community Support

  • openapi-typescript:

    Well-supported with a strong community presence, this package has extensive documentation and numerous examples, making it easier for developers to find help and resources.

  • @hey-api/openapi-ts:

    This package has a growing community and is gaining traction, but it may not yet have as many resources or examples available compared to more established options.

  • swagger-typescript-api:

    Has an active community and good documentation, providing users with ample support and resources to troubleshoot issues and learn best practices.

Performance

  • openapi-typescript:

    Performance is generally good, but the extensive customization options can lead to longer generation times for complex configurations.

  • @hey-api/openapi-ts:

    Optimized for quick code generation, this package performs well for smaller projects but may not be as efficient for very large APIs due to its simplicity.

  • swagger-typescript-api:

    This package is designed to handle larger APIs efficiently, providing good performance even with extensive features and customization options.

How to Choose: openapi-typescript vs @hey-api/openapi-ts vs swagger-typescript-api
  • openapi-typescript:

    Select openapi-typescript if you require a highly customizable solution that allows for extensive configuration options and supports advanced TypeScript features. This package is ideal for developers who want fine-grained control over the generated code and its structure.

  • @hey-api/openapi-ts:

    Choose @hey-api/openapi-ts if you need a straightforward, easy-to-use tool that generates TypeScript types and API clients with minimal configuration. It is particularly beneficial for projects that prioritize simplicity and quick integration.

  • swagger-typescript-api:

    Opt for swagger-typescript-api if you need a comprehensive solution that not only generates TypeScript types and API clients but also includes additional features like request/response validation and customizable API methods. This package is suitable for larger projects where more functionality is desired.

README for openapi-typescript
openapi-typescript

openapi-typescript turns OpenAPI 3.0 & 3.1 schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.

The code is MIT-licensed and free for use.

Tip: New to OpenAPI? Speakeasy’s Intro to OpenAPI is an accessible guide to newcomers that explains the “why” and “how” of OpenAPI.

Features

  • ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like discriminators)
  • ✅ Generate runtime-free types that outperform old school codegen
  • ✅ Load schemas from YAML or JSON, locally or remotely
  • ✅ Generate types for even huge schemas within milliseconds

Note: OpenAPI 2.x is supported with versions 5.x and previous

Examples

👀 See examples

Setup

This library requires the latest version of Node.js installed (20.x or higher recommended). With that present, run the following in your project:

npm i -D openapi-typescript typescript

And in your tsconfig.json, to load the types properly:

{
  "compilerOptions": {
+    "module": "ESNext", // or "NodeNext"
+    "moduleResolution": "Bundler" // or "NodeNext"
  }
}

Highly recommended

Also adding the following can boost type safety:

{
  "compilerOptions": {
+    "noUncheckedIndexedAccess": true
  }
}

Basic usage

First, generate a local type file by running npx openapi-typescript, first specifying your input schema (JSON or YAML), and where you’d like the --output (-o) to be saved:

# Local schema
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
# 🚀 ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms]

# Remote schema
npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]

Then in your TypeScript project, import types where needed:

import type { paths, components } from "./my-openapi-3-schema"; // generated by openapi-typescript

// Schema Obj
type MyType = components["schemas"]["MyType"];

// Path params
type EndpointParams = paths["/my/endpoint"]["parameters"];

// Response obj
type SuccessResponse =
  paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
type ErrorResponse =
  paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];

From here, you can use these types for any of the following (but not limited to):

  • Using an OpenAPI-supported fetch client (like openapi-fetch)
  • Asserting types for other API requestBodies and responses
  • Building core business logic based on API types
  • Validating mock test data is up-to-date with the current schema
  • Packaging API types into any npm packages you publish (such as client SDKs, etc.)

📓 Docs

View Docs