How to Convert OpenAPI to TypeScript - Step by Step Guide
From an OpenAPI 3 or Swagger 2 file to interfaces you can import next to your API calls
Paste or upload the spec
Paste an OpenAPI 3 or Swagger 2.0 file in YAML or JSON. The converter looks at components/schemas (OpenAPI 3) or definitions (Swagger 2) and writes one TypeScript type per schema:
openapi.json your backend servesExample: a spec with two schemas
A Product with an enum, a list, a nullable date, a key with a dash and a $ref:
openapi: 3.0.3
info:
title: Shop
version: 1.0.0
paths: {}
components:
schemas:
Product:
type: object
description: A product in the catalogue
required: [id, name, price]
properties:
id: { type: integer }
name: { type: string }
price: { type: number }
currency:
type: string
enum: [USD, EUR, GBP]
tags:
type: array
items: { type: string }
first-seen: { type: string, format: date, nullable: true }
category: { $ref: '#/components/schemas/Category' }
Category:
type: object
required: [name]
properties:
name: { type: string }
parentId: { type: integer }Check the generated types
Object schemas become export interface; enums, lists and unions become export type:
required have no ?; nullable ones add | null"USD" | "EUR" | "GBP"first-seen stays "first-seen" in quotes, so it matches the JSONExample: the TypeScript for the spec above
This is exactly what the converter writes for the Step 1 example:
// TypeScript types for the components/schemas of "Shop" 1.0.0.
// These describe the JSON shape only; nothing checks the data at runtime.
/** A product in the catalogue */
export interface Product {
id: number;
name: string;
price: number;
currency?: "USD" | "EUR" | "GBP";
tags?: string[];
"first-seen"?: string | null;
category?: Category;
}
export interface Category {
name: string;
parentId?: number;
}Drop the file into your project
Save the code as api-types.ts and import the types with import type. With plain fetch it looks like this:
import type { Product } from './api-types';
const res = await fetch('/api/products/42');
const product: Product = await res.json();
console.log(product.name, product['first-seen'] ?? 'never');axios.get<Product[]>('/products')useQuery<Product> gives autocomplete on the resultWant to check the spec first? The OpenAPI Validator points out structural problems before you generate code.
What is OpenAPI to TypeScript Conversion?
An OpenAPI (or older Swagger) file describes the JSON an API sends and receives. Turning its schemas into TypeScript types keeps your frontend in step with the backend: when the spec changes and you regenerate the file, the compiler shows every place that still expects the old shape.
The output is one plain .ts file with nothing but interfaces and type aliases. Descriptions, defaults and deprecated flags from the spec become JSDoc comments, so your editor shows them on hover, and property names stay exactly as the API spells them.
This page writes the data types only. For a client with a typed function per endpoint, openapi-typescript, Orval and swagger-typescript-api are the usual picks. Only have a sample response? JSON to TypeScript builds interfaces from data, and Swagger to OpenAPI moves a Swagger 2 file to OpenAPI 3.
Frequently Asked Questions
Which OpenAPI versions and formats can I paste?
OpenAPI 3.0 and 3.1 and Swagger 2.0, as YAML or JSON. Types come from components/schemas in OpenAPI 3 and from definitions in Swagger 2. Paths, parameters and inline request or response schemas are not turned into types; move a schema into components/schemas and point at it with $ref if you need it.
How do OpenAPI types map to TypeScript?
string becomes string, integer and number both become number, boolean becomes boolean, arrays become T[] and an object with only additionalProperties becomes Record<string, T>. Formats such as date-time and int64 stay string and number, because that is what JSON.parse hands you. One catch: JavaScript numbers lose digits past 9007199254740991, so when the spec has int64 fields or values that large, a note at the top says so. Enum and default values are still copied with every digit, 8901240544102066246 included.
How are required, optional and nullable fields written?
A field listed in required is written without a question mark, so the compiler complains when you build an object without it. Any other field gets ?. A nullable field (nullable: true, x-nullable, or "null" in an OpenAPI 3.1 type list, oneOf or enum) adds | null to its type, so a field can be optional, nullable, or both.
What happens to enums, $ref, allOf, oneOf and anyOf?
Enums of strings, numbers and booleans become literal unions such as "USD" | "EUR" | "GBP", with each value exactly as written in the spec. A $ref uses the type of the schema it points to, in any order, including schemas that refer to themselves. allOf becomes an intersection (A & B); when a part only lists extra required fields, the parts are merged into one object instead. oneOf and anyOf become unions.
What about property keys and schema names that are not valid identifiers?
Property keys keep their exact spelling. Keys like first-name or 2nd are written in quotes ("first-name"?: string), so they match the JSON the API sends and you read them with product["first-seen"]. Schema names that are not identifiers become PascalCase (my-model becomes MyModel), and names that would hide a built-in type, such as Record or Date, get Model added.
Do these types check the data at runtime?
No. TypeScript types disappear when the code is compiled, so a response that does not match the spec is not caught by them. If you need runtime checks, pair the types with a validator such as Zod, or generate a full client with openapi-typescript or Orval. For quick work, the interfaces fit straight into fetch, Axios generics and React Query.
Is my spec uploaded anywhere?
No. The spec is read and converted in your browser and is not sent to our server. Nothing is kept either: a reload starts with an empty editor.
Related Tools
OpenAPI Validator
Validate OpenAPI 3.0 and Swagger 2.0 specifications online. Check required fields, paths, operations, schemas, and security schemes with instant error reporting.
JSON to OpenAPI
Generate OpenAPI/Swagger specifications from JSON examples. Infer data types and create complete API schemas automatically.
OpenAPI Fixer
Fix broken OpenAPI and Swagger specifications automatically, repair malformed paths, schemas, $ref references, and YAML syntax errors
OpenAPI to Rust
Turn the schemas in an OpenAPI or Swagger spec into Rust structs and enums with serde attributes.
OpenAPI to Go
Generate type-safe Go structs with JSON tags from OpenAPI 3.0 and Swagger 2.0 specifications.
OpenAPI to Markdown
Convert OpenAPI 3.0 and Swagger 2.0 specifications to readable Markdown API documentation. Generate endpoint tables, parameter docs, and schema references.