@app.middleware("http") decorator. Every POST request with a JSON body is scanned before it reaches your endpoint, and every JSON response is scanned before it leaves your server — without changing any of your endpoint code.
Install
Steps
1
Register the middleware
Add the
inferwall_middleware function to your FastAPI app. The decorator intercepts all HTTP traffic.2
Scan the request body
The middleware reads the request body, extracts the text field (adapting to your field name), and calls
inferwall.scan_input(). Blocked requests receive an immediate 403 response; flagged requests are logged and allowed through.The middleware checks for
prompt, message, and text fields in the request body. Update the data.get() calls to match the field names in your own API.3
Forward the request to your endpoint
After the input scan, the middleware calls the next handler in the stack — your actual endpoint function.
4
Scan the response body
The middleware intercepts the response before it is sent. If the response body contains a
response or text field, InferenceWall scans it. Blocked responses return 451 Unavailable For Legal Reasons.What gets scanned automatically
Non-JSON requests and responses pass through without scanning.
Handling blocked requests
When InferenceWall blocks a request, your endpoint code never executes. The middleware returns the error response directly. The JSON body includes thedecision, score, and matched_signatures so clients can inspect the reason.
Complete example
Related
- API overview — run InferenceWall as a standalone HTTP server
- How InferenceWall works
- Policies and thresholds