React Query hooks
Generate useQuery and useMutation hooks on top of this client.
This guide walks you through installing openapi-zod-ts and generating a typed fetch client from an OpenAPI spec. It takes about five minutes.
Install
npm i -D openapi-zod-tspnpm add -D openapi-zod-tsyarn add -D openapi-zod-tsAdd a config file
Create openapi-zod-ts.config.json in your project root:
{ "input_openapi": "./openapi.json", "output": "./src/api"}Point input_openapi at your spec file (JSON or YAML). The output directory is created if it does not exist.
The two required fields:
| Field | Description |
|---|---|
input_openapi | Path to your OpenAPI 3.x spec (JSON or YAML) |
output | Directory to write the generated files |
Optional fields let you set a default baseUrl, enable Zod schemas (input_schema), or generate a server-side client factory (server_client: true). See the full options reference.
Run the generator
npx openapi-zod-tsThis writes the following files to ./src/api/:
| File | Contents |
|---|---|
models.ts | TypeScript interfaces for every schema in components.schemas |
client.ts | One typed async function per API operation, using native fetch |
client-config.ts | configureClient() and getConfig(): set base URL and auth once at startup |
index.ts | Barrel re-export of all three files above |
Use the generated client
Call configureClient once at app startup, then import typed functions anywhere:
import { configureClient } from './src/api'import { listTasks, createTask, ApiError } from './src/api'
// Call once at app startup (e.g. main.ts or App.tsx)configureClient({ baseUrl: 'https://api.example.com', token: () => getAccessToken(), // sync or async, called per request})
// Every function is fully typed: parameters, return type, and error shapeconst page = await listTasks({ status: 'pending', page: 1 })
try { const task = await createTask({ title: 'Ship it' }) console.log(task.id)} catch (err) { if (err instanceof ApiError) { console.error(err.status, err.body) }}You have a fully typed fetch client. Layer in hooks, a server interface, or error mapping as your project grows:
React Query hooks
Generate useQuery and useMutation hooks on top of this client.
Server interface
Generate a typed service interface and optional Hono, Express, or Fastify router.
Form error mapping
Map ApiError responses from mutation hooks directly to form field errors.
MSW mocks
Generate seeded MSW v2 handlers from the same spec, so tests and local dev hit realistic mock data.
Full-stack tutorial
See the generators and api-errors working together in the petstore-fastify demo.