Zod
About
Zod is a TypeScript-first schema validation library with static type inference.
The Zod plugin for Hey API generates schemas from your OpenAPI spec, fully compatible with validators, transformers, and all core features.
Features
- Zod v4 support
- seamless integration with
@hey-api/openapi-ts
ecosystem - Zod schemas for requests, responses, and reusable definitions
Installation
In your configuration, add zod
to your plugins and you'll be ready to generate Zod artifacts. 🎉
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
'zod',
],
};
SDKs
To add data validators to your SDKs, set sdk.validator
to true
.
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
'zod',
{
name: '@hey-api/sdk',
validator: true,
},
],
};
Learn more about data validators in your SDKs on the SDKs page.
Output
The Zod plugin will generate the following artifacts, depending on the input specification.
Requests
A single request schema is generated for each endpoint. It may contain a request body, parameters, and headers.
const zData = z.object({
body: z
.object({
foo: z.optional(z.string()),
bar: z.optional(z.union([z.number(), z.null()])),
})
.optional(),
path: z.object({
baz: z.string(),
}),
query: z.optional(z.never()),
});
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
{
name: 'zod',
requests: true,
},
],
};
TIP
If you need to access individual fields, you can do so using the .shape
API. For example, we can get the request body schema with zData.shape.body
.
You can customize the naming and casing pattern for requests
schemas using the .name
and .case
options.
Responses
A single Zod schema is generated for all endpoint's responses. If the endpoint describes multiple responses, the generated schema is a union of all possible response shapes.
const zResponse = z.union([
z.object({
foo: z.optional(z.string()),
}),
z.object({
bar: z.optional(z.number()),
}),
]);
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
{
name: 'zod',
responses: true,
},
],
};
You can customize the naming and casing pattern for responses
schemas using the .name
and .case
options.
Definitions
A Zod schema is generated for every reusable definition from your input.
const zFoo = z.int();
const zBar = z.object({
bar: z.optional(z.array(z.int())),
});
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
{
name: 'zod',
definitions: true,
},
],
};
You can customize the naming and casing pattern for definitions
schemas using the .name
and .case
options.
Metadata
It's often useful to associate a schema with some additional metadata for documentation, code generation, AI structured outputs, form validation, and other purposes. If this is your use case, you can set metadata
to true
to generate additional metadata about schemas.
export const zFoo = z.string().register(z.globalRegistry, {
description: 'Additional metadata',
});
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
{
name: 'zod',
metadata: true,
},
],
};
Types
In addition to Zod schemas, you can generate schema-specific types. These can be generated for all schemas or for specific resources.
export type ResponseZodType = z.infer<typeof zResponse>;
export default {
input: 'https://get.heyapi.dev/hey-api/backend',
output: 'src/client',
plugins: [
// ...other plugins
{
name: 'zod',
types: {
infer: false, // by default, no `z.infer` types
},
responses: {
types: {
infer: true, // `z.infer` types only for response schemas
},
},
},
],
};
You can customize the naming and casing pattern for schema-specific types
using the .name
and .case
options.
API
You can view the complete list of options in the UserConfig interface.
Examples
You can view live examples on StackBlitz.
Sponsors
Help Hey API stay around for the long haul by becoming a sponsor.