Zod to OpenAPI
Generate OpenAPI and Swagger documentation from Zod schemas, compare zod-to-openapi package workflows, and choose OpenAPI 3.0 or 3.1 for TypeScript APIs.
Quick answer: how do you convert Zod to OpenAPI?
Use a Zod to OpenAPI package when you need a complete Swagger/OpenAPI document with paths, operations, request bodies, responses, and reusable components. Use FrameworkKit when you need to inspect or copy OpenAPI-compatible schema components from Zod before adding a package workflow to your codebase.
Generate a full OpenAPI document from Zod
A full zod to openapi workflow has three layers: Zod schemas for runtime validation, OpenAPI metadata for schema names and examples, and route definitions for paths, methods, request bodies, responses, tags, and security.
import { OpenAPIRegistry, OpenApiGeneratorV3 } from "@asteasolutions/zod-to-openapi";
import { z } from "zod";
const registry = new OpenAPIRegistry();
const UserSchema = registry.register(
"User",
z.object({
id: z.string().uuid(),
email: z.string().email(),
}),
);
registry.registerPath({
method: "get",
path: "/users/{id}",
responses: {
200: {
description: "User response",
content: {
"application/json": {
schema: UserSchema,
},
},
},
},
});
export const openApiDocument = new OpenApiGeneratorV3(registry.definitions).generateDocument({
openapi: "3.0.3",
info: { title: "Users API", version: "1.0.0" },
});Components schemas from Zod
When your immediate need is a component schema, convert Zod to an OpenAPI-compatible schema object first. This is the safest way to review nullable fields, required keys, enums, arrays, and extra-property behavior before adding that schema under `components.schemas`.
Paths, request bodies, and responses
OpenAPI docs need more than schemas. Add each API path with method, params, query, request body, response content, status codes, operation IDs, tags, and auth metadata so Swagger UI and SDK generators understand the API surface.
- Use Zod schemas for request bodies and response bodies at the API boundary.
- Register route params and query params explicitly instead of hiding them in application code.
- Add stable operation IDs when the OpenAPI document will feed SDK generation.
- Keep examples representative, but avoid pasting secrets or production-only payloads into public docs.
OpenAPI 3.0 vs OpenAPI 3.1 for Zod
Choose OpenAPI 3.0 when your docs, gateway, or SDK generator expects `nullable: true`. Choose OpenAPI 3.1 when your tooling understands modern JSON Schema unions such as `type: ["string", "null"]`.
// OpenAPI 3.0 style
{ "type": "string", "nullable": true }
// OpenAPI 3.1 style
{ "type": ["string", "null"] }Swagger UI and SDK generation
Once the document is generated, validate it before publishing it to Swagger UI, Redoc, Speakeasy, Stainless, OpenAPI Generator, or an API gateway. Treat generated docs as a contract, not just a prettier view of route code.
- Run OpenAPI validation in CI before publishing docs.
- Preview the generated document in Swagger UI or Redoc.
- Generate a small SDK/client fixture to catch operation ID and schema drift.
- Test valid, invalid, nullable, optional, and error response examples against the same Zod schemas.
When FrameworkKit fits the workflow
FrameworkKit is not a replacement for a route-aware zod to openapi package. It is useful before package setup, during schema review, or when a teammate needs a browser-only way to inspect the OpenAPI-compatible component shape.
Zod to OpenAPI workflow comparison
| Workflow | Best for | Tradeoff |
|---|---|---|
| FrameworkKit schema converter | Generating reviewed OpenAPI-compatible schema components from a supported Zod snippet. | It creates schema output, not a complete OpenAPI document with paths. |
| @asteasolutions/zod-to-openapi | Generating full OpenAPI documents in code with a registry, paths, request bodies, responses, and components. | Requires package setup, metadata annotations, and a build/runtime generation step. |
| zod-openapi | Metadata-driven OpenAPI workflows where schemas carry OpenAPI details beside Zod validation. | Strong for project integration, but still needs route/document wiring. |
| Native z.toJSONSchema | Converting Zod 4 schemas into JSON Schema-compatible component shapes. | Useful for schemas, but it does not generate OpenAPI paths, operations, or Swagger docs by itself. |
Use FrameworkKit to generate the starter code, then review the output before shipping it in production.
Zod to JSON Schema ConverterZod to JSON Schema resources
Zod to JSON Schema Converter
Use the canonical free online converter for Draft 2020-12, Draft 7, AJV, or OpenAPI-compatible output.
Zod to JSON Schema examples
Review object, enum, nullable, optional, array, and nested Zod conversion examples.
Convert Zod to JSON Schema with AJV
Convert Zod to JSON Schema, choose an Ajv draft, compile validators, and avoid unsupported Zod features.
Fastify Zod validation
Use Zod schemas with Fastify request validation, response schemas, type providers, and OpenAPI output.
Zod to JSON Schema for OpenAPI
Publish OpenAPI-compatible schema output from TypeScript-owned Zod contracts.
Zod refine vs superRefine
Review why custom refinements cannot be represented as plain JSON Schema without a separate validation step.
Zod API schemas
Use Zod at API request and response boundaries before publishing or validating contracts.
Zod vs JSON Schema
Decide when Zod, JSON Schema, OpenAPI, and AJV should own runtime validation contracts.
FAQ
Can Zod generate OpenAPI?
Zod can be the source of truth for OpenAPI, but a package usually does the document generation. Native Zod JSON Schema output helps with component schemas, while zod-to-openapi packages add routes, paths, responses, and document metadata.
What is the best Zod to OpenAPI package?
For full document generation, start by comparing `@asteasolutions/zod-to-openapi` and `zod-openapi`. Pick based on your route framework, metadata style, OpenAPI 3.0/3.1 needs, and how much generation should run in CI.
Is z.toJSONSchema enough for OpenAPI?
`z.toJSONSchema()` is useful for schema conversion, but it does not create OpenAPI paths, operations, request bodies, response objects, tags, security schemes, or a complete Swagger document.
Should I use OpenAPI 3.0 or 3.1 for Zod?
Use OpenAPI 3.0 for broad tooling compatibility and `nullable: true` output. Use OpenAPI 3.1 when your docs and generators support modern JSON Schema semantics.
Can I use this with Swagger UI?
Yes. Generate and validate the OpenAPI document first, then serve the JSON document through Swagger UI or Redoc. If you only have component schemas, wrap them in a complete OpenAPI document before publishing.
Related tools
JSON to Zod Converter
Convert JSON to Zod schemas with strict objects, optional field inference, and inferred TypeScript types.
TypeScript to Zod Converter
Convert TypeScript interfaces and type aliases into Zod schemas with inferred types in your browser.
OpenAPI to Zod
Turn OpenAPI schemas into Zod validators and lightweight typed fetch clients.