Skip to content
JSON

Generate Zod Schema from API Response

Paste a sample API response and generate a Zod schema for TypeScript, Next.js, Node.js, or another TypeScript project.

API response example

Start from a response body like this one, or paste JSON from your own API. Choose Load example in the converter to fill the input, then generate the schema.

{
  "id": 123,
  "name": "Aye",
  "email": "aye@example.com",
  "roles": ["admin", "user"],
  "profile": {
    "active": true
  }
}

How this sample becomes a Zod schema

The converter reads the pasted JSON with the same JSON to Zod logic used on the general tool. With the default root name RootSchema, this sample becomes a root object and a nested schema for profile.

id is a JSON number, so it becomes z.number(). name and email are strings, so both become z.string(). roles is an array of strings, so it becomes z.array(z.string()). profile is an object, so active becomes z.boolean() on a nested schema, and the root object references that schema. The file starts with import { z } from "zod"; and exports the root schema. Every key is required in this built-in sample only, because that one object never omits a key.

Rename the root schema before you generate if you want a name that matches the endpoint. If you need TypeScript interfaces instead of a runtime Zod schema, use JSON to TypeScript.

What this tool can and cannot infer

One response describes the values in that paste. A later response can add fields, leave fields out, or use different types. The generated schema is a starting point for your project. It does not document or verify the API's full contract, and this page does not call your API.

A date written as text stays z.string() until you transform it. An email address or a string that looks like a UUID also stays z.string(). The converter does not add z.string().email() or a UUID check. A number stored as a string, such as "123", stays z.string() rather than becoming z.number().

A missing key becomes .optional() only when the pasted JSON is a top-level array of objects and at least one object omits that key. On one object, each key that is present is required. When an array is nested inside an object, such as orders, objects with different keys become a z.union of separate object schemas. You can merge those into one schema and add .optional() by hand. In a top-level array of objects, a field with different JSON types becomes z.union of those types. In a nested array, those objects become a z.union of separate object schemas. An empty array is inferred as z.array(z.null()), so a sample with empty arrays says nothing about their items.

Common questions

How do I generate a Zod schema from an API response?

Paste a JSON object or array from a sample response, set the root schema name if you want a different export, and choose Generate Zod schema. Conversion runs in your browser. Copy the result or download it as schema.ts.

Can I use the generated schema in Next.js?

Yes. The output imports z from "zod". Use it in a route handler, server action, or other TypeScript module after you install Zod in that project. The same file works in Node.js and other TypeScript codebases.

Does the tool validate the API response?

It checks that the pasted text is JSON, then writes a schema from that sample. It does not request the API. The schema checks data only after you call it from your own code, and one sample does not prove every response the API can return.

Can Zod infer optional fields from one response?

On one object, every present key is required. A missing key becomes .optional() only when the pasted JSON is a top-level array of objects and at least one object leaves that key out. When an array is nested inside an object, such as orders, objects with different keys become a z.union of separate object schemas. Merge those into one schema and add .optional() yourself if you need a single shape.

Does it detect dates, UUIDs, or email addresses automatically?

No. Dates, UUID-like strings, and email addresses stay z.string(). A numeric string stays z.string() as well. Add a tighter check in the generated schema if your project needs one.