A Lovable app asks Bot Protection on its server before it creates an account or starts a trial, and loads the tag in its pages. Lovable writes both from a prompt. This page gives the prompt for each kind of Lovable project, then what Lovable writes from it, so you can read the diff.
Which project you have
Lovable creates new projects on TanStack Start: their pages are rendered on Cloudflare Workers, and their server code is TanStack Start server functions. A project with src/routes/__root.tsx is one of them: follow TanStack Start. A project with an index.html and no server of its own runs its server code as Supabase Edge Functions: follow Supabase Edge Functions.
TanStack Start
The server function runs in the same Worker as the pages, on your app's own domain, so the tag's cookie goes with each call: the token needs no header.
The prompt
Paste it into your project's chat. Signed in, this page fills in your own site key. Lovable then asks for KORZEN_API_KEY in a secret form: paste your secret API key there, never in the chat.
Add Korzen Bot Protection to this app, in monitor mode, failing open.
1. In the root route (src/routes/__root.tsx), inside the shell's <head>, add exactly:
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>
2. Add the npm package @korzen/bot-protection@3. Create a TanStack Start server function
checkSignup (createServerFn, method POST) whose handler, on the server alone:
- reads the secret KORZEN_API_KEY from process.env and makes the client once with
createClient({ apiKey }).
- gets the request with getRequest() from @tanstack/react-start/server and builds the
input with fromRequest(request, { ip: ipFrom.cloudflare, path: "/signup", protectedAction: true }).
- calls client.decide(input). decide returns null on any error or timeout: then go on.
- returns { allowed: false } when the action is "block", and otherwise
{ allowed: true, requestId: the decision's request_id, or null }.
3. In the sign-up form, before supabase.auth.signUp: run `await window.korzen?.ready()` (the
tag's token then rides with the call in its _kz cookie), then call checkSignup. When it
is not allowed, show "We could not create your account." and do not sign up. Otherwise
sign up as before, with korzen_request_id (the requestId) in options.data. If the call
to checkSignup itself fails, sign up as before. Never call /v1/decide from the browser.
4. If the app has a free trial, start it in a server function instead of from the
browser: with the signed-in user (requireSupabaseAuth), it runs the same check with
path "/trial" first and starts nothing on "block". Then users can no
longer start a trial from the browser by themselves.
Do not add a CAPTCHA, change the sign-in, or log the API key. Ask me for KORZEN_API_KEY as
a secret. Show me the diff.
Lovable installs no npm release younger than 24 hours: just after a release, wait a day or pin the version before.
The tag
In src/routes/__root.tsx, in the shell's <head>, so every page is rendered with it. Your Lovable address and your own domain are both learnt as your site's domains on the first visits.
<head> <HeadContent /> <script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script> </head>
The server function
TanStack Start runs it on the server alone; the page gets a stub that calls it. It reads the API key from the project's secrets, asks Bot Protection, and answers whether the sign-up may go on. Any error, a timeout past 200 ms or a wrong key gives no decision, and the sign-up goes on.
// src/lib/korzen.server.ts import { createClient, fromRequest, ipFrom, type Client } from "@korzen/bot-protection"; import { getRequest } from "@tanstack/react-start/server"; let cached: { apiKey: string; client: Client } | undefined; export async function checkBot(path: "/signup" | "/trial") { const apiKey = process.env.KORZEN_API_KEY ?? ""; // A changed secret reaches the next request without a new build: a new client for it. if (cached?.apiKey !== apiKey) cached = { apiKey, client: createClient({ apiKey }) }; // CF-Connecting-IP is the visitor's; the token comes in the _kz cookie. // protectedAction: a script that never loaded your page holds no token. const d = await cached.client.decide(fromRequest(getRequest(), { ip: ipFrom.cloudflare, path, protectedAction: true })); if (d?.action === "block") return { allowed: false as const }; return { allowed: true as const, requestId: d?.request_id ?? null }; } // src/lib/signup.functions.ts import { createServerFn } from "@tanstack/react-start"; import { checkBot } from "./korzen.server"; export const checkSignup = createServerFn({ method: "POST" }).handler(() => checkBot("/signup"));
The form
The form asks the server function first, then signs up with Supabase as it did before, keeping the decision's request_id with the account. When the call itself fails, it signs up as before.
const checkSignupFn = useServerFn(checkSignup); // in the sign-up's submit: let data: { korzen_request_id: string | null } | undefined; try { await window.korzen?.ready(); const verdict = await checkSignupFn(); if (!verdict.allowed) return toast.error("We could not create your account."); data = { korzen_request_id: verdict.requestId }; } catch { // no answer from the check: sign up as before } const { error } = await supabase.auth.signUp({ email, password, options: { data } });
The trial
The trial starts in a server function with the signed-in user, after the same check with the path /trial. Lovable also takes away the browser's own right to start one, so the check cannot be skipped.
// src/lib/trial.functions.ts export const startTrial = createServerFn({ method: "POST" }) .middleware([requireSupabaseAuth]) .handler(async ({ context }) => { const verdict = await checkBot("/trial"); if (!verdict.allowed) return { started: false }; const { supabaseAdmin } = await import("@/integrations/supabase/client.server"); await supabaseAdmin.from("trials").insert({ user_id: context.userId }); // keep verdict.requestId with the trial return { started: true }; }); // the migration Lovable adds: only the server function starts a trial // REVOKE INSERT ON public.trials FROM authenticated;
The visitor's IP
Lovable serves TanStack Start apps from Cloudflare Workers, where CF-Connecting-IP is the visitor's address: Cloudflare writes it, and refuses a request that sends its own. ipFrom.cloudflare reads it. Never read X-Forwarded-For there: Lovable passes on whatever the visitor sends in it.
Supabase Edge Functions
Here the app is a page in the browser and a Supabase project behind it, with no server of its own. The check runs in a Supabase Edge Function that creates the account, on Supabase's domain, so the token goes in a header.
The prompt
Paste it into your project's chat. Signed in, this page fills in your own site key. Then add your secret API key to the project's Edge Function secrets as KORZEN_API_KEY when Lovable asks for it, never in the chat.
Add Korzen Bot Protection to this app, in monitor mode, failing open.
1. In index.html, inside <head>, add exactly:
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>
2. Create a Supabase Edge Function "signup" (verify_jwt = false) that the sign-up form
calls instead of supabase.auth.signUp. It imports
npm:@korzen/bot-protection@3 and npm:@supabase/supabase-js@2, reads the secret
KORZEN_API_KEY, and for each POST {email, password}:
- builds the input with
fromRequest(req, { ip: ipFrom.vercel, path: "/signup", protectedAction: true }) and
calls client.decide(input). decide returns null on any error or timeout: then go on.
- if the action is "block", answers 403 with
{"error": "We could not create your account."} and creates nothing.
- otherwise calls supabase.auth.signUp with the anon key, passing
korzen_request_id (the decision's request_id) in options.data, and answers
what Supabase answered.
- answers OPTIONS for CORS and allows the headers authorization, x-client-info,
apikey, content-type and x-korzen-token.
3. In the sign-up form, before calling the function, get the token with
`await window.korzen?.ready()` and send it as the x-korzen-token header of
supabase.functions.invoke("signup", ...). Show the function's error as the form's
error. Never call /v1/decide from the browser.
4. If the app has a free trial, call the same check with path "/trial" in the Edge
Function that starts it, before the trial is written.
Do not add a CAPTCHA, change the sign-in, or log the API key. Show me the diff.
The tag
In index.html, in the <head>. The tag watches the page and hands its token to the function with each sign-up. Your Lovable address and your own domain are both learnt as your site's domains on the first visits.
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>
The Edge Function
Supabase runs Edge Functions on Deno, which runs the Node SDK as it is, imported from npm. The function asks Bot Protection, then creates the account with your anon key, as the form did before. Any error, a timeout past 200 ms or a wrong key gives no decision, and the sign-up goes on.
// supabase/functions/signup/index.ts import { createClient as korzen, fromRequest, ipFrom } from "npm:@korzen/bot-protection@3"; import { createClient } from "npm:@supabase/supabase-js@2"; const bp = korzen({ apiKey: Deno.env.get("KORZEN_API_KEY") ?? "" }); const cors = { "access-control-allow-origin": "*", "access-control-allow-headers": "authorization, x-client-info, apikey, content-type, x-korzen-token", }; Deno.serve(async (req) => { if (req.method === "OPTIONS") return new Response("ok", { headers: cors }); const { email, password } = await req.json(); // The token comes in x-korzen-token; the first X-Forwarded-For entry is the visitor's. // protectedAction: a script that never loaded your page holds no token. const d = await bp.decide(fromRequest(req, { ip: ipFrom.vercel, path: "/signup", protectedAction: true })); if (d?.action === "block") { return Response.json({ error: "We could not create your account." }, { status: 403, headers: cors }); } const supabase = createClient(Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_ANON_KEY")!); const { data, error } = await supabase.auth.signUp({ email, password, options: { data: { korzen_request_id: d?.request_id ?? null } }, }); return Response.json(error ? { error: error.message } : data, { status: error ? 400 : 200, headers: cors }); });
Deploy it with verify_jwt = false for this function in supabase/config.toml: a visitor who signs up has no session yet. Lovable does both when it writes the function.
The form
The form calls the function in place of supabase.auth.signUp, with the tag's token in a header: the page and the function are on different domains, so the cookie does not travel.
const token = (await window.korzen?.ready()) ?? ""; const { data, error } = await supabase.functions.invoke("signup", { body: { email, password }, headers: { "x-korzen-token": token }, });
The trial
Where the app starts a free trial in an Edge Function, the same call goes first, with the path /trial. The visitor is signed in there, so keep Supabase's JWT check on, and send the token from the page the same way.
const d = await bp.decide(fromRequest(req, { ip: ipFrom.vercel, path: "/trial", protectedAction: true })); if (d?.action === "block") { return Response.json({ error: "We could not start your trial." }, { status: 403, headers: cors }); } // start the trial as before, keeping d?.request_id with it
The visitor's IP
In an Edge Function the visitor's address is the first X-Forwarded-For entry, as Supabase's own examples read it; ipFrom.vercel reads that one. Check it once: send a sign-up with an X-Forwarded-For of your own and look at the decision's IP on the dashboard. If it shows the address you wrote, tell us before turning on block mode.
Close the way around it
The anon key is in the page, so a bot can still call Supabase's sign-up directly, in either kind of project. The Supabase Auth guide's Before User Created hook refuses sign-ups that did not come through your check: your function makes the pass that guide's server action makes. A server function takes the email and answers the pass with its verdict, and the form puts it in options.data; an Edge Function makes it with the Web Crypto API in place of node:crypto. Add it once monitor mode shows the decisions you expect.
Test it
- Open your app's preview and its 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. - Check fail open: set a wrong
KORZEN_API_KEYand sign up again. The call answers401, and the sign-up goes through.
The account starts in monitor mode, where action is always allow: nothing changes for visitors until you turn on block mode.
Common mistakes
- The API key in the page or in the chat. It belongs in the project's secrets alone; the page has only the site key.
- A sign-up or a trial that still skips the check: the form calling
supabase.auth.signUpbefore the check, or the trial still written from the browser. Search the code for both after Lovable's change. - In TanStack Start,
X-Forwarded-Forread as the visitor's IP. Any visitor can write it there; readCF-Connecting-IP. - In an Edge Function, no
x-korzen-tokenin the CORS headers. The browser then refuses the call before it leaves. - An error from the call that stops the sign-up. No decision means: let the visitor through.