Integration capabilities
Before you start
Create the policy in Observe mode. Observe records decisions in Activity without enforcing
them. Review the results, then switch the policy to Enforce. See
Policies.
1. Install the client
2. Mount the middleware
Build the client once, at module scope, and reuse it. Register the middleware before the routes it protects. Express runs middleware in registration order, so middleware mounted after a handler does not see that handler’s traffic:if (req.method !== "POST") return next();: the filter. Narrow it to the AI routes (app.use("/api/chat", …)) so unrelated endpoints do not pay the latency, and so a large non-AI upload is not serialized into an evaluation.JSON.stringify(req.body): evaluates the complete request body as received, not the prompt your handler will build from it.if (response.isBlocked): without it, the finding is recorded and the request reaches the model anyway.req.body = response.transformedPayload: a DLP rule masks by rewriting the payload. Assign the returned value so the handler receives the rewritten body.transformedPayloadis absent unless a Transform rule changed the payload.
consumerId routes the request to a per-consumer policy and attributes the
finding to a person in Activity; sessionId groups the turns of one
conversation. The example reads them from an x-user-id header and a
session_id cookie. Replace them with the stable user and conversation
identifiers used by your service.
In Next.js, middleware runs on the Edge runtime. See Limits.
3. Cover the response direction
The middleware evaluates the request. Evaluate the model response separately in the route handler with the same client:direction on every call. It selects the
detector phase: input before the model and
output after it. The field defaults to input, so omitting it from the second
call prevents Output-phase rules from running. The payload field remains input
on an output call; direction identifies the phase.
If you stream the answer to the browser, tokens have already been delivered when
the assembled text becomes available for evaluation. An output-side block cannot
prevent delivery. Buffer the stream until a verdict is available if the route
requires preventive output enforcement. Input enforcement is unaffected.
4. Decide what an unreachable TrustGuard does
Handle timeouts, connection errors, and authentication failures explicitly. An unhandled rejection in asynchronous middleware can leave the request pending.5. Verify
- Send a POST to a guarded route with a jailbreak string in the body.
- Confirm the event in TrustGuard Activity, under the
consumerIdyou sent. - Reconcile the request with the console using
traceIdfrom the response. It is the same identifier the finding carries in Activity.
200 response and records the finding
in Activity. In Enforce mode, the same request returns
403 {"detail":"Blocked by TrustGuard"}.
Reference
Coverage
Use middleware to apply the same check to selected HTTP routes. Background jobs,
queue consumers, and internal calls bypass it; instrument those call sites with
the SDK.
⚠️ Your middleware enforces the verdict. See the enforcement branches in
Mount the middleware.
➖ Tool calls are outside the middleware scope. It sees HTTP requests, not
model or tool calls. Guard tool dispatch with the
SDK at the call site.
Limits. In Next.js, middleware runs on the Edge runtime by default. Confirm
that the deployment target supports the SDK or move the check into the route
handler. Output coverage requires a second evaluation. Input evaluation covers
the request body, not content that the handler adds later, such as a system
prompt, retrieved document, or another service’s response.
What is evaluated
Both calls hit
POST /v1/evaluate with the collector
key, and the policy’s detectors decide the verdict. TrustGuard returns HTTP 200
for a block verdict; the middleware in the example returns the 403 response.
Configuration
The middleware and the Node.js SDK use the same client
and collector key. Call the SDK around the model when you need application
context that is unavailable on the request path. Other runtimes:
Python middleware for FastAPI, Django and
Flask, Python SDK, or REST from
any HTTP client.
Attributes
Middleware can read both values from a header, cookie, or session layer. Mount it
after authentication to use a verified user identity instead of an untrusted
request header.
Troubleshooting
Related
- Node.js SDK: evaluate model calls and tool dispatch directly
- Evaluate API: request and response reference for
guard() - Policies: Observe and Enforce modes, gates, and policy phases
- Collectors: keys, policy routing, and per-consumer overrides
- Python middleware: apply the same pattern in FastAPI, Django, and Flask
- Coverage: compare available collectors
- Express middleware guide · Next.js middleware: framework references