Better Auth runs in your own server, so the request that signs a visitor up passes through your code. A databaseHooks.user.create.before hook runs before Better Auth creates any user, from the email and password form or from a social provider's callback, and gets the request's headers: it asks Bot Protection there, and refuses the user on block. This guide is for Better Auth 1.x.
The tag
As in the guide of your framework (Next.js, Express), in the layout of your pages, so the sign-up page has it:
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>
The call
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
Then the hook, and a field on the user that keeps the decision's request_id:
// lib/auth.js import { betterAuth } from "better-auth"; import { APIError } from "better-auth/api"; import { createClient, fromHeaders, ipFrom } from "@korzen/bot-protection"; // The timeout for your server's region: https://korzen.dev/bot-protection/docs/server-call#latency const korzen = createClient({ apiKey: process.env.KORZEN_API_KEY, url: "https://eu.korzen.dev", timeoutMs: 200 }); export const auth = betterAuth({ // your database, emailAndPassword, socialProviders ... user: { additionalFields: { korzenRequestId: { type: "string", required: false, input: false } }, }, databaseHooks: { user: { create: { before: async (user, ctx) => { if (!ctx?.headers) return; // a user your own code creates, outside a request const d = await korzen.decide( fromHeaders(ctx.headers, { ip: ipFrom.vercel, // your platform's: see "The visitor's IP" path: "/signup", method: ctx.request?.method ?? "POST", // the form's post needs the tag's token; a provider's callback (GET) spends none protectedAction: true, }), ); if (d?.action === "block") { throw new APIError("BAD_REQUEST", { message: "We could not create your account." }); } return { data: { ...user, korzenRequestId: d?.request_id ?? null } }; }, }, }, }, });
Then npx auth@latest generate (or migrate, with Better Auth's own Kysely adapter) once, so the database has the new column.
The visitor's IP
The hook runs in your server, so the IP is what your platform's guide says: ipFrom.vercel on Vercel, ipFrom.cloudflare if your server takes traffic from Cloudflare alone, ipFrom.lastForwarded behind your own load balancer or nginx. A header the visitor can write gives a bot its own choice of IP.
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 your server and open the sign-up page on localhost. 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.envand 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
/signup, and the new user has itskorzenRequestId. - Check fail open: set a wrong key and sign up again. The call answers
401, and the sign-up goes through.
Common mistakes
- A
hooks.beforeon/sign-up/emailalone. Sign-ups with Google or GitHub never reach it; the database hook sees every new user. - Asking in
session.createas well. Each call counts as a decision: one per new account is enough here; protect sign-in and checkout in their own handlers. - 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.