Back to TypeMorph
30 seconds·Updated

Convert Any API Response to a Zod Schema

Pipe your API response through typemorph-cli zod and get a Zod v4 schema with real type inference — email, uuid, url, datetime, and int vs float are detected automatically from the actual values.

The problem

You call an external API and need runtime validation. Writing the Zod schema by hand is tedious and error-prone — especially for large responses with nested objects, arrays, and date strings. Generic converters output z.string() for every string field, missing email, uuid, url, and datetime constraints.

The solution

TypeMorph reads the actual values in your JSON, not just the types. One command turns a live API response into a schema that validates uuid, email, url, and datetime fields — ready to drop into your codebase.

Step-by-step

  1. 1

    Install (one-time)

    No global install needed. npx pulls the latest version automatically.

    Terminal

    npm install -g typemorph-cli
    # or use npx without installing
  2. 2

    Pipe the API response directly

    Fetch the endpoint and pipe stdout to typemorph-cli. The --root flag sets the exported schema name.

    Terminal

    curl -s https://api.example.com/users/1 \
      | npx typemorph-cli zod --root User
  3. 3

    See what TypeMorph infers

    Compare this to what other converters produce — every string field gets z.string() elsewhere.

    TypeScript

    // Input JSON
    // { "id": "a1b2c3d4-...", "email": "[email protected]", "created_at": "2024-01-01T00:00:00Z", "age": 25 }
    
    // TypeMorph output
    export const UserSchema = z.object({
      id: z.uuid(),
      email: z.email(),
      created_at: z.iso.datetime(),
      age: z.number().int().min(0),
    });
    
    // Other tools
    export const UserSchema = z.object({
      id: z.string(),       // ← loses uuid constraint
      email: z.string(),    // ← loses email constraint
      created_at: z.string(),
      age: z.number(),
    });
  4. 4

    Save to a file

    Redirect output to your project.

    Terminal

    curl -s https://api.example.com/users/1 \
      | npx typemorph-cli zod --root User > src/schemas/user.ts

No terminal? Use the web workbench

Paste JSON at typemorph.dev, pick "Zod" from the format dropdown, and copy the schema. Works 100% in your browser — no signup, no data upload.

Open TypeMorph Workbench

Frequently asked

What Zod version does TypeMorph output?

Zod v4 by default (using z.email(), z.uuid(), z.iso.datetime()). Select "Zod v3" in the format picker or pass --format zod-v3 in the CLI for the legacy import from "zod".

Does it handle nested objects and arrays?

Yes. Nested objects become z.object({...}), arrays become z.array(...), and type inference runs recursively through every level.

What if the API returns different shapes on different calls?

TypeMorph infers from the single sample you provide. For polymorphic responses, run the command on a few representative samples and merge the outputs manually.

Can I generate TypeScript interfaces instead of Zod?

Yes — replace zod with typescript: npx typemorph-cli typescript schema.json. Over 160 output formats are supported.

Related guides