# How to add an AI chatbot to a Next.js app

**In Next.js, load the widget with `next/script` in the root layout, so it appears on every page. You don't need an npm package.** Get a key at [/get-api-key](/get-api-key), or have your agent call the [keys API](/install#step-2-request-a-key).

## App Router: `app/layout.tsx`

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

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script src="https://onelinechat.com/widget.js" data-key="olc_pub_YOUR_KEY" strategy="afterInteractive" />
      </body>
    </html>
  );
}
```

## Pages Router: `pages/_app.tsx`

```tsx
import Script from 'next/script';
import type { AppProps } from 'next/app';

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <Script src="https://onelinechat.com/widget.js" data-key="olc_pub_YOUR_KEY" strategy="afterInteractive" />
    </>
  );
}
```

## Notes

- `next/script` passes `data-*` attributes through to the script tag, so `data-color`, `data-greeting` and `data-position` work here too.
- The key is public, so put it straight into the component. You don't need a `NEXT_PUBLIC_` environment variable, although one works if you prefer.
- Vercel preview deployments (`*.vercel.app`) are allowed automatically.

---
Source: https://onelinechat.com/install/nextjs · Last updated: 2026-11-20 · All docs: https://onelinechat.com/llms.txt
