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

# Self-hosted Next.js

> Add the Waniwani relay to a Next.js app that runs on your own server, for example with next start on AWS EC2.

Use this setup when your Next.js app runs on your own server, and no CDN that Waniwani supports serves the HTML. An example is `next start` on AWS EC2. The relay is a `proxy.ts` file. Next.js runs it as its [proxy](https://nextjs.org/docs/app/api-reference/file-conventions/proxy). You need Next.js 16 or later.

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 the package:

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

2. Set the key from the setup screen as `WANIWANI_AGENT_TRAFFIC_KEY` on the server that runs the app in production. For example, put it in `.env.production` or in the configuration of the service. Then restart the app.

## Create proxy.ts

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

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

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

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.

## 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";
import { after } from "next/server";

export default async function proxy(request: Request) {
  if (isAgent(request)) {
    recordAgentVisit(request, { waitUntil: after });
  }
  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

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

## Notes

* [`after`](https://nextjs.org/docs/app/api-reference/functions/after) keeps the request open until the event goes to Waniwani. The agent does not wait for the event. This is important when the server can stop or freeze after the response.
* The relay does not know this platform. Thus, the website keeps the platform that you selected in the setup.
* 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"`.
* To open this setup directly, use `https://app.waniwani.ai/agent-traffic/websites/new?platform=self-hosted`.


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