Skip to main content
One of Express Zod API’s most powerful features is the ability to generate TypeScript clients that provide complete end-to-end type safety between your backend API and frontend applications.

Overview

The Integration class generates TypeScript code containing:
  • Input/output types for all your endpoints
  • A fully-typed client for making API requests
  • Runtime validation of request and response data
  • Support for Server-Sent Events (SSE) subscriptions

Quick Start

Create a script to generate your client:
generate-client.ts
Run this script during your build process to keep your client in sync with your API.

Configuration Options

Basic Options

typescript
typeof ts
required
The TypeScript compiler API. Import from the typescript package.
routing
Routing
required
Your API routing configuration.
config
CommonConfig
required
Your API server configuration.
serverUrl
string
default:"https://example.com"
The base URL where your API is hosted.

Advanced Options

variant
'types' | 'client'
default:"client"
What to generate:
  • "types" - Only TypeScript types (for DIY solutions)
  • "client" - Full client with types and implementation
clientClassName
string
default:"Client"
Name for the generated client class.
subscriptionClassName
string
default:"Subscription"
Name for the generated subscription class (for SSE).
noContent
z.ZodType
default:"z.undefined()"
Schema for responses without body (like 204 No Content).
hasHeadMethod
boolean
default:true
Generate HEAD method for each GET endpoint (Express feature).
brandHandling
object
Custom handling rules for branded schemas. See the Integration class documentation for details.

Using the Generated Client

Basic Usage

The generated client provides type-safe methods for all your endpoints:
frontend-app.ts

Path Parameters

The client automatically substitutes path parameters:

Custom Implementation

You can provide a custom implementation function to use your preferred HTTP library:

Server-Sent Events (SSE)

For endpoints that use EventStreamFactory, use the generated Subscription class:

Pagination Support

The client includes a hasMore() method for paginated endpoints:

Formatting Options

Using Prettier

The printFormatted() method automatically uses Prettier if installed:
You can also provide custom formatting:

Without Formatting

For unformatted output:

Async Creation

If you want to avoid importing TypeScript yourself, use the async create() method:

Types-Only Generation

For DIY solutions where you want to implement your own client:
This generates:
  • Input types for all endpoints
  • Response types for all endpoints
  • Path and method type unions
  • Request/response interfaces

Complete Example

Here’s a full example with multiple features:
generate-client.ts
frontend-usage.ts
The generated client requires TypeScript 4.1 or higher to consume.

Benefits

Type Safety

Compile-time verification of request parameters and response handling

Auto-Complete

Full IDE support with autocomplete for all endpoints and their types

Refactoring

Changes to your API automatically surface as TypeScript errors in frontend

Documentation

Types serve as inline documentation for API consumers

Next Steps