Middleware
Apply logging, dependencies, error reporting, and data protection across your functions from one place.
Add a database client, error reporter, or encryption behavior once instead of copying setup into each handler.
Common uses
- Dependency injection adds a shared database or API client to typed function context.
- Encryption middleware protects event data, step data, and function output before they reach Inngest.
- Sentry middleware sends function errors and trace context to Sentry.
- Creating middleware shows how to write and register your own.
How middleware works
- Where it runs: around a request, a function handler, a step, or an event send.
- Scope: register it on an Inngest client to apply it to that app's functions, or on one function for a narrower scope.
- Structure: in TypeScript v4, a middleware extends
Middleware.BaseMiddlewareand defines only the hooks it needs. The SDK creates a fresh instance for each request. - Wrapping hooks must call
next()so the wrapped work continues. Code before and afternext()runs on either side of that work. With several middleware, the hooks nest.
Check hook order and arguments before changing retries, step output, or event data.
Use it safely
- Logging and metrics: use observable hooks. The SDK logs their errors without failing the run.
- Final failures:
onRunErrorcan run on every errored attempt. Use a function'sonFailurehandler for work that should happen only once, after the final failure. - Business actions: put anything that must be durable in named steps, so a retry reuses completed work.
SDK support
- TypeScript v4 uses middleware classes. The v3
InngestMiddlewareAPI doesn't work in a v4 app. - Python has its own middleware lifecycle and versioned hooks.
- Go support is planned.