This guide is for Astro 7, with a server adapter such as @astrojs/node. You add the tag to the layout, and call Bot Protection from the endpoint 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 the layout of your sign-up, sign-in and checkout pages. is:inline leaves the script as it is, for the browser to load:
--- // src/layouts/Layout.astro --- <html lang="en"> <head> <meta charset="utf-8" /> <script is:inline src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script> </head> <body><slot /></body> </html>
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 astro.config.mjs, so that astro:env/server reads it from the server's environment when the server starts, on every adapter. It is optional, so a missing key never stops your site: the client then warns once and lets every visitor through:
// astro.config.mjs import { defineConfig, envField } from "astro/config"; import node from "@astrojs/node"; export default defineConfig({ adapter: node({ mode: "standalone" }), env: { schema: { KORZEN_API_KEY: envField.string({ context: "server", access: "secret", optional: true }), }, }, });
Then one client for the process, in a module that only server code imports:
// src/lib/korzen.js import { KORZEN_API_KEY } from "astro:env/server"; 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, ipFrom } from "@korzen/bot-protection";
The call
In an endpoint rendered on demand: prerender = false makes it one, even when the rest of the site is static. fromRequest reads the request's headers, the _kz cookie and the method, and no body, so the endpoint still reads the form after it:
// src/pages/api/signup.js import { fromRequest, korzen } from "../../lib/korzen.js"; export const prerender = false; export async function POST({ request, clientAddress, redirect }) { // protectedAction: sign-up, login and payment: a script that never loaded your page holds no token const d = await korzen.decide(fromRequest(request, { ip: clientAddress, path: "/signup", protectedAction: true })); // see "The visitor's IP" if (d?.action === "block") return new Response("We could not create your account.", { status: 400 }); const user = await createAccount(await request.formData()); // your own code await saveRequestID(user.id, d?.request_id); // see "Apply the action" return redirect("/welcome", 303); }
In the page, the form posts to it, and waits for the tag before it is sent: window.korzen?.ready() reports what the visitor did since the tag last reported, and resolves within a few seconds whatever happens:
<!-- src/pages/signup.astro, inside your layout --> <form method="post" action="/api/signup" id="signup"> <input name="email" type="email" required /> <button>Sign up</button> </form> <script> const form = document.getElementById("signup"); form.addEventListener("submit", async (event) => { event.preventDefault(); await window.korzen?.ready(); form.submit(); }, { once: true }); </script>
The visitor's IP
clientAddress is the address your adapter reads. On Vercel, Netlify and Cloudflare, it is the one the platform saw, which a visitor cannot write. With @astrojs/node and nothing in front of it, it is the connection's address.
Behind a proxy of yours, clientAddress is the proxy's address, or, once security.allowedDomains names your host, the first entry of X-Forwarded-For, which the visitor writes. There, send the entry your proxy added: fromRequest(request, { ip: ipFrom.lastForwarded, … }).
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:4321/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
- An Astro page left prerendered. Without
prerender = false(oroutput: "server"), the endpoint has no request to judge, andclientAddressthrows. clientAddressbehind a proxy of yours, withsecurity.allowedDomainsset. It is then the first entry ofX-Forwarded-For, which the visitor writes, so a bot picks its own IP: useipFrom.lastForwarded.- The client imported into code that reaches the browser. The API key would reach every visitor; keep it in a server-only module.
- 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.