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

# Cloudflare

> Run the Waniwani relay in a Cloudflare Worker in front of your website, on any host.

On Cloudflare, the relay is a Worker on routes of your zone. The Worker sends each request on to your site. Thus, your site can run on any host.

The code on this page is for the US region with **Every page** selected. Copy your code from the setup screen. It has your region and your paths.

## Before you start

Create a Worker project. Then install `@waniwani/relay` 0.6.0 or later:

```bash theme={null}
npm create cloudflare@latest
npm install @waniwani/relay
```

## Steps

**1. Write src/index.ts**

```ts src/index.ts theme={null}
import { isAgent, recordAgentVisit } from "@waniwani/relay";

export default {
  async fetch(request, env, ctx) {
    if (isAgent(request)) {
      recordAgentVisit(request, {
        key: env.WANIWANI_AGENT_TRAFFIC_KEY,
        waitUntil: ctx.waitUntil.bind(ctx),
      });
    }
    return fetch(request);
  },
} satisfies ExportedHandler<{ WANIWANI_AGENT_TRAFFIC_KEY: string }>;
```

If a Worker already runs on these pages, add the `if` block at the top of its `fetch` handler.

**2. Write wrangler.jsonc**

Put your hostname in place of `example.com`:

```jsonc wrangler.jsonc theme={null}
{
  "name": "waniwani-relay",
  "main": "src/index.ts",
  "compatibility_date": "2026-10-01",
  "compatibility_flags": ["nodejs_compat"],
  "routes": ["example.com/*"]
}
```

**3. Add the key and deploy**

In the Worker folder, run these commands. When the first command asks for the key, paste the key from the setup screen.

```bash theme={null}
npx wrangler secret put WANIWANI_AGENT_TRAFFIC_KEY
npx wrangler deploy
```

**4. Check**

In the setup screen, enter the address of one of your pages and click **Check now**.

## With agent pages or blocking

When the website serves [agent pages](https://docs.waniwani.ai/agent-traffic/agent-pages.md) or [blocks an agent](https://docs.waniwani.ai/agent-traffic/blocking.md), the setup screen gives you this `src/index.ts`. It also calls `serveAgentPage`:

```ts src/index.ts theme={null}
import { isAgent, recordAgentVisit, serveAgentPage } from "@waniwani/relay";

export default {
  async fetch(request, env, ctx) {
    if (isAgent(request)) {
      recordAgentVisit(request, {
        key: env.WANIWANI_AGENT_TRAFFIC_KEY,
        waitUntil: ctx.waitUntil.bind(ctx),
      });
    }
    const agentPage = await serveAgentPage(request, { key: env.WANIWANI_AGENT_TRAFFIC_KEY });
    if (agentPage) return agentPage;
    return fetch(request);
  },
} satisfies ExportedHandler<{ WANIWANI_AGENT_TRAFFIC_KEY: string }>;
```

On a path with an agent page, an AI agent gets that page from the Worker. A client that asks for Markdown also gets it. All other requests go to your site with no change. If a Worker already runs on these pages, copy each line above `export default` to the top of its file. Then add each line of the `fetch` handler, except `return fetch(request)`, at the top of its `fetch` handler.

You make this change one time. After it, you turn agent pages and blocking on and off in the dashboard, with no deploy.

## Notes

* `waitUntil` keeps the event alive after the response. Thus, the agent does not wait for it.
* With **Only some paths**, the routes have your paths, and the Worker checks each path again with `isChosenPage`. A route can only end in `*`. Thus, `/pricing*` also matches `/pricing-old`. Also add the paths of your [agent pages](https://docs.waniwani.ai/agent-traffic/agent-pages.md).
* In the EU region, each call gets `region: "eu"`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.