OpenAPI to TypeScript Generator
Convert OpenAPI 3.0/3.1 YAML or JSON to TypeScript interfaces in your browser. Handles $ref, oneOf, allOf, enum, nullable, and optional Zod schemas. No upload.
- Runs in your browser
- Tool input is not uploaded to ZeroTool servers
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
What gets generated
- components/schemas become one
export interfaceorexport typeper schema. Object schemas become interfaces; primitive, union, enum, and intersection schemas become type aliases. - $ref targets inside
#/components/schemas/...are resolved and emitted by name, preserving the declaration order from your spec. - oneOf / anyOf become union types (
A | B | C). allOf becomes an intersection (A & B). - enum becomes a string or numeric literal union.
- nullable in 3.0 and
type: ['X', 'null']in 3.1 both becomeX | null. - format hints add JSDoc comments —
format: date-timenotes ISO 8601,format: binarybecomesBlob. - additionalProperties become index signatures or
Record<string, T>.
Path types (optional)
With Include path types enabled, each operation produces a typed envelope:
export namespace Components {
export namespace GetPetById {
export interface PathParameters {
id: number;
}
export type Response200 = Pet;
}
}
If an operation has no operationId, the tool synthesises one from method + path, e.g. GetPetsId.
Zod schemas (optional)
Toggle Include Zod schemas to emit a parallel XSchema for every component:
import { z } from 'zod';
export const PetSchema = z.object({
id: z.number().int(),
name: z.string(),
status: z.enum(["available", "pending", "sold"]),
});
Pair this output with TypeScript to Zod when you want to keep schemas in sync with hand-written types instead of an OpenAPI spec.
Example: The Built-in Pet Store Spec
Click Example to load a small OpenAPI 3.0.3 spec with Pet and NewPet schemas and three operations. The schema section of the output:
export interface Pet {
id: number;
name: string;
tag?: string | null;
status: "available" | "pending" | "sold";
photo?: Blob;
}
export interface NewPet {
name: string;
tag?: string;
}
Required properties stay required, everything else gets ?, nullable: true adds | null, and format: binary becomes Blob. With Include path types on, getPetById becomes Components.GetPetById with PathParameters { id: number } and Response200 = Pet. With Include Zod schemas on, the generated PetSchema (checked with zod 3.25) rejects status: "lost" and accepts tag: null.
Example: const, Type Arrays and an External Reference
This OpenAPI 3.1 schema uses const, a type array and a reference to another file:
openapi: 3.1.0
info: { title: Events, version: 1.0.0 }
paths: {}
components:
schemas:
Event:
type: object
required: [id, kind, createdAt]
properties:
id: { type: integer, format: int64 }
kind: { const: order.created }
createdAt: { type: string, format: date-time }
note: { type: [string, 'null'] }
owner: { $ref: 'https://example.com/schemas/user.yaml#/User' }
The tool outputs:
export interface Event {
id: number;
kind: "order.created";
/** ISO 8601 date-time */ createdAt: string;
note?: string | null;
owner?: unknown /* unresolved $ref: https://example.com/schemas/user.yaml#/User */;
}
type: [string, "null"] becomes string | null and const becomes the literal type "order.created" (in Zod, z.literal("order.created")). The reference to user.yaml is in another document, which the tool does not fetch. It types owner as unknown, keeps the reference in a comment, and the status bar shows "Not supported: $ref to https://example.com/schemas/user.yaml#/User". Run redocly bundle first to pull external files into components, then paste the bundled spec.
Example: Forward References and Recursion in Zod
A Zod schema is a JavaScript constant, so it cannot be used before the line that defines it. With these schemas, Order refers to Customer, which is declared later, and Node refers to itself:
components:
schemas:
Order:
type: object
required: [customer]
properties:
customer: { $ref: '#/components/schemas/Customer' }
Customer:
type: object
required: [name]
properties:
name: { type: string }
Node:
type: object
required: [value]
properties:
value: { type: string }
children:
type: array
items: { $ref: '#/components/schemas/Node' }
The Zod output wraps both references in z.lazy, which looks the schema up when data is parsed rather than when the module loads. A schema that refers to itself, directly or through other schemas, also gets a z.ZodType<Node> annotation, because TypeScript cannot infer the type of a constant that appears in its own initializer (error TS7022):
export const OrderSchema = z.object({
customer: z.lazy(() => CustomerSchema),
});
export const CustomerSchema = z.object({
name: z.string(),
});
export const NodeSchema: z.ZodType<Node> = z.object({
value: z.string(),
children: z.array(z.lazy(() => NodeSchema)).optional(),
});
The generated module compiles with TypeScript 5.9 in strict mode, and zod 3.25 parses a three-level tree with it.
Limits
- Only
#/components/schemas/...references are resolved, plus#/components/parameters/...in path types. Other references (URLs, other files,#/components/responses/...) are typed asunknownand listed in the status bar. - Schema names that are not valid TypeScript identifiers are renamed: characters other than letters, digits,
_and$become_(pet-status→pet_status), names that start with a digit get a leading_, and keywords such asunknownorclassget a trailing_. - A
constwith an object or array value is typed asunknown; string, number, boolean and null values become literal types. integerwithformat: int64becomesnumber, which loses precision above 253. Change such IDs tostringif the API sends them as strings.- Path types read only
application/jsonrequest and response bodies.
Related tools
- OpenAPI Validator — sanity-check the spec before generating types.
- JSON to TypeScript — when you only have a sample JSON response, not a full spec.
- JSON to Zod Schema — generate Zod from a single JSON example.
FAQ
Which OpenAPI versions are supported?
Both 3.0.x and 3.1.x. OpenAPI 3.1 type unions like `type: ['string', 'null']` map to `string | null`. OpenAPI 3.0 `nullable: true` is also recognised.
What about Swagger 2.0?
Swagger 2.0 is not supported by design. Migrate your spec to OpenAPI 3.x first using editor.swagger.io (Swagger Editor's built-in converter), then paste the upgraded spec here.
Are remote `$ref` URLs resolved?
No. Only `#/components/schemas/...` refs are resolved (plus `#/components/parameters/...` in path types). Any other `$ref`, such as a URL or another file, becomes `unknown` with a comment, and the status bar lists every unresolved reference. Bundle the spec into one file first, for example with `redocly bundle`, or use the openapi-typescript CLI.
Where does my spec go?
It is not uploaded. The YAML or JSON is parsed and the TypeScript is generated in this tab; the spec is not saved in browser storage, and remote $ref URLs are not fetched. The page analytics record only the tool name and action generate after you commit a spec or namespace edit, change an option, or load the example and types are generated without unresolved references. Typing does not record generate, and an unchanged output is not counted again. Clicking Example separately records example_load. The spec and generated types are not included.
Can I get Zod schemas as well?
Yes. Toggle **Include Zod schemas** to emit `z.object({...})` definitions alongside the TypeScript interfaces. Useful when you want runtime validation that mirrors your static types.
What does Include path types do?
It generates a `Components` (or your chosen root namespace) wrapper with one namespace per operation, exposing `PathParameters`, `QueryParameters`, `RequestBody`, and `Response200` / `Response201` etc. Skip it if you only need DTO interfaces.