# Add your agent to a Next.js site on Vercel

> Load the messenger in a Next.js or Vercel project using an environment variable.

Source: https://askmavis.co/help/nextjs-and-vercel  
Collection: Install your agent  
Last updated: 2026-12-05

If your site is a Next.js project on Vercel, you can keep your agent's ID in an environment variable instead of in your code.

## 1. Allow your website

Open your agent, go to **Guardrails** and add your site under **Allowed domains**, for example `https://your-project.vercel.app` and your own domain. Then enable the widget on the **Install** tab.

## 2. Add the agent ID

1. Copy the **Agent ID** from **Agent settings**.
2. In your Vercel project, open **Settings > Environment Variables**.
3. Add `NEXT_PUBLIC_ASKMAVIS_AGENT_ID` with the Agent ID as its value.

## 3. Load the messenger

Add this to `app/layout.tsx`, inside `<body>`. For `src`, use the address of `messenger.js` shown in the snippet on your agent's **Install** tab.

```tsx
import Script from 'next/script';

const agentId = process.env.NEXT_PUBLIC_ASKMAVIS_AGENT_ID;

// In app/layout.tsx, inside <body>:
{agentId ? (
  <Script
    src="https://mavis.help/messenger.js"
    data-messenger-config-id={agentId}
    data-consent="granted"
    referrerPolicy="strict-origin-when-cross-origin"
    strategy="afterInteractive"
  />
) : null}
```

Then redeploy your project.

## Related

- [Add the messenger to your website](https://askmavis.co/help/add-the-messenger-to-your-website)
