Type-safe OpenAPI clients without the boilerplate
Your OpenAPI spec defines the contract. Your TypeScript types should match it exactly — not diverge silently every sprint. snaptype generates both from the spec, and catches breaking changes before they reach production.
The problem
OpenAPI specs and TypeScript types are written separately and maintained separately. The spec gets updated when the backend changes. The types get updated when someone remembers. They drift apart within weeks, and you only find out when a runtime error surfaces in production.
// The spec was updated 2 sprints ago...
// User.role was added, User.name split into firstName + lastName
// But your TypeScript still says:
interface User {
id: number;
name: string; // ❌ no longer exists in the API
email: string;
// role is missing entirely
}The compiler stays silent because the interface compiles fine — it just no longer matches what the API actually returns.
The solution
Make the OpenAPI spec the single source of truth. snaptype reads your spec and generates one TypeScript file per schema component — automatically, reproducibly, and with Zod schemas included if you need runtime validation.
How it works
Run one command against your spec file:
npx snaptype from-openapi openapi.yaml -o src/types/Given a schema in your OpenAPI spec:
components:
schemas:
User:
type: object
required: [id, email, role]
properties:
id:
type: integer
email:
type: string
format: email
role:
type: string
enum: [admin, user]
createdAt:
type: string
format: date-timesnaptype generates a typed file per schema:
export interface User {
id: number;
email: string;
role: "admin" | "user";
createdAt?: string;
}Add --zod to also generate a Zod schema for runtime validation of API responses:
import { z } from "zod";
export const UserSchema = z.object({
id: z.number(),
email: z.email(),
role: z.enum(["admin", "user"]),
createdAt: z.iso.datetime().optional(),
});
export type User = z.infer<typeof UserSchema>;Breaking change detection Pro
Add snaptype to your CI pipeline to catch regressions before they ship. If a required field is removed or a type changes incompatibly, the command exits with code 1 and fails the build.
# In your CI pipeline (GitHub Actions, GitLab CI, etc.)
npx snaptype from-openapi openapi.yaml -o src/types/
npx snaptype diff --ci src/types/
# exits 1 if breaking changes are detected → build failsWhat you get
- Spec is the single source of truth — TypeScript types never drift from the API contract
- One file per schema component, clean output directory structure
- Zod schemas for runtime validation of API responses alongside TypeScript interfaces
- Supports both YAML and JSON specs, OpenAPI 3.x and Swagger 2.0
- Breaking change detection in CI — exit code 1 when a field is removed or a type regresses
Related resources
Try snaptype in 30 seconds
No account needed. Works with any JSON file or API endpoint.
npm install -D snaptype
npx snaptype from-openapi openapi.yaml -o src/types/