Quickstart
Full walkthrough of installing and running the generator from scratch.
This guide is for developers who already have a TypeScript project with an OpenAPI generator set up and want to switch to @codewithagents/openapi-gen. It covers the mental model shift, the step-by-step switch, and what to watch for.
Many OpenAPI generators follow a pattern where the generated code depends on a runtime client library that the generator itself publishes. Your code calls into that runtime, and the generated types describe its API. The generator ships a package to dependencies, not just devDependencies.
This toolchain works differently:
models.ts, client.ts, client-config.ts, and (optionally) schemas.ts live in your repository. They are plain TypeScript you can read, extend, and commit like any other file.@codewithagents/openapi-gen) is a devDependency. The generated client.ts uses only native fetch. Nothing is added to your production bundle that your project did not already have.input_schema to your config, the generator bootstraps a Zod schemas.ts file once, then never overwrites it. You add error messages and business rules. The generator warns on drift but your schema stays yours.Schema.parse(body) before sending and Schema.parse(await res.json()) after receiving.Remove the old generator.
Uninstall the previous generator and any runtime packages it required:
# example: remove hey-api packagesnpm uninstall @hey-api/client-fetch @hey-api/openapi-ts
# example: remove orvalnpm uninstall orvalDelete the old generated output directory and any existing generator config files (orval.config.ts, openapi-ts.config.ts, etc.).
Install @codewithagents/openapi-gen.
npm install -D @codewithagents/openapi-genIf you also want React Query hooks, add the hooks package:
npm install -D @codewithagents/openapi-react-queryCreate the config file.
Create openapi-gen.config.json in your project root:
{ "input_openapi": "./openapi.json", "output": "./src/api"}Optional fields for a fuller setup:
{ "input_openapi": "./openapi.json", "output": "./src/api", "input_schema": "./src/api/schemas.ts", "baseUrl": "https://api.example.com", "server_client": true}| Field | Required | Description |
|---|---|---|
input_openapi | Yes | Path to your OpenAPI spec (JSON or YAML) |
output | Yes | Directory to write the generated files |
input_schema | No | Path to a user-owned Zod schema file. Bootstrapped on first run, never overwritten after that. |
baseUrl | No | Default base URL embedded in client-config.ts |
server_client | No | When true, also generates server.ts with a createServerClient() factory |
Run the generator.
npx openapi-genThis writes the following files to your output directory:
| File | Contents |
|---|---|
models.ts | TypeScript interfaces for every schema |
client.ts | One typed async function per API operation |
client-config.ts | configureClient() and getConfig() |
index.ts | Barrel re-export of all three files above |
Update your imports.
Replace imports from the old generator’s runtime with imports from your generated barrel:
// Before: importing from a runtime packageimport { createClient } from '@hey-api/client-fetch'
// After: importing from your own generated codeimport { configureClient, listTasks, ApiError } from './src/api'Call configureClient once at app startup:
import { configureClient } from './src/api'
configureClient({ baseUrl: 'https://api.example.com', token: () => getAccessToken(), // sync or async, called per request})Add a generate script to package.json.
{ "scripts": { "generate": "openapi-gen" }}// orval.config.ts (illustrative, your config may differ)import { defineConfig } from 'orval'
export default defineConfig({ myApi: { input: './openapi.json', output: { target: './src/api/client.ts', client: 'react-query', }, },})// package.json dependencies (illustrative){ "devDependencies": { "orval": "^7.x" }, "dependencies": { "axios": "^1.x" }}{ "input_openapi": "./openapi.json", "output": "./src/api"}{ "scripts": { "generate": "openapi-gen" }, "devDependencies": { "@codewithagents/openapi-gen": "^1.0.0" }}Quickstart
Full walkthrough of installing and running the generator from scratch.
Types and fetch client
Complete configuration reference, generated file details, and advanced usage.
React Query hooks
Generate typed useQuery and useMutation hooks on top of the generated client.