···11+---
22+title: Zod
33+description: Zod plugin for Hey API. Compatible with all our features.
44+---
55+66+<script setup lang="ts">
77+// import { embedProject } from '../../embed'
88+import ZodHeading from './ZodHeading.vue';
99+</script>
1010+1111+<ZodHeading />
1212+1313+### About
1414+1515+[Zod](https://zod.dev) is a TypeScript-first schema validation library with static type inference.
1616+1717+<!-- ### Demo
1818+1919+<button class="buttonLink" @click="(event) => embedProject('hey-api-client-fetch-plugin-zod-example')(event)">
2020+Launch demo
2121+</button> -->
2222+2323+## Features
2424+2525+- seamless integration with `@hey-api/openapi-ts` ecosystem
2626+- Zod schemas for requests, responses, and reusable definitions
2727+2828+## Installation
2929+3030+In your [configuration](/openapi-ts/get-started), add `zod` to your plugins and you'll be ready to generate Zod artifacts. :tada:
3131+3232+```js
3333+export default {
3434+ input: 'https://get.heyapi.dev/hey-api/backend',
3535+ output: 'src/client',
3636+ plugins: [
3737+ // ...other plugins
3838+ 'zod', // [!code ++]
3939+ ],
4040+};
4141+```
4242+4343+### SDKs
4444+4545+To add data validators to your SDKs, set `sdk.validator` to `true`.
4646+4747+```js
4848+export default {
4949+ input: 'https://get.heyapi.dev/hey-api/backend',
5050+ output: 'src/client',
5151+ plugins: [
5252+ // ...other plugins
5353+ 'zod',
5454+ {
5555+ name: '@hey-api/sdk', // [!code ++]
5656+ validator: true, // [!code ++]
5757+ },
5858+ ],
5959+};
6060+```
6161+6262+Learn more about data validators in your SDKs on the [SDKs](/openapi-ts/output/sdk#validators) page.
6363+6464+## Output
6565+6666+The Zod plugin will generate the following artifacts, depending on the input specification.
6767+6868+## Requests
6969+7070+A single request schema is generated for each endpoint. It may contain a request body, parameters, and headers.
7171+7272+::: code-group
7373+7474+```js [config]
7575+export default {
7676+ input: 'https://get.heyapi.dev/hey-api/backend',
7777+ output: 'src/client',
7878+ plugins: [
7979+ // ...other plugins
8080+ {
8181+ name: 'zod',
8282+ requests: true, // [!code ++]
8383+ },
8484+ ],
8585+};
8686+```
8787+8888+```ts [output]
8989+const zData = z.object({
9090+ body: z
9191+ .object({
9292+ foo: z.string().optional(),
9393+ bar: z.union([z.number(), z.null()]).optional(),
9494+ })
9595+ .optional(),
9696+ path: z.object({
9797+ baz: z.string(),
9898+ }),
9999+ query: z.never().optional(),
100100+});
101101+```
102102+103103+:::
104104+105105+::: tip
106106+If you need to access individual fields, you can do so using the [`.shape`](https://zod.dev/api?id=shape) API. For example, we can get the request body schema with `zData.shape.body`.
107107+:::
108108+109109+You can customize the naming and casing pattern for `requests` schemas using the `.name` and `.case` options.
110110+111111+## Responses
112112+113113+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.
114114+115115+::: code-group
116116+117117+```js [config]
118118+export default {
119119+ input: 'https://get.heyapi.dev/hey-api/backend',
120120+ output: 'src/client',
121121+ plugins: [
122122+ // ...other plugins
123123+ {
124124+ name: 'zod',
125125+ responses: true, // [!code ++]
126126+ },
127127+ ],
128128+};
129129+```
130130+131131+```ts [output]
132132+const zResponse = z.union([
133133+ z.object({
134134+ foo: z.string().optional(),
135135+ }),
136136+ z.object({
137137+ bar: z.number().optional(),
138138+ }),
139139+]);
140140+```
141141+142142+:::
143143+144144+You can customize the naming and casing pattern for `responses` schemas using the `.name` and `.case` options.
145145+146146+## Definitions
147147+148148+A Zod schema is generated for every reusable definition from your input.
149149+150150+::: code-group
151151+152152+```js [config]
153153+export default {
154154+ input: 'https://get.heyapi.dev/hey-api/backend',
155155+ output: 'src/client',
156156+ plugins: [
157157+ // ...other plugins
158158+ {
159159+ name: 'zod',
160160+ definitions: true, // [!code ++]
161161+ },
162162+ ],
163163+};
164164+```
165165+166166+```ts [output]
167167+const zFoo = z.number().int();
168168+169169+const zBar = z.object({
170170+ bar: z.array(z.number().int()).optional(),
171171+});
172172+```
173173+174174+:::
175175+176176+You can customize the naming and casing pattern for `definitions` schemas using the `.name` and `.case` options.
177177+178178+## Metadata
179179+180180+It's often useful to associate a schema with some additional [metadata](https://zod.dev/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.
181181+182182+::: code-group
183183+184184+```js [config]
185185+export default {
186186+ input: 'https://get.heyapi.dev/hey-api/backend',
187187+ output: 'src/client',
188188+ plugins: [
189189+ // ...other plugins
190190+ {
191191+ name: 'zod',
192192+ metadata: true, // [!code ++]
193193+ },
194194+ ],
195195+};
196196+```
197197+198198+```ts [output]
199199+export const zFoo = z.string().describe('Additional metadata');
200200+```
201201+202202+:::
203203+204204+## Types
205205+206206+In addition to Zod schemas, you can generate schema-specific types. These can be generated for all schemas or for specific resources.
207207+208208+::: code-group
209209+210210+```js [config]
211211+export default {
212212+ input: 'https://get.heyapi.dev/hey-api/backend',
213213+ output: 'src/client',
214214+ plugins: [
215215+ // ...other plugins
216216+ {
217217+ name: 'zod',
218218+ types: {
219219+ infer: false, // by default, no `z.infer` types [!code ++]
220220+ },
221221+ responses: {
222222+ types: {
223223+ infer: true, // `z.infer` types only for response schemas [!code ++]
224224+ },
225225+ },
226226+ },
227227+ ],
228228+};
229229+```
230230+231231+```ts [output]
232232+export type ResponseZodType = z.infer<typeof zResponse>;
233233+```
234234+235235+:::
236236+237237+You can customize the naming and casing pattern for schema-specific `types` using the `.name` and `.case` options.
238238+239239+## Config API
240240+241241+You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/zod/types.d.ts) interface.
242242+243243+<!--@include: ../../../partials/examples.md-->
244244+<!--@include: ../../../partials/sponsors.md-->
+2-5
docs/openapi-ts/plugins/zod/v3.md
···5566<script setup lang="ts">
77// import { embedProject } from '../../embed'
88-import ZodVersionSwitcher from './ZodVersionSwitcher.vue';
88+import ZodHeading from './ZodHeading.vue';
99</script>
10101111-<div class="heading-with-version">
1212- <h1>Zod</h1>
1313- <ZodVersionSwitcher />
1414-</div>
1111+<ZodHeading />
15121613### About
1714