# Middleware
Source: https://www.inngest.com/docs/features/middleware

import {
  RiEyeOffLine,
  RiMistFill,
  RiPlugLine,
  RiFileSearchLine,
} from "@remixicon/react";

Middleware allows your 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:

<CardGroup cols={2}>
  <Card title="Custom observability" icon={<RiFileSearchLine className="text-basis h-4 w-4"/>} href={'/docs/features/middleware/create'}>
    Add custom logging, tracing or helpers to your Inngest Functions.
  </Card>
  <Card title="Dependency Injection" icon={<RiPlugLine className="text-basis h-4 w-4" />} href={'/docs/features/middleware/dependency-injection'}>
    Provide shared client instances (ex, OpenAI) to your Inngest Functions.
  </Card>
  <Card title="Encryption Middleware" icon={<RiEyeOffLine className="text-basis h-4 w-4" />} href={'/docs/features/middleware/encryption-middleware'}>
    End-to-end encryption for events, step output, and function output.
  </Card>
  <Card title="Sentry Middleware" icon={<RiMistFill className="text-basis h-4 w-4"/>} href={'/docs/features/middleware/sentry-middleware'}>
    Quickly setup Sentry for your Inngest Functions.
  </Card>
</CardGroup>

## Middleware SDKs support

Middleware are available in the [TypeScript SDK](/docs/reference/middleware/typescript) <VersionBadge version={'v2.0.0+'} /> and [Python SDK](/docs/reference/python/middleware/lifecycle) <VersionBadge version={'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:

1. Middleware registered on the **client**, in descending order
2. Middleware registered on the **function**, in descending order

For example:

<CodeGroup>
```ts {{ title: "TypeScript" }}
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 () => {
    // ...
  }
);
```
```py {{ title: "Python" }}
inngest_client = inngest.Inngest(
    app_id="my_app",
    middleware=[
        LogMiddleware, # This is executed first
        ErrorMiddleware # This is executed second
    ],
)