snaptype
Stop writing TypeScript types by hand.
snaptype is a CLI that turns your JSON files, CSV exports, REST APIs, and OpenAPI/GraphQL schemas into TypeScript interfaces and Zod schemas — in one command.
→ snaptype.dev — docs, Pro licence, live demo
The problem
Every time you hit an API or open a data file, you end up writing types by hand. You copy-paste a JSON response, squint at the shape, type out an interface. The API changes — you do it again.
snaptype automates that step entirely.
Quick start
npx snaptype from-json ./user.json -o types.ts
Input — user.json:
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"role": "admin",
"createdAt": "2024-01-15T10:30:00Z",
"address": { "city": "Paris", "zip": "75001" }
}
Output — types.ts:
export interface Address {
city: string;
zip: string;
}
export interface User {
id: number;
name: string;
email: string;
role: string;
createdAt: string;
address: Address;
}
Add --zod to also get a ready-to-use Zod schema:
npx snaptype from-json ./user.json -o types.ts --zod
import { z } from 'zod';
export const AddressSchema = z.object({
city: z.string(),
zip: z.string(),
});
export const UserSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
role: z.string(),
createdAt: z.string().datetime(),
address: AddressSchema,
});
export type User = z.infer<typeof UserSchema>;
Installation
npm install -D snaptype
npm install zod
Requires Node.js 20+.
Input sources
| Local JSON file | snaptype from-json ./data.json -o types.ts |
| Live API / URL | snaptype from-url https://api.example.com/users -o types.ts |
| CSV file | snaptype from-csv ./export.csv -o types.ts |
| stdin (pipe) | curl https://… | snaptype from-stdin --name User -o types.ts |
| OpenAPI 3.x spec | snaptype from-openapi ./openapi.yaml -o types.ts |
| GraphQL endpoint | snaptype from-graphql https://…/graphql -o types.ts (Pro) |
What it infers automatically
- Primitives:
string, number, boolean, null
- Nested objects and arrays
- Optional and nullable fields
- Semantic hints on string fields —
email, url, ISO 8601 dates → .email(), .url(), .datetime() in Zod
- Enum / union literals when a field has low cardinality (≤ 10 distinct values, ≥ 80% coverage)
$ref, oneOf, allOf, anyOf from OpenAPI specs
- GraphQL scalars, enums, unions, and interfaces
More commands
snaptype diff old.ts new.ts
snaptype mock ./types.ts -o mocks.ts
snaptype to-zod ./src/types/user.ts
snaptype barrel ./src/types
snaptype from-json ./api.json -o types.ts --watch
Project config
Drop a .snaptyperc at the root of your project to stop repeating flags:
{
"naming": "camel",
"emit": "interface",
"zod": true,
"outDir": "src/types"
}
CLI flags always take priority. See the configuration docs for all keys.
Free vs Pro
Pro is a one-time purchase — no subscription, works across machines.
from-json (single file) | ✓ | ✓ |
from-url, from-csv, from-stdin | ✓ | ✓ |
from-openapi (single file) | ✓ | ✓ |
| TypeScript + Zod generation | ✓ | ✓ |
| Semantic inference (email, date, url) | ✓ | ✓ |
| Enum / union literal detection | ✓ | ✓ |
--readonly, .snaptyperc, barrel | ✓ | ✓ |
from-json / from-url (multiple files) | — | ✓ |
from-graphql | — | ✓ |
snaptype diff + --ci | — | ✓ |
snaptype mock | — | ✓ |
snaptype to-zod | — | ✓ |
--watch | — | ✓ |
→ Get Pro at snaptype.dev
License
MIT — free tier is free forever. Pro features require a licence key.