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

# Vercel

> Add the Waniwani relay to a website on Vercel, with Next.js or a different framework.

On Vercel, the relay is a `proxy.ts` file. Next.js runs it as its [proxy](https://nextjs.org/docs/app/api-reference/file-conventions/proxy). With a different framework, Vercel runs it as [Routing Middleware](https://vercel.com/docs/routing-middleware).

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

1. Install `@waniwani/relay` 0.6.0 or later:

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

2. In Vercel, open **Settings › Environment Variables**. Add the key from the setup screen as `WANIWANI_AGENT_TRAFFIC_KEY` for **Production**.

## Next.js

Create `proxy.ts` next to your `app` or `pages` folder:

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

export default function proxy(request: Request) {
  if (isAgent(request)) {
    recordAgentVisit(request);
  }
}

export const config = {
  matcher: ["/((?!api/|_next/|.*\\.[a-z0-9]{1,5}$).*)", "/robots.txt", "/llms.txt"],
};
```

If the project already has a `proxy.ts` or a `middleware.ts`, add the `if` block at the top of its function.

## Other frameworks

Create `proxy.ts` at the root of your project. Use the same function, without `config`. Then add the `proxy` key to `vercel.json` (or `vercel.ts`):

```json vercel.json theme={null}
{
  "proxy": {
    "entrypoint": "proxy.ts",
    "matcher": ["/((?!api/|.*\\.[a-z0-9]{1,5}$).*)", "/robots.txt", "/llms.txt"]
  }
}
```

## 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 `proxy.ts`. It also calls `serveAgentPage`:

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

export default async function proxy(request: Request) {
  if (isAgent(request)) {
    recordAgentVisit(request);
  }
  const agentPage = await serveAgentPage(request);
  if (agentPage) return agentPage;
}

export const config = {
  matcher: ["/((?!api/|_next/|.*\\.[a-z0-9]{1,5}$).*)", "/robots.txt", "/llms.txt"],
};
```

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

## Deploy and check

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

## Notes

* `recordAgentVisit` does not stop the response. The event goes to Waniwani after the response, with the `waitUntil` of Vercel.
* With **Only some paths**, the matcher has your paths. `/blog/*` becomes `/blog/:path*`. 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.