Advanced: Middleware
v2.0.0+

Middleware allows you to specify functions to run at various points in an Inngest client's lifecycle, such as during a function's execution or when sending an event. Use the InngestMiddleware class to define new middleware.

import { Inngest, InngestMiddleware } from "inngest";

const myMiddleware = new InngestMiddleware({
  name: "My Middleware",
  init() {
    return {
      onFunctionRun({ fn }) {
        // This will be logged whenever a function is executed
        console.log(`Function ${fn.name} is running!`);

        // This is where you can optionally specify hooks for this particular run
        return {
          afterExecution() {
            console.log(`Function ${fn.name} has finished running!`);
          },
        };
      },
    };
  },
});

const inngest = new Inngest({
  id: "my-app",
  middleware: [myMiddleware],
});

This can be used for a wide range of uses, for example:

  • Utilizing error-monitoring libraries (e.g. Sentry)
  • Adding logging support
  • Transforming input and output data (e.g. superjson)
  • Setting up DB connections before starting a function's execution
  • Using Inngest's step building blocks to build reusable patterns

Next steps

Check out some of the samples below for a quick look at how to use middleware:

Or see the next pages to walk through the details: