> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xano.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Middleware

> Middleware is used to add additional logic that executes before or after other logic.

<Tip>
  Before continuing, make sure you're familiar with:

  * [Core Components](/building/logic/core-components)
  * [Working with Data](/building/logic/working-with-data)

  You'll also want to make sure you've already built something to apply middleware to.
</Tip>

Middleware enables additional control of your logic at pivotal points of execution. They are separate pieces of logic that can run before the logic executes (before input validation) or after the logic executes (after the response is generated, but before it is delivered).

Middleware can be applied to:

* APIs
* Custom Functions
* Background Tasks
* AI Tools

Middleware is available on any paid plan.

There are two types of middleware:

* **Pre-middleware** that runs before input validation
* **Post-middleware** that runs after the logic executes and a response is generated, but before it is delivered

You'll need to understand response and exception types when building your middleware.

Response types are used to determine how the middleware handles generating a response once it's done executing.

| Response Type | Description                                                                                                                                                                        |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `merge`       | Merges the response of the middleware with the existing response. If the middleware response contains a key that already exists in the generated response, it will be overwritten. |
| `replace`     | Replaces the existing response entirely with the new response                                                                                                                      |

Exception methods are used to determine how the middleware handles errors that occur during execution.

| Exception Method | Description                                                                                                                                                   |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `critical`       | Stops execution completely and returns an error                                                                                                               |
| `silent`         | Silently ignores errors                                                                                                                                       |
| `rethrow`        | When paired with post-middleware, it allows the post-middleware to run even when an error occurs in the pre-middleware. Good for error logging or monitoring. |

Middleware has predefined inputs that can not be changed. These inputs are:

* `vars` - The variables from the parent object. This could be either the inputs sent to the workflow for a pre-middleware, or the response generated by the parent object for a post-middleware.
* `type` - The type of middleware (pre or post).

## Building Middleware in Xano

<Steps>
  <Step title="Open the Middleware library">
    From the sidebar, click <span class="ui-bubble">Middleware</span>.
  </Step>

  <Step title="Click Add Middleware">
    Click the <span class="ui-bubble"><Icon icon="plus" /> Add Middleware</span> button in the top-right corner.
  </Step>
</Steps>

<Tabs>
  <Tab title="Visually" icon="hammer-brush">
    <Steps>
      <Step title="Provide some basic information">
        Give your middleware a name, description, and any tags you'd like to apply. Choose your response type and exception method.

        <Frame caption="Providing basic information for Middleware">
          <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-113051.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=0ce9624326f5daa761ec715c4a306998" alt="middleware-20251014-113051" width="488" height="654" data-path="images/middleware-20251014-113051.png" />
        </Frame>
      </Step>

      <Step title="Build the middleware logic" />
    </Steps>
  </Tab>

  <Tab title="XanoScript" icon="code">
    A basic middleware in XanoScript has three parts — **inputs**, **functions**, and **response** — just like in the visual builder:

    ```javascript lines icon="code" Example of a basic middleware in XanoScript theme={null}
    middleware check_banned_user {
      description = "Checks to see if a banned user is attempting to perform any action, and if so, blocks it"

      input {
        json vars
        enum type {
          values = ["pre", "post"]
        }
      }

      stack {
        db.get user {
          field_name = "id"
          field_value = $auth.id
        } as $user1

        precondition ($user1.banned == false) {
          error_type = "unauthorized"
          error = "Your account has been suspended."
        }
      }

      response {
        value = {user1: $user1}
      }

      response_strategy = "merge"
      exception_policy = "critical"
      tags = ["user actions"]
    ```

    <Card title="Learn how to build middleware in XanoScript" icon="code" horizontal href="/xanoscript/middleware">
      For more information on how to build middleware in XanoScript, see the XanoScript Middleware documentation.
    </Card>
  </Tab>
</Tabs>

Once you've build your middleware, you'll need to apply it to the workflows you want it to run with.

## Applying Middleware to Workflows

Middleware can be applied at the workspace level, workflow group level (like API groups), or onto individual workflows. If no customizations are set on a workflow, it will inherit middleware settings from their parent object, such as the group they reside in or the workspace.

You can quickly disable middleware by clicking the toggle <span class="ui-bubble"><Icon icon="toggle-off" /></span> and remove it completely from the object by clicking the <span class="ui-bubble"><Icon icon="trash" /></span>.

### Workspace level

From your <span class="ui-bubble"><Icon icon="objects-column" /> Dashboard</span>, click⚙️ and choose <span class="ui-bubble">Middleware</span> from the menu.

<Frame caption="Accessing the Middleware menu on your workspace">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-114300.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=96ead7d22a1cbf2af2c4efb8fae4af3b" alt="middleware-20251014-114300" width="513" height="334" data-path="images/middleware-20251014-114300.png" />
</Frame>

<br />

<Frame caption="Select the workflow type you want to apply the middleware to.">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-113949.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=b8f147f203ffe1fd07be17cc76d1cf7e" alt="middleware-20251014-113949" width="489" height="308" data-path="images/middleware-20251014-113949.png" />
</Frame>

<br />

<Frame caption="Select the appropriate option to apply either pre or post middleware.">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-114037.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=76667edb019decf7d65cbda3e20193f6" alt="middleware-20251014-114037" width="481" height="301" data-path="images/middleware-20251014-114037.png" />
</Frame>

### Applying middleware to a workflow group

<Tip>
  Group level middleware only applies to APIs.
</Tip>

From an API group, click <span class="ui-bubble"><Icon icon="ellipsis-vertical" /></span> and select <span class="ui-bubble">Middleware</span>.

<Frame caption="Accessing the Middleware menu on an API group">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-114329.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=c0f766e19cb51cc57e4976660b3e7fb8" alt="middleware-20251014-114329" width="488" height="394" data-path="images/middleware-20251014-114329.png" />
</Frame>

<br />

<Frame caption="Check the Customize box next to the type of middleware you want to apply to that API group. You'll see the Add buttons appear once you do.">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-114401.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=d179082d61aa0a5f666b8f02e160704c" alt="middleware-20251014-114401" width="486" height="495" data-path="images/middleware-20251014-114401.png" />
</Frame>

### Applying middleware to an individual workflow

From the workflow, click <span class="ui-bubble"><Icon icon="ellipsis" /></span> and select <span class="ui-bubble">Middleware</span>.

<Frame caption="Accessing the Middleware menu on a workflow">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-114536.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=f98abee9a85d592b953d2089bb3cd59b" alt="middleware-20251014-114536" width="534" height="340" data-path="images/middleware-20251014-114536.png" />
</Frame>

<br />

<Frame caption="Check the Customize box next to the type of middleware you want to apply to that workflow. You'll see the Add buttons appear once you do.">
  <img src="https://mintcdn.com/xano-997cb9ee/NgWyYUIOE6OPGYha/images/middleware-20251014-114401.png?fit=max&auto=format&n=NgWyYUIOE6OPGYha&q=85&s=d179082d61aa0a5f666b8f02e160704c" alt="middleware-20251014-114037" width="486" height="495" data-path="images/middleware-20251014-114401.png" />
</Frame>
