Korzen
Bot Protection
In these docs

Clerk

Bot Protection with Clerk: one decision on a new account's first page, a ban on block, and a mark in the session token your app checks.

Clerk's sign-up goes from the browser to Clerk, and Clerk has no hook that runs before it creates a user. So Bot Protection decides right after: the first page a new account reaches asks about it once, bans it on block, and marks it checked; until then, your app lets it do nothing. This is Clerk's own onboarding pattern, with Next.js.

The tag

As in the Next.js guide, in the root layout, inside <ClerkProvider>. The decide function is that guide's too.

The call

In the Clerk dashboard, under Sessions, add the user's public metadata to the session token, so your server reads the mark without an API call:

{
  "metadata": "{{user.public_metadata}}"
}

The server action that asks once, and marks the account:

// app/signup/check/actions.js
"use server";
import { auth, clerkClient } from "@clerk/nextjs/server";
import { decide } from "@/lib/korzen"; // from the Next.js guide

// check asks Bot Protection once about a new account, and marks it checked.
export async function check() {
  const { userId } = await auth();
  if (!userId) return "signed-out";
  const client = await clerkClient();
  const user = await client.users.getUser(userId);
  if (user.publicMetadata.korzen) return "done";
  let d = null;
  if (Date.now() - user.createdAt < 24 * 3600_000) {
    // accounts made before you added Bot Protection are marked without a decision
    d = await decide("/signup"); // the Next.js guide's: it sends protected_action
    if (d?.action === "block") {
      await client.users.banUser(userId); // ends its sessions; it cannot sign in again
      return "blocked";
    }
  }
  await client.users.updateUserMetadata(userId, {
    publicMetadata: { korzen: { request_id: d?.request_id ?? null } },
  });
  return "done";
}

The page new accounts land on. Send them there with NEXT_PUBLIC_CLERK_SIGN_UP_FORCE_REDIRECT_URL=/signup/check:

// app/signup/check/page.js
"use client";
import { useUser } from "@clerk/nextjs";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { check } from "./actions";

export default function Check() {
  const { isLoaded, user } = useUser();
  const router = useRouter();
  useEffect(() => {
    if (!isLoaded) return;
    (async () => {
      await window.korzen?.ready();
      if ((await check()) !== "done") return router.push("/sign-in");
      await user?.reload(); // a new session token, with the mark in it
      router.push("/dashboard");
    })();
  }, [isLoaded]);
  return <p>One moment…</p>;
}

Middleware that sends an unmarked account back to it. Clerk calls this a convenience: in a route handler or server action that spends money or sends messages, check sessionClaims?.metadata?.korzen again.

// middleware.js (proxy.js on Next.js 16)
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";

const isOpen = createRouteMatcher(["/", "/sign-in(.*)", "/sign-up(.*)", "/signup/check"]);

export default clerkMiddleware(async (auth, req) => {
  const { userId, sessionClaims } = await auth();
  if (userId && !sessionClaims?.metadata?.korzen && !isOpen(req)) {
    return NextResponse.redirect(new URL("/signup/check", req.url));
  }
});

// keep the config.matcher your Clerk set-up already has

The visitor's IP

The decision runs in your server action, so the IP is what the Next.js guide says. Clerk's own bot protection on sign-up can stay on: it guards Clerk's form, and Bot Protection the account it made.

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 with your Clerk development keys 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.

  1. Set KORZEN_API_KEY in .env.local and open the sign-up page. In the browser's developer tools, the _kz cookie appears after a few seconds.
  2. Sign up. The decision shows on the dashboard's Decisions page, under the path you sent.
  3. Check fail open: set a wrong key and sign up again. The call answers 401, and the sign-up goes through.

Common mistakes

  • The mark checked in middleware only. A bot calls your API routes directly: check the claim where it matters.
  • No user.reload() after the mark. The session token keeps the old metadata for up to a minute, and the middleware sends the account back.
  • Asking again on every visit. Each call counts as a decision: the mark makes it once per account.
  • Calling /v1/decide from 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.