Optional. Behind Cloudflare, this Worker makes the call from your server in place of the SDK, for an origin in any language and with no change to your application: one or the other, never an extra step. What only your application knows, the context (the order, the account), still needs the SDK in your application, alongside the Worker if you want both.
When your site is behind Cloudflare, a Worker on the routes you protect can ask Bot Protection before the request reaches your origin: no change to your application at all. It runs at Cloudflare's edge, close to korzen.dev, which is behind Cloudflare too. On a static site, a Cloudflare Pages Function does the same for one action, as on our own blog.
Payment, login and sign-up routes: list their paths in KORZEN_PROTECTED_ACTIONS, so a script that never loads your page cannot take the action. Protecting a checkout
The tag
The tag still goes on your pages, in the <head>, so the _kz cookie is there when the visitor submits:
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>
The Worker
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
createWorker is the whole Worker: it asks about every request on its routes with your timeout, and takes its settings from the Worker's environment:
// src/index.js: a Worker on the routes Bot Protection judges, in front of the cache and your origin. // wrangler.toml: // routes = [ // { pattern = "tickets.example/api/resale/*", zone_name = "tickets.example" }, // { pattern = "tickets.example/orders/*", zone_name = "tickets.example" }, // ] // [vars] // KORZEN_URL = "https://eu.korzen.dev" // KORZEN_TIMEOUT_MS = "200" # The timeout for your visitors' regions, since a Worker runs near each: https://korzen.dev/bot-protection/docs/server-call#latency // KORZEN_PROTECTED_ACTIONS = "/orders/*" // The key: npx wrangler secret put KORZEN_API_KEY import { createWorker } from "@korzen/bot-protection"; export default createWorker();
KORZEN_API_KEY: your secret key, as a Worker secret. Without it every request goes on unjudged, and the Worker's log says so once.KORZEN_URL:https://eu.korzen.dev.KORZEN_TIMEOUT_MS: how long a decision may take, 10 to 1000 ms (200 unless set). Past it, the request goes on.KORZEN_PROTECTED_ACTIONS: the paths whose actions need the tag's token, separated by commas; one ending in*is a prefix.
KORZEN_FIRST_PARTY serves the tag and its beacon from your own domain too, for visitors whose network blocks Korzen's host. First-party mode
On block the Worker answers 403. Everything else goes on to Cloudflare's cache and your origin, with Korzen-Request-Id and Korzen-Action headers your application can log. Any error or timeout lets the request through: when Bot Protection is slow or down, your site works as it did without the Worker.
Actions that need a token
An action on a path in KORZEN_PROTECTED_ACTIONS (its POST, PUT, PATCH or DELETE: a page load spends no token) goes on only with a token the tag earned in that browser, once per session. A script that never runs JavaScript is refused on every attempt, whatever address it comes from. Put the tag on the page that leads to the action.
Polling served from the cache
The Worker runs before Cloudflare's cache: a request the cache answers never reaches your server, but it reaches the Worker. Put the Worker on the routes that are polled (an inventory, a seat map) and every poll is judged; the ones allowed are still served from the cache. Each request on the Worker's routes is a decision on your plan's volume, so route only what you want judged.
What Cloudflare adds
With each decision the Worker sends what Cloudflare measured of the visitor (request.cf): on every plan the network (ASN), the country, the HTTP version and the TCP round trip between the visitor and Cloudflare (none over HTTP/3); with Enterprise Bot Management also the JA4 fingerprint and the detection ids. Bot Protection reads them with the rest of the request; none of them blocks a visitor alone.
Your own handler
createWorker covers the usual case. For a Worker that does more, wrap your own fetch with withKorzen; it sends what Cloudflare measured just the same.
withKorzen wraps the Worker's fetch: it reads what Cloudflare knows about the visitor and asks with your timeout. On block it answers 403 itself. Everything else goes on to your origin, with the decision's id and action in headers your application can log:
// src/index.js: a Worker on the routes you protect, in front of your origin. // wrangler.toml: routes = [{ pattern = "shop.example/signup*", zone_name = "shop.example" }] // The key: npx wrangler secret put KORZEN_API_KEY import { createClient, ipFrom, withKorzen } from "@korzen/bot-protection"; let protect; // made once per isolate, with the key from env export default { fetch(request, env, ctx) { protect ??= withKorzen( async (request, env, ctx, d) => { // on to your origin, with the decision's id and action const forward = new Request(request); if (d?.request_id) forward.headers.set("Korzen-Request-Id", d.request_id); if (d?.action) forward.headers.set("Korzen-Action", d.action); return fetch(forward); }, { // The timeout for your visitors' regions, since a Worker runs near each: https://korzen.dev/bot-protection/docs/server-call#latency client: createClient({ apiKey: env.KORZEN_API_KEY, url: "https://eu.korzen.dev", timeoutMs: 200 }), ip: ipFrom.cloudflare, // CF-Connecting-IP: Cloudflare sets it }, ); return protect(request, env, ctx); }, };
A Cloudflare Pages Function
On a static site on Cloudflare Pages, the action a page posts to (a form, a button's request) can be a Pages Function: the same client judges that one route and sends what Cloudflare measured (request.cf) just the same. Install the package at the project's root, where Pages bundles a Function's imports when it builds, and put the key in the project's secrets, for Production and for Preview if you test on previews:
npm install @korzen/bot-protection npx wrangler pages secret put KORZEN_API_KEY --project-name your-project npx wrangler pages secret put KORZEN_API_KEY --project-name your-project --env preview
// functions/api/vote.js: POST /api/vote, the action a button on your pages sends. import { createClient, ipFrom, withKorzen } from "@korzen/bot-protection"; const refused = () => Response.json({ ok: false }, { status: 400 }); let protect; // made once per isolate, with the key from env export function onRequestPost({ request, env }) { protect ??= withKorzen( async (request, decision) => { // your action, once Bot Protection let the visitor through return Response.json({ ok: true }); }, { // The timeout for your visitors' regions, since a Worker runs near each: https://korzen.dev/bot-protection/docs/server-call#latency client: createClient({ apiKey: env.KORZEN_API_KEY, url: "https://eu.korzen.dev", timeoutMs: 200 }), ip: ipFrom.cloudflare, // CF-Connecting-IP: Cloudflare sets it onBlock: refused, }, ); return protect(request); }
Before the page sends the request, it calls await window.korzen?.ready(), so the _kz cookie goes with it. On a payment, login or sign-up action, add protectedAction: true to withKorzen's options.
The visitor's IP
CF-Connecting-IP is set by Cloudflare and cannot be written by the visitor, so a Worker always has the right address. Your origin should still accept traffic from Cloudflare only, or a bot can skip the Worker by calling it directly.
The headers and their order
withKorzen sends the headers Bot Protection reads (Accept, Accept-Encoding, the client hints, Sec-Fetch-*) and Accept-Language. It does not send their order (header_order): a Worker's request.headers lists the names sorted, not as they came, so the field is left out and the decision reads the rest. On your own Node server, fromNodeRequest sends the order from rawHeaders.
Test it
npx wrangler dev runs the Worker on your machine; set the key in .dev.vars as KORZEN_API_KEY=…. Then npx wrangler deploy, and watch the decisions on the dashboard's Decisions page.
A Pages Function runs on your machine with npx wrangler pages dev, over the folder your site builds into (Hugo's public, for example), at http://localhost:8788. It reads the key from .dev.vars at the project's root, as a Worker does: one line, KORZEN_API_KEY=…. Pages on localhost always accept the tag and are never listed as a domain. Keep .dev.vars in .gitignore: it holds your API key, which never goes into the repository.
echo ".dev.vars" >> .gitignore npx wrangler pages dev public
Common mistakes
- A Worker on every route: each request on its routes is a decision. Route only what you want judged: the polling you watch, sign-up, sign-in, checkout.
- A path in
KORZEN_PROTECTED_ACTIONSwhose pages have no tag: no visitor holds a token there, so every action on it is refused. - Reading the request's body to decide, and then forwarding a body already used: a Request's body can be read once.
- An origin open to the whole internet: the Worker only guards the requests that go through Cloudflare.