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.The Request Lifecycle
When a request hits your API, it goes through several stages: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)
Input Combination
Express Zod API combines configured input sources into a single
input object based on the HTTP method:Middleware Execution
Middlewares run in order, each receiving:
input: Validated input from previous middleware or requestrequest: The original Express requestresponse: The Express response objectlogger: The configured loggerctx: Context from previous middlewares
- Validate additional input
- Perform authentication/authorization
- Add data to
ctxfor the endpoint handler - Throw errors to stop execution
Input Validation
The combined input is validated against the endpoint’s input schema:If validation fails, a
400 Bad Request response is sent automatically.Handler Execution
Your endpoint handler receives:
input: Fully validated and typed inputctx: Context from middlewareslogger: Logger for this requestrequest: Original Express request (for advanced use)response: Express response (rarely needed)
output object or throws an error.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).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:- 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:| Parameter | Type | Description |
|---|---|---|
input | Validated input | Combines body, query, params based on method |
ctx | Context object | Data provided by middlewares |
logger | Logger instance | For logging (debug, info, warn, error) |
request | Express Request | Raw request object (advanced use) |
response | Express Response | Raw response object (advanced use) |
3. Middlewares
Middlewares provide reusable logic that runs before endpoint handlers: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: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:ResultHandler.