This guide is for SvelteKit 3. You add the tag to src/app.html, and call Bot Protection from the form action that creates the account. 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.
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 src/app.html, so every page has it:
<!-- src/app.html --> <head> <meta charset="utf-8" /> <script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script> %sveltekit.head% </head>
For visitors whose network blocks Korzen's host, firstParty: true serves the tag and its beacon from your own domain. First-party mode
The client
Behind Cloudflare? A Worker can do this without changing your app. Cloudflare Workers
Install the client:
npm install @korzen/bot-protection
Declare the API key in src/env.js, so that $app/env/private reads it from the server's environment when the app starts. It is optional, so a missing key never stops your app: the client then warns once and lets every visitor through:
// src/env.js import { defineEnvVars } from "@sveltejs/kit/env"; export const variables = defineEnvVars({ KORZEN_API_KEY: { schema: (value) => value, description: "Korzen Bot Protection's API key" }, });
Then one client for the process, in src/lib/server/, which SvelteKit never lets into the browser's bundle:
// src/lib/server/korzen.js import { KORZEN_API_KEY } from "$app/env/private"; import { createClient } from "@korzen/bot-protection"; // The timeout for your server's region: https://korzen.dev/bot-protection/docs/server-call#latency export const korzen = createClient({ apiKey: KORZEN_API_KEY, url: "https://eu.korzen.dev", timeoutMs: 200 }); export { fromRequest } from "@korzen/bot-protection";
On SvelteKit 2, import it as $lib/server/korzen.js, and read the key as env.KORZEN_API_KEY from $env/dynamic/private, with no src/env.js.
The call
In the form action of the sign-up page. fromRequest reads the request's headers, the _kz cookie, the path and the method, and no body, so the action still reads the form after it:
// src/routes/signup/+page.server.js import { fail, redirect } from "@sveltejs/kit"; import { fromRequest, korzen } from "#lib/server/korzen.js"; export const actions = { default: async ({ request, getClientAddress }) => { // protectedAction: sign-up, login and payment: a script that never loaded your page holds no token const d = await korzen.decide(fromRequest(request, { ip: getClientAddress(), protectedAction: true })); // see "The visitor's IP" if (d?.action === "block") return fail(400, { error: "We could not create your account." }); const user = await createAccount(await request.formData()); // your own code await saveRequestID(user.id, d?.request_id); // see "Apply the action" redirect(303, "/welcome"); }, };
In the page, use:enhance waits for the tag before it sends the form: window.korzen?.ready() reports what the visitor did since the tag last reported, and resolves within a few seconds whatever happens:
<!-- src/routes/signup/+page.svelte --> <script> import { enhance } from "$app/forms"; let { form } = $props(); </script> <form method="POST" use:enhance={async () => { await window.korzen?.ready(); }}> <input name="email" type="email" required /> <button>Sign up</button> <p>{form?.error}</p> </form>
The visitor's IP
getClientAddress() is the address your adapter reads. On Vercel, Netlify and Cloudflare, it is the one the platform saw, which a visitor cannot write. With @sveltejs/adapter-node and nothing in front of it, it is the connection's address. Behind a proxy of yours, set ADDRESS_HEADER=X-Forwarded-For and XFF_DEPTH=1 (one proxy), where every request comes through that proxy: the adapter then takes the entry your proxy added, and refuses a request without the header.
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 npm run dev and open http://localhost:5173/signup. 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.
- Set
KORZEN_API_KEYin the server's environment and 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
$libor$env/dynamic/privateon SvelteKit 3.$libis gone there, and the build stops; a variable thatsrc/env.jsdoes not declare reads as nothing, so every call goes out without a key.XFF_DEPTHhigher than the number of your own proxies in front of the server. The adapter then takes an entry the visitor wrote, so a bot picks its own IP.- The call in
handle, insrc/hooks.server.js. It runs on every request, and every call counts as a decision: call it only in the action you protect. - 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.