You’ll also want to make sure you’ve already built something to apply middleware to.
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.
From the left-hand menu, click Library and then Middleware.
2
Click Add Middleware
Click the Add Middleware button in the top-right corner.
Visually
XanoScript
1
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.
2
Build the middleware logic
A basic middleware in XanoScript has three parts — inputs, functions, and response — just like in the visual builder:
Example of a basic middleware in XanoScript
Copy
Ask AI
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"]
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 and remove it completely from the object by clicking the .