The following example demonstrates how openapi-zod-client transforms a standard OpenAPI 3.0 YAML specification into a fully typed Zodios client.
- Input: An OpenAPI 3.0 YAML file defining paths (e.g.,
/pets, /pets/{petId}), parameters, and schemas (e.g., Pet, Error). - Output: A TypeScript file containing:
zod schemas for all components.- An
endpoints definition created via makeApi. - A
Zodios instance representing the API. - A helper function
createApiClient to instantiate the client with a base URL.
import { makeApi, Zodios } from "@zodios/core";
import { z } from "zod";
const Pet = z.object({ id: z.number().int(), name: z.string(), tag: z.string().optional() });
const Pets = z.array(Pet);
const Error = z.object({ code: z.number().int(), message: z.string() });
export const schemas = {
Pet,
Pets,
Error,
};
const endpoints = makeApi([
{
method: "get",
path: "/pets",
requestFormat: "json",
parameters: [
{
name: "limit",
type: "Query",
schema: z.number().int().optional(),
},
],
response: z.array(Pet),
},
{
method: "post",
path: "/pets",
requestFormat: "json",
response: z.void(),
},
{
method: "get",
path: "/pets/:petId",
requestFormat: "json",
parameters: [
{
name: "petId",
type: "Path",
schema: z.string(),
},
],
response: Pet,
},
]);
export const api = new Zodios(endpoints);
export function createApiClient(baseUrl: string) {
return new Zodios(baseUrl, endpoints);
}