Integrating third-party APIs often starts with optimism but descends into frustration. You open the documentation, copy a sample response, and begin constructing an interface—only to question whether a field should be a string, an object, or nullable. By the time you’re done, the interface feels fragile, and any API update risks breaking your code. There’s a faster, more reliable approach.
Generate typed interfaces without typing manually
Meet snaptype, a command-line tool that creates TypeScript interfaces and Zod schemas by analyzing real data sources—whether they’re live API endpoints, OpenAPI specifications, JSON files, or CSV exports. The tool requires no configuration, no custom code, and executes in a single command. Instead of guessing the shape of your data, snaptype infers it from actual responses, ensuring your types stay accurate and up to date.
Pull types directly from a live API endpoint
The most common use case for snaptype is generating types from a live JSON endpoint. For example, to fetch and type data from a products API, run:
npx snaptype from-url --zod -o src/types/product.tsThe tool fetches the response, analyzes its structure, and outputs a fully typed interface:
export interface Product {
id: number;
name: string;
price: number;
currency: "USD" | "EUR" | "GBP";
stock: number | null;
createdAt: string;
variants: Variant[];
}
export interface Variant {
sku: string;
color: string;
size: "S" | "M" | "L" | "XL";
}
export const ProductSchema = z.object({
id: z.number(),
name: z.string(),
price: z.number(),
currency: z.enum(["USD", "EUR", "GBP"]),
stock: z.number().nullable(),
createdAt: z.iso.datetime(),
variants: z.array(VariantSchema),
});Key improvements over manual typing include:
- Nested objects are automatically expanded into their own interfaces.
- Enums are inferred from repeated string values, replacing generic
stringtypes. - Nullable fields are detected and marked with
| null. - Semantic types like ISO date strings use specialized Zod validators such as
z.iso.datetime().
This approach uses real data for inference, not a rigid field-by-field mapping, reducing errors and saving time.
Use OpenAPI specs to generate entire schemas at once
When an API provides an OpenAPI or Swagger specification, snaptype can generate a complete set of typed interfaces from a single file. Point the CLI at your local YAML or JSON spec:
npx snaptype from-openapi ./openapi.yaml -o src/types/Or use a hosted spec URL:
npx snaptype from-openapi -o src/types/For a spec containing schemas like Pet, Order, and User, snaptype creates a separate TypeScript file for each. The generated files include optional fields, enums, and nested structures—ready for immediate use in your project.
Traditionally, setting up typed clients from OpenAPI specs involved:
- Selecting or building a code generator.
- Configuring complex templates.
- Running the generator.
- Cleaning up messy output.
- Repeating the process every time the spec changes.
With snaptype, this entire workflow collapses into one command.
Handle authenticated and private APIs seamlessly
For APIs requiring authentication, snaptype supports headers and tokens directly in the command. Pass them via the -H flag:
npx snaptype from-url \
-H "Authorization: Bearer $TOKEN" \
--zod -o src/types/me.tsFor projects involving multiple endpoints on the same API, store default headers in .snaptyperc to avoid repetition:
{
"baseUrl": "
"headers": {
"Authorization": "Bearer my-token",
"X-Tenant": "acme"
}
}These defaults apply to every from-url command, though CLI headers take precedence if specified. For complex authentication flows not easily replicated in a single request, pipe the response from a tool like curl:
curl -s -H "Authorization: Bearer $TOKEN" \
| npx snaptype from-stdin --zod -o src/types/me.tsGenerate types from local JSON and CSV files
Snaptype isn’t limited to live endpoints. You can derive types from local JSON files:
npx snaptype from-json ./user.json --zod -o src/types/user.tsOr from CSV exports, such as database dumps or spreadsheet exports:
npx snaptype from-csv ./orders.csv --zod -o src/types/order.tsThe tool applies the same inference logic—detecting enums, nullables, and nested structures—regardless of the source format.
Start in seconds with zero configuration
Snaptype eliminates setup complexity. If you want to define defaults like enabling Zod schemas and setting the output directory, create a .snaptyperc file:
{
"zod": true,
"outDir": "src/types"
}From there, commands become even simpler, as the tool picks up your preferences automatically.
Try it immediately
Run snaptype directly with npx—no installation required:
npx snaptype from-url --zod -o src/types/github-user.tsOr install it as a dev dependency for consistent versions and faster execution:
npm install -D snaptype
npx snaptype from-url --zod -o src/types/github-user.tsExplore the full documentation and source code on snaptype.dev and GitHub. If the tool helps streamline your workflow, consider starring the repository—it helps others discover it too.
Snaptype was created after one too many mornings spent manually rewriting interfaces from API documentation. If you find it useful for a real project, share which APIs you’re integrating—feedback shapes the tool’s future.
The tool is still evolving, and community input is welcome as it matures.
AI summary
Stop writing TypeScript interfaces by hand. Use snaptype to generate accurate, typed schemas from JSON, CSV, or OpenAPI specs in one command—saving hours and reducing errors.