Korzen
Bot Protection
In these docs

Nuxt

Bot Protection in Nuxt: the tag in nuxt.config, the call in a server route, the API key in runtimeConfig, and the visitor's IP on Node, Vercel and Cloudflare.

This guide is for Nuxt 4. You add the tag in nuxt.config.ts, and call Bot Protection from the server route 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 app.head, so every page has it. The API key goes in runtimeConfig, which stays on the server; Nuxt fills it from NUXT_KORZEN_API_KEY in the server's environment:

// nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    korzenApiKey: "", // NUXT_KORZEN_API_KEY
  },
  app: {
    head: {
      script: [{ src: "https://eu.korzen.dev/v1/tag.js", "data-site-key": "YOUR_SITE_KEY", async: true }],
    },
  },
});

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

Then one client for the process, in server/utils/, which runs on the server only. It is made at the first call, once Nuxt has read the runtime config:

// server/utils/korzen.js
import { createClient } from "@korzen/bot-protection";

let client;

export function korzen() {
  // The timeout for your server's region: https://korzen.dev/bot-protection/docs/server-call#latency
  client ??= createClient({ apiKey: useRuntimeConfig().korzenApiKey, url: "https://eu.korzen.dev", timeoutMs: 200 });
  return client;
}

The call

A server route. fromHeaders(event.headers) reads the request's headers and leaves its body to readFormData; building a second Request with toWebRequest(event) would leave the form unread:

// server/api/signup.post.js
import { fromHeaders } from "@korzen/bot-protection";
import { korzen } from "../utils/korzen.js";

export default defineEventHandler(async (event) => {
  // protectedAction: sign-up, login and payment: a script that never loaded your page holds no token
  const d = await korzen().decide(fromHeaders(event.headers, { ip: getRequestIP(event), path: "/signup", protectedAction: true })); // see "The visitor's IP"
  if (d?.action === "block") {
    setResponseStatus(event, 400);
    return "We could not create your account.";
  }
  const user = await createAccount(await readFormData(event)); // your own code
  await saveRequestID(user.id, d?.request_id); // see "Apply the action"
  return sendRedirect(event, "/welcome", 303);
});

In the page, the form 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:

<!-- app/pages/signup.vue -->
<script setup>
async function submit(event) {
  await window.korzen?.ready();
  event.target.submit();
}
</script>

<template>
  <form method="post" action="/api/signup" @submit.prevent="submit">
    <input name="email" type="email" required />
    <button>Sign up</button>
  </form>
</template>

The visitor's IP

getRequestIP(event), with no options, is the connection's address: right with nothing in front of your server. Behind a proxy of yours, send the entry it added to X-Forwarded-For, ipFrom.lastForwarded, imported from @korzen/bot-protection. On Vercel, ipFrom.vercel. Behind Cloudflare, ipFrom.cloudflare reads CF-Connecting-IP, if your server only takes traffic from Cloudflare.

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:3000/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.

  1. Set NUXT_KORZEN_API_KEY in the server's environment 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

  • h3's getRequestIP(event, { xForwardedFor: true }) behind a proxy that appends to X-Forwarded-For. It takes the first entry, which the visitor writes, so a bot picks its own IP.
  • The API key in runtimeConfig.public. Everything there reaches the browser; the key belongs at the top level of runtimeConfig.
  • The call in middleware. 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: set ip to the visitor's address (the visitor's IP). In Docker on your machine it is the bridge's address, and harmless there.