Skip to main content

Architecture Overview

Express Zod API builds on top of Express.js, adding a layer of type safety and automatic validation. Understanding how data flows through the system will help you build better APIs.
Express Zod API Data Flow Diagram

The Request Lifecycle

When a request hits your API, it goes through several stages:
1

Request Parsing

Express parses the incoming request, extracting:
  • request.body (JSON or form data)
  • request.query (URL query parameters)
  • request.params (path parameters like :id)
  • request.headers (HTTP headers)
  • request.files (uploaded files, if enabled)
2

Input Combination

Express Zod API combines configured input sources into a single input object based on the HTTP method:
3

Middleware Execution

Middlewares run in order, each receiving:
  • input: Validated input from previous middleware or request
  • request: The original Express request
  • response: The Express response object
  • logger: The configured logger
  • ctx: Context from previous middlewares
Each middleware can:
  • Validate additional input
  • Perform authentication/authorization
  • Add data to ctx for the endpoint handler
  • Throw errors to stop execution
4

Input Validation

The combined input is validated against the endpoint’s input schema:
If validation fails, a 400 Bad Request response is sent automatically.
5

Handler Execution

Your endpoint handler receives:
  • input: Fully validated and typed input
  • ctx: Context from middlewares
  • logger: Logger for this request
  • request: Original Express request (for advanced use)
  • response: Express response (rarely needed)
The handler returns an output object or throws an error.
6

Output Validation

The handler’s output is validated against the output schema:
If validation fails, a 500 Internal Server Error is sent (this indicates a bug in your code).
7

Response Formatting

The ResultHandler formats the response:
  • Success: Wraps output in a standard format (default: { status: "success", data: {...} })
  • Error: Formats errors consistently (default: { status: "error", error: { message: "..." } })
  • Sets appropriate HTTP status codes
  • Adds headers (CORS, content-type, etc.)

Core Components

1. Schemas (Zod)

Schemas define the shape and validation rules for your data:
Key features:
  • Type inference: TypeScript types are automatically derived
  • Transformations: Convert data types (e.g., string to number)
  • Refinements: Custom validation logic
  • Composition: Combine schemas with .merge(), .extend(), etc.

2. Endpoints

Endpoints are the core building blocks of your API:
Handler parameters:

3. Middlewares

Middlewares provide reusable logic that runs before endpoint handlers:
Attaching middlewares:

4. Factories

Factories create endpoints, optionally with pre-attached middlewares:

5. Result Handlers

Result handlers control how responses are formatted and sent:

6. Routing

Routing maps endpoints to URL paths:

7. Configuration

Configuration centralizes all server settings:

Data Flow Example

Let’s trace a request through the entire system:
1

Request arrives

2

Express parses request

3

Input sources combined

4

Middleware validates & adds context

5

Input validated against schema

6

Handler executes

7

Output validated

8

Result handler formats response

Type Safety Flow

One of Express Zod API’s biggest advantages is end-to-end type safety:

Error Handling Flow

Errors can occur at multiple stages:
All errors are caught and formatted by the ResultHandler.

Next Steps

Now that you understand how Express Zod API works:

Middleware Guide

Learn to create and chain middlewares

Advanced Validation

Master Zod schemas and transformations

Custom Result Handlers

Customize response formatting

API Documentation

Generate OpenAPI specs automatically