Middleware
Middleware allows you code to run at various points in an Inngest client's lifecycle, such as during a function's execution or when sending an event.
This can be used for a wide range of uses:
Custom observability
Add custom logging, tracing or helpers to your Inngest Functions.
Dependency Injection
Provide shared client instances (ex, OpenAI) to your Inngest Functions.
Encryption Middleware
End-to-end encryption for events, step output, and function output.
Sentry Middleware
Quickly setup Sentry for your Inngest Functions.
Middleware SDKs support
Middleware are available in the TypeScript SDK v2.0.0+ and Python SDK v0.3.0+.
Support in the Go SDK in planned.
Middleware lifecycle
Middleware can be registered at the Inngest clients or functions level.
Adding middleware contributes to an overall "stack" of middleware. If you register multiple middlewares, the SDK will group and run hooks for each middleware in the following order:
- Middleware registered on the client, in descending order
- Middleware registered on the function, in descending order
For example:
const inngest = new Inngest({
id: "my-app",
middleware: [
logMiddleware, // This is executed first
errorMiddleware, // This is executed second
],
});
inngest.createFunction(
{
id: "example",
middleware: [
dbSetupMiddleware, // This is executed third
datadogMiddleware, // This is executed fourth
],
},
{ event: "test" },
async () => {
// ...
}
);
Learn more about the Middleware hooks and their execution order in "Creating a Middleware".