Skip to main content

Goal

Run one or more middleware functions before an HTTP handler executes. Middleware can inspect the request and either continue to the handler or short-circuit with a response.

How It Works

Middleware in iii is just a regular function. The engine calls it before the handler. The function returns either { action: "continue" } to proceed, or { action: "respond", response: {...} } to short-circuit. There are two ways to attach middleware:

Per-Route Middleware

1. Register the middleware function

Middleware functions receive a request object with path_params, query_params, headers, and method (no body).
auth-middleware.ts

2. Attach middleware to the trigger

Include middleware_function_ids in the trigger config. Middleware runs in the order listed.
auth-middleware.ts (continued)

3. Test it

Chaining Multiple Middleware

List multiple function IDs. They execute in order. If any short-circuits, the rest are skipped.

Global Middleware

Global middleware runs on every HTTP request, before route-level conditions and per-route middleware. Configure it in iii-config.yaml:
iii-config.yaml
Register the global middleware functions in a worker, just like any other function:

Middleware Response Protocol

Every middleware function must return one of:

Middleware Input

Middleware receives a lightweight request object (no body, for performance):
Middleware does not receive the request body. This is intentional: global middleware runs before body parsing, so auth checks and rate limiting skip the expensive JSON parse for rejected requests. Use conditions for body-based validation.

Request Lifecycle

Error Handling