> ## 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.

# How the relay works

> The @waniwani/relay code that records the AI agent requests to your website.

The relay is your own code that calls [`@waniwani/relay`](https://github.com/WaniWani-AI/relay). It runs in front of your website, as a proxy, a Worker or a Lambda\@Edge function. For ASP.NET on IIS, the relay is a [NuGet package](https://docs.waniwani.ai/agent-traffic/aspnet.md) that runs as an IIS module. The package is open source under Apache 2.0, and it has no runtime dependencies. The [README](https://github.com/WaniWani-AI/relay#readme) is the full API reference.

## Functions

The setup screen uses these functions:

| Function | What it does |
| - | - |
| `isAgent(request)` | Returns `true` for an AI agent request. It reads only the headers and the client IP. It does not wait for the network. |
| `recordAgentVisit(request, options?)` | Sends one event for the request to Waniwani. On Vercel, Cloudflare and Next.js, the event goes after the response, and the agent does not wait. |
| `serveAgentPage(request, options?)` | Returns the [agent page](https://docs.waniwani.ai/agent-traffic/agent-pages.md) for the request, or a `403` for a [blocked agent](https://docs.waniwani.ai/agent-traffic/blocking.md). In all other cases, it returns `null`, and your code sends the request on to your site. |
| `handleViewerRequest(request, options?)` | On CloudFront only. Sends a `403` to a blocked agent and marks the requests that get an agent page. |

The setup screen adds `serveAgentPage` or `handleViewerRequest` when the website serves agent pages or blocks an agent.

No function throws an error, and no promise rejects. An error in the relay does not break your page. Your code always sends the request on to your site.

## The key

All websites of your organization use one key. Copy it from the setup screen and keep it secret. Without a valid key, the relay sends nothing.

| Platform | Where the key goes |
| - | - |
| Vercel | The `WANIWANI_AGENT_TRAFFIC_KEY` environment variable, for **Production** |
| Cloudflare | The `WANIWANI_AGENT_TRAFFIC_KEY` secret of the Worker |
| AWS CloudFront | In the function in the zip from the setup screen. Lambda\@Edge has no environment variables. |
| Self-hosted Next.js | The `WANIWANI_AGENT_TRAFFIC_KEY` environment variable on the server that runs the app |
| ASP.NET on IIS | `Waniwani:AgentTrafficKey` in the `appSettings` of `web.config` |

For an organization in the [EU region](https://docs.waniwani.ai/platform/regions.md), the code from the setup screen has `region: "eu"`.

## What Waniwani receives

For each agent request, Waniwani receives one JSON event. The event has the method, the host, the path, the time and a short list of request headers.

* The client IP does not leave your server. Waniwani gets only an HMAC-SHA256 hash of it, made with your key.
* Cookies and the request body do not leave your server.
* For `Authorization`, only the scheme name leaves, for example `Bearer`.

Use the `omit` option to send less. The README has the [full field list](https://github.com/WaniWani-AI/relay#agentvisitrecord) and the [options](https://github.com/WaniWani-AI/relay#relayoptions).

## Which requests are agent requests

A request is an agent request when one of these is true:

* Its `User-Agent` has a name from the Waniwani AI bot list.
* It has a `Signature-Agent` or a `DPoP` header.
* Its IP is in a range that an AI company publishes.

`Googlebot`, `Bingbot`, browsers and `curl` are not agent requests. The relay gets the newest list every 12 hours. You do not deploy again. Source: [`src/logic`](https://github.com/WaniWani-AI/relay/tree/main/src/logic).

## Relay status

Open the website and go to **Settings**. The **Relay** card shows the status of the relay:

| Status | Meaning |
| - | - |
| **Live on** your hostname | The relay recorded agent requests in the last 7 days. |
| **No agent request in the last 7 days** | The relay recorded agent requests before, but none in the last 7 days. |
| **Waiting for the first agent request** | The relay is installed, but it did not record an agent request yet. |
| **Not installed** | The relay did not send data from this website. Readiness scans work without the relay. |

The **Relay package** row shows the version of the relay:

| Value | Meaning |
| - | - |
| **Latest** | Your relay is the newest version. |
| **Update available** | A newer version is available. Update it. See [Update the relay](#update-the-relay). |
| **Latest version unknown** | Waniwani cannot find the newest version now. |

The **Relay code** row shows which relay functions your code calls:

| Value | Meaning |
| - | - |
| **Ready** | Your code records agent visits. If the code also calls `serveAgentPage`, it serves agent pages and blocks agents. |
| **Needs a change** | Agent pages or blocking are on, but your code does not call `serveAgentPage`. On CloudFront, a function from the newest zip is missing. The card shows what to change. |
| **Not reported** | Before the first agent request, Waniwani does not know the functions. After it, your relay is too old to report its functions. Update the relay and deploy. |

## Update the relay

* On Vercel, Cloudflare and self-hosted Next.js, update the `@waniwani/relay` package and deploy.
* On ASP.NET, update the `Waniwani.Relay.AspNet` NuGet package and deploy.
* On CloudFront, download the zip again from the setup screen and deploy it.

## Troubleshooting

> [accordion] The check cannot get to the page
>
> The page must be a public `https` page of the website. The deploy must be complete. A firewall or a bot protection rule can stop the check. The check sends one `GET` request.

> [accordion] The check gets to the page, but no agent request shows
>
> Make sure that the key is set for production. Make sure that you deployed after you set the key. Make sure that the relay runs on the path that you check. After 10 minutes, the setup screen shows **Still no agent request**.

> [accordion] My site already has middleware
>
> Keep it. Put the lines of the relay at the top of your function.


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