> ## Documentation Index
> Fetch the complete documentation index at: https://docs.neuraltrust.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare Workers

> TrustGuard Worker on AI routes.

1. `npm create cloudflare@latest` — put the handler in `src/index.js`.
2. `wrangler secret put TRUSTGUARD_API_KEY`
3. Route AI paths in `wrangler.toml`; zone WAF still runs first.
4. `wrangler deploy`

Optional: push repeat offenders to a Cloudflare IP List and block in WAF before the Worker.

```js theme={null}
// wrangler.toml
name = "trustguard-waf"
main = "src/index.js"
routes = [
  { pattern = "app.example.com/api/chat*", zone_name = "example.com" }
]

// src/index.js
export default {
  async fetch(request, env) {
    if (request.method === "POST") {
      const text = await request.clone().text();
      const result = await fetch("{TRUSTGUARD_URL}/v1/evaluate", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${env.TRUSTGUARD_API_KEY}`,
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          protocol: "llm",
          direction: "input",
          payload: { input: text },
          consumer_id: request.headers.get("x-user-id") ?? "",
          session_id: request.headers.get("x-session-id") ?? "",
        }),
      }).then((r) => r.json());
      if (result.status === "block") {
        return new Response("Blocked by TrustGuard", { status: 403 });
      }
      if (result.status === "transform" && result.transformed_payload) {
        return fetch(new Request(request, {
          body: typeof result.transformed_payload === "string"
            ? result.transformed_payload
            : JSON.stringify(result.transformed_payload),
        }));
      }
    }
    return fetch(request);
  },
};
```
