# Connect with Next.js middleware - Agent Delivery Network | Agent Delivery Network

<!-- adn:geo-lead -->
> Use **Next.js middleware** when the site is a **Next.js** app on Vercel, Node, or another host that supports middleware. You deploy a small ADN snippet; people keep the normal orig

<!-- /adn:geo-lead -->

[Back to Help](/help)

Use **Next.js middleware** when the site is a **Next.js** app on Vercel, Node, or another host that supports middleware. You deploy a small ADN snippet; people keep the normal origin HTML while agents get structured ADN responses.

## When to choose this

• The app is Next.js with middleware support

• You do not want to change public www DNS today

• Vercel or Node hosts the production deploy

Other frameworks without middleware should use **DNS connect** or waitlist paths surfaced in preflight warnings.

## Wizard steps

1. Complete ownership TXT and **Check now**.

2. On **Choose how to connect**, select **Next.js middleware** and press **Next**.

3. Accept legal agreements on **Connect** if shown.

4. Copy the snippet, redeploy, then **Continue to go live**.

## Step-by-step on Connect

### 1. Site token and environment variables

After legal acceptance, press **Generate site token** on **Connect** (shown once). Copy **Copy install steps** and set **ADN_GATEWAY_URL** and **ADN_SITE_TOKEN** on Vercel (or `.env.local`), plus **NEXT_PUBLIC_SITE_URL** for robots. Redeploy the production project. You can rotate the token later under **Domain settings → Connect**.

### 2. Middleware snippet

1. Copy the generated middleware code from the **Connect** panel.

2. Paste it into your Next.js app per the inline comments (typically `middleware.ts` at the project root).

3. Redeploy the app to production.

### 3. Verify routing

1. Open **Domain settings → Connect** after deploy.

2. Run connector simulate if your account has QA simulate enabled.

3. Open **Analytics** for machine rows on the paths you protect.

### 4. Vercel specifics

• Set environment variables the snippet expects (site token and ADN gateway host if customised).

• Ensure middleware runs on the routes you want agents to see.

• If Vercel bot protection blocks AI user agents before middleware, see [Allow AI crawlers through to ADN](/help/kb/ai-crawler-edge-blocks).

## Package

Published integration: `@agent-delivery-network/next` (install and config details appear in the dashboard snippet and package readme).

## Related

• [Connection methods compared](/help/connection-methods)

• [Dashboard status badges and smoke tests](/help/kb/dashboard-status-badges-and-smoke-tests)