Skip to main content
Express is the long-standing web framework for Node.js, and Next.js is a React framework that also serves backend routes. Both are built around middleware: a function registered on matching requests before the route handler runs. TrustGuard middleware evaluates the HTTP request body before the handler. It does not see content added by the handler or model calls made outside the request.

Integration capabilities

Your middleware enforces the verdict. Return a response when isBlocked is true, and assign transformedPayload back to req.body when present. Logging a verdict and calling next() provides monitoring only.

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:
The following lines determine what the middleware evaluates and enforces:
  • 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. transformedPayload is 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:
Set 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.
Fail-closed handling makes the AI route unavailable when evaluation fails. Fail-open handling lets the request proceed without inspection. Choose the behavior per route and set a timeout that does not hold requests open indefinitely.

5. Verify

  1. Send a POST to a guarded route with a jailbreak string in the body.
  2. Confirm the event in TrustGuard Activity, under the consumerId you sent.
  3. Reconcile the request with the console using traceId from the response. It is the same identifier the finding carries in Activity.
In Observe mode, this returns the normal 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