Skip to content

Install Qlyne on a Next.js app

For a Next.js app on Vercel, or on any host that runs Next.js, without Cloudflare. Qlyne runs as the app’s middleware and decides before each page and API route. On Next.js it runs the bot score, the challenge, the waiting room and rate limits, and passes signals to your app.

In the dashboard, open your site, Connector › Configuration, “Install on Next.js”, and click Download qlyne-next.mjs. The file already carries your site ID, the Qlyne address and the key that checks the rules. Put it at the root of the project, next to package.json.

Create proxy.js at the root too (Next.js 16; on Next.js 13 to 15 the file is middleware.js, with the same content):

import { qlyne } from './qlyne-next.mjs';
export default qlyne();
export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };

The matcher keeps Next.js’s own files out, so they don’t count as visits. If the project already has a middleware, call Qlyne first and keep yours for what Qlyne lets through:

import { qlyne } from './qlyne-next.mjs';
const protect = qlyne();
export default async function proxy(request, event) {
const response = await protect(request, event);
if (!response.headers.get('x-middleware-next')) return response; // refused or sent to the waiting room
return yourMiddleware(request, event);
}

Add the environment variable QLYNE_SECRET with the connector secret (in Connector › Configuration, “Generate secret”). On Vercel: Settings, Environment Variables, for Production and Preview. Then deploy.

Leave the mode on “Observe only” at first and check the last 24 hours in the site Overview. When the numbers make sense, switch to “Enforce” in Connector › Configuration. Changes reach the middleware within a minute, with no new deploy.

A middleware decides before the app answers, but never sees the answer. So:

RuleOn Next.js
Bot scoreYes, with the script tag in your layout (see below). The actions by score band work too
ChallengeYes, on the routes you pick and on the score actions
Waiting roomYes, by rate, without the cap on people at once (see Waiting Room)
Rate limitsCounted by the Qlyne server, adding up every instance of your app (one call to Qlyne on limited routes, about tens of milliseconds); if it doesn’t answer in time, each instance counts on its own until it’s back. qlyne({ rateLimit: 'memory' }) keeps the count in each instance only
Security headersYes, on the response. On next start, Next.js also copies them into the request your app reads
Signals to your appYes, as request headers, like Qlyne-Country and Qlyne-Bot-Score
CountryFrom Vercel (x-vercel-ip-country). There is no network (ASN) and no TLS fingerprint (JA4)

Read the signals in a route handler or server component with headers() from next/headers.

Anything that needs the page itself doesn’t run on Next.js. That covers the hidden fields of protected forms (use the form captcha), the robots.txt lines for AI bots, the cookie banner, page cache, and the sensor script the proxy adds to each page.

IP lists, your rules, managed rules, login protection and access gates run on the same code as the Cloudflare connector, but we haven’t checked them on Next.js yet. For those, use the Qlyne proxy or the Cloudflare connector.

The middleware can’t put the sensor in your pages, so the score comes from the Qlyne script tag. In the dashboard, open your site, Connection, pick Script tag and click Copy tag. The line already carries your site key (data-site, starts with pk_) and the Qlyne address (data-core). Put it in the root layout:

app/layout.js
import Script from 'next/script';
export default function RootLayout({ children }) {
return (
<html>
<body>
{children}
{/* the line copied from the dashboard */}
<Script src="https://app.qlyne.com/q.js" data-site="pk_..." data-core="..." strategy="afterInteractive" />
</body>
</html>
);
}

The tag sends a summary of the browser to Qlyne and stores the score in the qlyne_bs cookie, on your domain. From the next request on, the middleware reads that cookie. Your app gets the score in Qlyne-Bot-Score and the score actions apply. The first page a visitor opens has no score yet. The cookie only counts for the IP and browser that got it.

Challenge browsers that never ran the script

Section titled “Challenge browsers that never ran the script”

With Challenge browsers that never ran the script on, every visitor without a clearance sees the challenge on the first HTML page. That happens with or without the tag, because the middleware can’t tell in advance who will run it. A real browser solves the invisible challenge on its own and goes on until the clearance expires. curl and headless tools stop there. Turn it on only if that first challenge is fine for your site.

On Vercel the middleware reads the IP from x-real-ip and x-forwarded-for, which Vercel sets and the visitor can’t forge. On your own server, make sure the reverse proxy in front of Next.js (nginx, Caddy) overwrites those headers with the real address; otherwise a visitor could send a fake one and dodge the per-IP limits.

A CF-Connecting-IP sent by the visitor never reaches your app. The middleware drops it, like the Qlyne-* headers.

The dashboard shows the host the visitor asked for, from x-forwarded-host (set by Vercel) or Host. On your own server, have the reverse proxy pass the original Host.

The version is on the first line of the file. To update, download it again, replace it and deploy. Your rules stay in the dashboard; the file only carries the code.