Skip to main content
Fastly provides CDN caching, DDoS protection, and WAF services in front of websites and APIs. Compute runs your code at the edge before forwarding a request to its declared backend. This integration evaluates inbound requests to AI endpoints served through the Compute service without changing application code. It does not inspect model responses, individual tool calls, direct provider traffic that bypasses Fastly, or employee access to third-party AI.

Integration capabilities

Before you start

Start with the policy in Observe mode. Findings appear in Activity without affecting traffic. Switch to Enforce after reviewing the results. See Policies.

1. Create a collector API key

On the Fastly collector, open Auth and create a key. It is shown once. The key identifies the collector and its assigned policy; the request body does not need a collector ID.

2. Scaffold the Compute service

Put the handler in src/index.js. Configure the remaining settings on the service backends.

3. Declare the backends and store the key

A Compute service can fetch only declared backends. Declare two: The handler passes these names as backend. If a name is wrong or undeclared, the corresponding fetch does not leave the edge. Put the tgk_… key in a Fastly Secret Store and read it from there in the handler. The snippet uses the literal <collector-api-key> for clarity. Replace it with a Secret Store lookup.

4. Evaluate the request body

The handler clones the request, sends the body to POST /v1/evaluate over the trustguard backend with the collector key as a bearer token, and forwards to origin only if the verdict is not block:
The following details determine what the handler evaluates:
  • req.clone(): read the clone, not the request. Consuming the original body stream leaves nothing to forward, and the origin receives an empty body.
  • req.method === "POST": other methods pass through without evaluation. Update the condition if an endpoint accepts prompts through another method.
  • payload.input is the complete body: the raw POST body as a string, JSON scaffolding, model name and message roles included. Detectors see one blob with no role boundaries: retrieved text, your system prompt and the user’s turn are indistinct. payload also accepts a complete OpenAI, Anthropic, or Gemini provider body, so parse and send that object if you need the distinction.
block returns 403 with Blocked by TrustGuard, and the origin is not called. The finding is available in Activity. allow, report, and ask all pass through to origin. Use a block gate for enforcement.
This sample does not implement redaction. A transform verdict contains TrustGuard’s { "input": … } payload rather than your origin’s request schema. Applying it requires custom, schema-aware code that inserts the rewritten text into the original body. Use Block rules unless you have implemented and tested that conversion.
The sample omits error handling around the request to TrustGuard. Catch failures and choose whether to return fetch(req, { backend: "origin" }) (fail open) or a 403 (fail closed). Without a catch, the request fails at the edge.

5. Publish the service

The integration covers traffic handled by this service. AI endpoints served by another service or reached without Fastly are not evaluated. The Next-Gen WAF runs first. Requests it rejects do not reach the handler or trigger a TrustGuard evaluation.

6. Verify

  1. Put the policy in Enforce and POST a prompt to a guarded endpoint that trips a rule.
  2. Confirm that the caller receives 403 with the body Blocked by TrustGuard.
  3. Confirm the event in TrustGuard Activity, under the consumer_id you sent as x-user-id.
  4. Send a clean POST and confirm that the origin receives the original body. This verifies that the handler reads the clone rather than consuming the request stream.

Reference

Coverage

The handler evaluates POST bodies before forwarding them to the origin. It does not inspect model responses, expose individual tool calls, or support redaction. Each evaluated request adds one round trip to {TRUSTGUARD_URL}.

What is evaluated

The call is POST /v1/evaluate with the collector tgk_… key as a bearer token, and the policy’s detectors decide the verdict. direction selects which detector phase runs; this handler only ever sends input.

Verdict handling

Configuration

Configure the integration in the Fastly service:

Attributes

  • consumer_id: read from the x-user-id request header, "" when absent. It is what per-consumer policy routing keys on, so without it every caller shares the collector’s default policy.
  • session_id: read from x-session-id. The sample sends "" when the header is absent. Send a stable, verified conversation ID if you need reliable grouping in Activity.
  • Both are client-supplied headers. The edge does not derive either one, and a caller can set either value. Replace them with values from an authenticated identity if policy decisions have to be trustworthy.
  • The sample sends no attributes block, so gates matching on model, source.application or collector.type have nothing to match. Add the dimensions you gate on to the evaluate body.

Troubleshooting