This guide is for the App Router, Next.js 15 and later. You add the tag to the root layout, and call Bot Protection from the server action or route handler that creates the account.
Run Node 22.23.4, 24.22.1 or 26.12.1, or a later release of the same line. Earlier releases carry CVE-2026-111497, a flaw in the Brotli decoder that fetch and node:zlib use: malformed compressed data can corrupt the process's memory. Node 20, 23 and 25 get no fix.
Payment, login and sign-up routes: turn on protectedAction ("protected_action": true in the call), so a script that never loads your page cannot take the action. Protecting a checkout
The tag
In the root layout, so every page has it:
// app/layout.js export default function RootLayout({ children }) { return ( <html lang="en"> <head> <script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script> </head> <body>{children}</body> </html> ); }
For visitors whose network blocks Korzen's host, firstParty: true serves the tag and its beacon from your own domain. First-party mode
The call
Behind Cloudflare? A Worker can do this without changing your app. Cloudflare Workers
The client is the npm package @korzen/bot-protection, with no dependencies: it asks /v1/decide, gives up at your timeout and lets the visitor through on any error. Install it:
npm install @korzen/bot-protection
On a platform that starts your functions cold, such as Vercel, a function's first call also opens a new connection to Bot Protection, slower than the one a running server keeps open, so the timeout is one second. A sign-up waits that second only when Bot Protection is slow. On a server that keeps running, your region's timeout is enough.
Then one function for every protected action:
// lib/korzen.js import { headers } from "next/headers"; import { createClient, fromHeaders, ipFrom } from "@korzen/bot-protection"; // One second: a function started cold opens its connection on its first call, which a // short timeout cuts. On a server that keeps running, your region's timeout: // https://korzen.dev/bot-protection/docs/server-call#latency export const client = createClient({ apiKey: process.env.KORZEN_API_KEY, url: "https://eu.korzen.dev", timeoutMs: 1000 }); // decide asks Bot Protection about the request being handled. It returns the answer, // or null on any error: the caller then goes on as on allow (fail open). export async function decide(path) { // ipFrom.vercel on Vercel; see "The visitor's IP" return client.decide(fromHeaders(await headers(), { path, ip: ipFrom.vercel, protectedAction: true })); }
Then, in the server action that creates the account:
// app/signup/actions.js "use server"; import { decide } from "@/lib/korzen"; export async function signUp(prevState, formData) { const d = await decide("/signup"); if (d?.action === "block") return { error: "We could not create your account." }; const user = await createAccount(formData); // your own code await saveRequestID(user.id, d?.request_id); // see "Apply the action" // ... }
In a route handler, withKorzen wraps it: it answers 403 on block, and hands the handler the answer after its own arguments:
// app/api/signup/route.js import { ipFrom, withKorzen } from "@korzen/bot-protection"; import { client } from "@/lib/korzen"; export const POST = withKorzen( async (request, ctx, d) => { const user = await createAccount(await request.formData()); // your own code await saveRequestID(user.id, d?.request_id); return Response.json({ ok: true }); }, { client, ip: ipFrom.vercel, protectedAction: true }, );
The visitor's IP
On Vercel, x-forwarded-for holds the visitor's IP: Vercel overwrites it, so a visitor cannot write their own. Elsewhere, read the header your own edge sets: cf-connecting-ip behind Cloudflare, x-real-ip if your nginx sets it. Behind a proxy that appends to x-forwarded-for instead of overwriting it, the first entry is whatever the visitor sent: take the last one your proxy added.
Apply the action
Read only action. On block, answer with the same error the action shows when it fails for any other reason, so a bot learns nothing. The account starts in monitor mode, where action is always allow: nothing changes for visitors until you turn on block mode.
Keep the answer's request_id with what the action created, such as the account or the order: it names that decision when you ask us about it. The answer
Test it on your machine
Run next dev and open http://localhost:3000. Pages on localhost always accept the tag and are never listed as a domain. Locally the IP is 127.0.0.1 or ::1, and that is fine. In Docker on your machine, the app sees the bridge's address instead, such as 172.17.0.1, and that is fine too.
- Set
KORZEN_API_KEYin.env.localand open the sign-up page. In the browser's developer tools, the_kzcookie appears after a few seconds. - Sign up. The decision shows on the dashboard's Decisions page, under the path you sent.
- Check fail open: set a wrong key and sign up again. The call answers
401, and the sign-up goes through.
Common mistakes
- Reading
x-forwarded-for's first entry behind a proxy that appends to it. A bot then picks its own IP. - A form sent by JavaScript as soon as the page loads, before the tag reported. Call
await window.korzen?.ready()before you send it; it resolves within a few seconds even when Bot Protection is slow. - The call in middleware. It runs on every request, and every call counts as a decision: call it only in the action you protect.
- Calling
/v1/decidefrom the browser. The API key would reach every visitor; the call belongs on your server. - No timeout, or an error that stops the sign-up. Any answer other than
200, and no answer in time, means: let the visitor through. - A line in your log that starts
korzen: the visitor's IP sent to Bot Protection. The client writes it once, at the first decision whose IP is your own network's (a load balancer's, a proxy's, a container's), which Bot Protection judges as no IP: setipto the visitor's address (the visitor's IP). In Docker on your machine it is the bridge's address, and harmless there.