Open API Spec validation and client generation tooling for Typescript
oax is a TypeScript-first API client generator that creates fully type-safe HTTP clients with runtime validation using Zod schemas. It combines the power of OpenAPI specifications, Zod validation, and the ky HTTP client for a seamless development experience.
- π Type-safe API clients generated from OpenAPI specifications
- β Runtime validation of requests and responses using Zod schemas
- π‘οΈ Automatic input/output validation with developer-friendly error messages
- π§ Validation control - enable/disable validation as needed
- π― Zero configuration - validation works out of the box
- π¦ Built on ky - modern, lightweight HTTP client
- π Backwards compatible - existing code continues to work
oax consists of three main packages:
- @oax/core - Core runtime library for API clients with validation
- @oax/cli - Command-line tool for generating typed API clients from OpenAPI specs
- @oax/hooks - React Query v5 hooks for seamless React integration
# Core runtime library
npm install @oax/core
# CLI for code generation
npm install -g @oax/cli
# React hooks (requires React 18+ and TanStack Query v5)
npm install @oax/hooks @tanstack/react-query# Using pnpm
pnpm add @oax/core
pnpm add -g @oax/cli
pnpm add @oax/hooks @tanstack/react-query
# Using yarn
yarn add @oax/core
yarn global add @oax/cli
yarn add @oax/hooks @tanstack/react-queryGenerate your API client (validation enabled by default):
import { createClient } from './generated-client';
const client = createClient('https://api.example.com');
// All requests and responses are automatically validated
const response = await client.getPet({ petId: 123 });const client = createClient('https://api.example.com', {
validate: false // Turns off all validation
});- Request Parameters: Validates path, query, and header parameters against their Zod schemas
- Request Body: Validates request body data against Zod schemas
- Response Data: Validates API response data against expected Zod schemas
- Default Behavior: Validation is enabled by default
- Opt-out: Pass
validate: falseto disable validation - Custom Hooks: Export validation helpers for custom ky hook configuration
- Console-readable error format with emojis and detailed context
- Shows validation path and specific error messages
- Includes the actual data that failed validation
import {
createValidationHelpers,
createKyValidationHooks,
ValidationError
} from '@oax/core';
import { createClient } from './generated-client';
const helpers = createValidationHelpers();
const kyHooks = createKyValidationHooks(helpers);
// Extend existing client with custom hooks
const client = createClient('https://api.example.com', {
validate: false // Turns off all validation
hooks: {
beforeRequest: [kyHooks.beforeRequest],
afterResponse: [kyHooks.afterResponse]
}
});When validation fails, a ValidationError is thrown with:
type: "request" or "response"operation: The operation ID that failedzodError: The underlying Zod validation errordata: The actual data that failed validation
π« Request validation error in operation: createPet
Validation errors:
β’ name: Required
β’ age: Expected number, received string
Data received: {
"name": "",
"age": "invalid"
}
-
Request Validation: Before sending the request
- Validates parameters against their schemas
- Validates request body against schema
- Throws
ValidationErrorif validation fails
-
Response Validation: After receiving the response
- Validates response data against expected schema
- Logs errors to console for debugging
- Returns validated data
- Validation only runs when
validate: true(default) - Uses Zod's efficient
safeParsemethod - Minimal overhead when validation is disabled
createValidationHelpers(): Creates validation helper functionscreateKyValidationHooks(helpers): Creates ky hooks for validationValidationError: Error class for validation failuresValidationHelpers: Interface for validation helper functionsKyValidationHooks: Interface for ky validation hooks
oax includes comprehensive examples to help you get started:
- Basic Example - Complete implementation using the Petstore API with TypeScript
- Hooks Example - React integration with TanStack Query v5 hooks
Each example includes:
- Full working code with TypeScript
- Generated API clients
- Development server setup
- Comprehensive documentation
- Node.js >= 18.0.0
- pnpm (recommended package manager)
# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run tests
pnpm test
# Start development mode
pnpm devoax/
βββ packages/
β βββ core/ # Core runtime library (@oax/core)
β βββ cli/ # Code generation CLI (@oax/cli)
β βββ hooks/ # React Query hooks (@oax/hooks)
βββ examples/
βββ basic/ # Basic TypeScript example
βββ hooks/ # React hooks example
All existing code continues to work without changes. Validation is additive and doesn't break existing functionality.
MIT