@hey-api/openapi-ts
Version:
🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more.
307 lines (231 loc) • 12.1 kB
Markdown
<div align="center">
<img alt="Hey API logo" height="150" src="https://heyapi.dev/images/logo-300w.png" width="150">
<h1 align="center"><b>OpenAPI TypeScript</b></h1>
<p align="center">🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more.</p>
</div>
<br/>
<p align="center">
<a href="https://opensource.org/license/mit" rel="nofollow"><img src="https://img.shields.io/github/license/hey-api/openapi-ts" alt="MIT License"></a>
<a href="https://github.com/hey-api/openapi-ts/actions?query=branch%3Amain"><img src="https://img.shields.io/github/last-commit/hey-api/openapi-ts" alt="Last commit" /></a>
<a href="https://github.com/hey-api/openapi-ts/actions?query=branch%3Amain"><img src="https://github.com/hey-api/openapi-ts/actions/workflows/ci.yml/badge.svg?event=push&branch=main" alt="CI status" /></a>
<a href="https://github.com/hey-api/openapi-ts/issues" rel="nofollow"><img src="https://img.shields.io/github/issues/hey-api/openapi-ts" alt="Number of open issues"></a>
<a href="https://app.codecov.io/gh/hey-api/openapi-ts/tree/main"><img src="https://codecov.io/gh/hey-api/openapi-ts/branch/main/graph/badge.svg" alt="Test coverage" /></a>
</p>
<p align="center">
<a href="https://stackblitz.com/edit/hey-api-example?file=openapi-ts.config.ts,src%2Fclient%2Fschemas.gen.ts,src%2Fclient%2Fsdk.gen.ts,src%2Fclient%2Ftypes.gen.ts">Demo</a>
<span> • </span>
<a href="https://heyapi.dev">Documentation</a>
<span> • </span>
<a href="https://github.com/hey-api/openapi-ts/issues">Issues</a>
<span> • </span>
<a href="https://github.com/orgs/hey-api/discussions/1495">Roadmap</a>
<span> • </span>
<a href="https://npmjs.com/package/@hey-api/openapi-ts">npm</a>
</p>
<br/>
## Features
- runs in CLI, Node.js 18+, or npx
- works with OpenAPI 2.0, 3.0, and 3.1
- customizable types and SDKs
- clients for your runtime (Fetch API, Axios, Next.js, Nuxt, etc.)
- plugin ecosystem to reduce third-party boilerplate
- custom plugins and custom clients
- [integration](https://heyapi.dev/openapi-ts/integrations) with Hey API Platform
## Dashboard
Hey API is an ecosystem of products helping you build better APIs. Superpower your codegen and APIs with Hey API Platform.
[Sign In](https://app.heyapi.dev) to Hey API Platform.
## Sponsors
Love Hey API? Become our [sponsor](https://github.com/sponsors/hey-api).
<h3 align="center">Gold</h3>
<table align="center" style="justify-content: center;align-items: center;display: flex;">
<tbody>
<tr>
<td align="center">
<p></p>
<p>
<a href="https://kutt.it/pkEZyc" target="_blank">
<picture height="50px">
<source media="(prefers-color-scheme: dark)" srcset="https://heyapi.dev/images/stainless-logo-wordmark-480w.jpeg">
<img alt="Stainless logo" height="50px" src="https://heyapi.dev/images/stainless-logo-wordmark-480w.jpeg">
</picture>
</a>
<br/>
Generate best-in-class SDKs.
<br/>
<a href="https://kutt.it/pkEZyc" style="text-decoration:none;" target="_blank">
stainless.com
</a>
</p>
<p></p>
</td>
</tr>
</tbody>
</table>
<h3 align="center">Silver</h3>
<table align="center" style="justify-content: center;align-items: center;display: flex;">
<tbody>
<tr>
<td align="center">
<a href="https://kutt.it/skQUVd" target="_blank">
<picture height="40px">
<source media="(prefers-color-scheme: dark)" srcset="https://heyapi.dev/images/scalar-logo-wordmark-480w.jpeg">
<img alt="Scalar logo" height="40px" src="https://heyapi.dev/images/scalar-logo-wordmark-480w.jpeg">
</picture>
</a>
<br/>
<a href="https://kutt.it/skQUVd" style="text-decoration:none;" target="_blank">
scalar.com
</a>
</td>
</tr>
</tbody>
</table>
<h3 align="center">Bronze</h3>
<table align="center" style="justify-content: center;align-items: center;display: flex;">
<tbody>
<tr>
<td align="center">
<a href="https://kutt.it/YpaKsX" target="_blank">
<picture height="34px">
<source media="(prefers-color-scheme: dark)" srcset="https://heyapi.dev/images/kinde-logo-wordmark-dark-480w.webp">
<img alt="Kinde logo" height="34px" src="https://heyapi.dev/images/kinde-logo-wordmark-480w.jpeg">
</picture>
</a>
</td>
<td align="center">
<a href="https://kutt.it/KkqSaw" target="_blank">
<picture height="34px">
<source media="(prefers-color-scheme: dark)" srcset="https://heyapi.dev/images/cella-logo-wordmark-480w.webp">
<img alt="Cella logo" height="34px" src="https://heyapi.dev/images/cella-logo-wordmark-480w.jpeg">
</picture>
</a>
</td>
</tr>
</tbody>
</table>
## Quick Start
The fastest way to use `-api/openapi-ts` is via npx
```sh
npx -api/openapi-ts \
-i https://get.heyapi.dev/hey-api/backend \
-o src/client
```
Congratulations on creating your first client! 🎉 You can learn more about the generated files on the [Output](https://heyapi.dev/openapi-ts/output) page.
## Installation
#### npm
```sh
npm install -api/openapi-ts -D
```
#### pnpm
```sh
pnpm add -api/openapi-ts -D
```
#### yarn
```sh
yarn add -api/openapi-ts -D
```
#### bun
```sh
bun add -api/openapi-ts -D
```
We recommend pinning an exact version so you can safely upgrade when you're ready. This package is in [initial development](https://semver.org/spec/v0.1.0.html#spec-item-5) and its API might change before v1.
### CLI
Most people run `-api/openapi-ts` via CLI. To do that, add a script to your `package.json` file which will make `openapi-ts` executable through script.
```json
"scripts": {
"openapi-ts": "openapi-ts"
}
```
The above script can be executed by running `npm run openapi-ts` or equivalent command in other package managers. Next, we will create a [configuration](https://heyapi.dev/openapi-ts/configuration) file and move our options from Quick Start to it.
### Node.js
You can also generate clients programmatically by importing `-api/openapi-ts` in a TypeScript file.
```ts
import { createClient } from '-api/openapi-ts';
createClient({
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
});
```
## Configuration
`-api/openapi-ts` supports loading configuration from any file inside your project root folder supported by [jiti loader](https://github.com/unjs/c12?tab=readme-ov-file#-features). Below are the most common file formats.
#### `openapi-ts.config.ts`
```js
import { defineConfig } from '-api/openapi-ts';
export default defineConfig({
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
});
```
#### `openapi-ts.config.cjs`
```js
/** @type {import('@hey-api/openapi-ts').UserConfig} */
module.exports = {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
};
```
#### `openapi-ts.config.mjs`
```js
/** @type {import('@hey-api/openapi-ts').UserConfig} */
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
};
```
Alternatively, you can use `openapi-ts.config.js` and configure the export statement depending on your project setup.
### Input
You must set the input so we can load your OpenAPI specification. It can be a path or URL, object containing a path or URL, or an object representing an OpenAPI specification. Hey API supports all valid OpenAPI versions and file formats.
> If you use an HTTPS URL with a self-signed certificate in development, you will need to set [`NODE_TLS_REJECT_UNAUTHORIZED=0`](https://github.com/hey-api/openapi-ts/issues/276#issuecomment-2043143501) in your environment.
### Output
You must set the output so we know where to generate your files. It can be a path to the destination folder or an object containing the destination folder path and optional settings.
> You should treat the output folder as a dependency. Do not directly modify its contents as your changes might be erased when you run `-api/openapi-ts` again.
## Parser
We parse your input before making it available to plugins. While configuring the parser is optional, it's the perfect place to modify or validate your input if needed.
## Plugins
Plugins are responsible for generating artifacts from your input. By default, Hey API will generate TypeScript interfaces and SDK from your OpenAPI specification. You can add, remove, or customize any of the plugins. In fact, we highly encourage you to do so!
### Client
Clients are responsible for sending the actual HTTP requests. Using clients is not required, but you must add a client to `plugins` if you're generating SDKs (we default to Fetch).
### Native Clients
- [`-api/client-fetch`](https://heyapi.dev/openapi-ts/clients/fetch)
- [`-api/client-axios`](https://heyapi.dev/openapi-ts/clients/axios)
- [`-api/client-next`](https://heyapi.dev/openapi-ts/clients/next-js)
- [`-api/client-nuxt`](https://heyapi.dev/openapi-ts/clients/nuxt)
Don't see your client? [Build your own](https://heyapi.dev/openapi-ts/clients/custom) or let us know your interest by [opening an issue](https://github.com/hey-api/openapi-ts/issues).
### Native Plugins
These plugins help reduce boilerplate associated with third-party dependencies. Hey API natively supports the most popular packages. Please open an issue on [GitHub](https://github.com/hey-api/openapi-ts/issues) if you'd like us to support your favorite package.
- [`-api/schemas`](https://heyapi.dev/openapi-ts/output/json-schema)
- [`-api/sdk`](https://heyapi.dev/openapi-ts/output/sdk)
- [`-api/transformers`](https://heyapi.dev/openapi-ts/transformers)
- [`-api/typescript`](https://heyapi.dev/openapi-ts/output/typescript)
- [`/angular-query-experimental`](https://heyapi.dev/openapi-ts/plugins/tanstack-query)
- [`/react-query`](https://heyapi.dev/openapi-ts/plugins/tanstack-query)
- [`/solid-query`](https://heyapi.dev/openapi-ts/plugins/tanstack-query)
- [`/svelte-query`](https://heyapi.dev/openapi-ts/plugins/tanstack-query)
- [`/vue-query`](https://heyapi.dev/openapi-ts/plugins/tanstack-query)
- [`fastify`](https://heyapi.dev/openapi-ts/plugins/fastify)
- [`valibot`](https://heyapi.dev/openapi-ts/plugins/valibot)
- [`zod`](https://heyapi.dev/openapi-ts/plugins/zod)
### Planned Plugins
The following plugins are planned but not in development yet. You can help us prioritize them by voting on [GitHub](https://github.com/hey-api/openapi-ts/labels/RSVP%20%F0%9F%91%8D%F0%9F%91%8E).
- [Ajv](https://heyapi.dev/openapi-ts/plugins/ajv)
- [Arktype](https://heyapi.dev/openapi-ts/plugins/arktype)
- [Express](https://heyapi.dev/openapi-ts/plugins/express)
- [Faker](https://heyapi.dev/openapi-ts/plugins/faker)
- [Hono](https://heyapi.dev/openapi-ts/plugins/hono)
- [Joi](https://heyapi.dev/openapi-ts/plugins/joi)
- [Koa](https://heyapi.dev/openapi-ts/plugins/koa)
- [MSW](https://heyapi.dev/openapi-ts/plugins/msw)
- [Nest](https://heyapi.dev/openapi-ts/plugins/nest)
- [Nock](https://heyapi.dev/openapi-ts/plugins/nock)
- [Pinia Colada](https://heyapi.dev/openapi-ts/plugins/pinia-colada)
- [Superstruct](https://heyapi.dev/openapi-ts/plugins/superstruct)
- [Supertest](https://heyapi.dev/openapi-ts/plugins/supertest)
- [SWR](https://heyapi.dev/openapi-ts/plugins/swr)
- [TypeBox](https://heyapi.dev/openapi-ts/plugins/typebox)
- [Yup](https://heyapi.dev/openapi-ts/plugins/yup)
- [Zustand](https://heyapi.dev/openapi-ts/plugins/zustand)
Don't see your plugin? [Build your own](https://heyapi.dev/openapi-ts/plugins/custom) or let us know your interest by [opening an issue](https://github.com/hey-api/openapi-ts/issues).
## Migrating
You can learn more on the [Migrating](https://heyapi.dev/openapi-ts/migrating) page.
## License
Released under the [MIT License](https://github.com/hey-api/openapi-ts/blob/main/LICENSE.md).