Stop form spam in Hono
Updated
This guide adds Bot403 to a contact form in Hono. The browser solves a small proof of work. Then a middleware checks the token with one call to @bot403/node, before your route accepts the message. The visitor sees no puzzle, and the script sets no cookie. The free plan covers 5,000 checks a month and needs no card.
The example uses Hono 4.13.18, @bot403/node 1.0.2 and @hono/node-server 2.1.8. On 13 November 2026, the code on this page ran in a fresh Hono app on Node.js 26.13.1, with a fake fetch that gives the documented answers of the verify API. A good token went through, also from a multipart form and from a JSON body. A failed token and a filled honeypot were refused. A capped month and an API that did not answer let the form through.
Before you start
Sign in. Create a site. Copy its public key (pk_) and its secret key (sk_). Keep the secret key out of your code and your repository.
npm install hono @bot403/node
export BOT403_SECRET_KEY=sk_live_YOUR_SECRET_KEY # Node.js, Bun and DenoOn Cloudflare Workers, the key is a secret, not an environment variable. Put it in .dev.vars for wrangler dev, and run npx wrangler secret put BOT403_SECRET_KEY for production (Cloudflare docs, secrets). Add .dev.vars to .gitignore.
The app has this layout:
app.ts # createApp() and the requireBot403 middleware
index.ts # the entry for Cloudflare Workers, Bun and Deno
server.ts # the entry for Node.js
test/contact.test.ts1. The form
The script tag protects every form on the page. Before the browser sends the form, the script adds a hidden _bot403 field with the token, and a hidden honeypot field (by default _website). In this example, Hono serves the page from GET /contact, as the page string in app.ts.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact</title>
<script src="https://bot403.com/v1/bot403.js" data-bot403-key="pk_live_YOUR_PUBLIC_KEY" async></script>
</head>
<body>
<form method="post" action="/contact">
<label>Email <input name="email" type="email" required></label>
<label>Message <textarea name="message" required></textarea></label>
<button type="submit">Send</button>
</form>
</body>
</html>2. The middleware and the app
requireBot403 is a middleware, so you can put it on every form route. It sends the whole form to https://bot403.com/api/v1/verify with your secret key. The route runs only when the answer is ok. If Bot403 does not answer, the middleware lets the form through and logs a warning. A wrong secret key or too many verify calls a minute throw an error, so you see them and fix them.
import { Hono } from "hono";
import { env } from "hono/adapter";
import { createMiddleware } from "hono/factory";
import { Bot403, Bot403Error } from "@bot403/node";
type MakeClient = (secretKey: string) => Bot403;
const page = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact</title>
<script src="https://bot403.com/v1/bot403.js" data-bot403-key="pk_live_YOUR_PUBLIC_KEY" async></script>
</head>
<body>
<form method="post" action="/contact">
<label>Email <input name="email" type="email" required></label>
<label>Message <textarea name="message" required></textarea></label>
<button type="submit">Send</button>
</form>
</body>
</html>`;
// Checks the form before the route handler runs. Put it on each form route.
export function requireBot403(makeClient: MakeClient) {
return createMiddleware(async (c, next) => {
// env() reads c.env on Workers and process.env on Node.js, Bun and Deno
const { BOT403_SECRET_KEY } = env<{ BOT403_SECRET_KEY: string }>(c);
const bot403 = makeClient(BOT403_SECRET_KEY);
let result;
try {
if (c.req.header("content-type")?.startsWith("application/json")) {
// parseBody() reads only form bodies, so read the token from the JSON
const body = await c.req.json();
result = await bot403.verify(body._bot403);
} else {
// 1. Post the whole form. Bot403 reads the _bot403 field and the honeypot field
result = await bot403.verifyForm(await c.req.parseBody());
}
} catch (err) {
if (err instanceof Bot403Error && err.status === 0) {
console.warn("bot403 did not answer, letting the form through:", err.message);
return next();
}
throw err; // 401 wrong key, 429 too many calls: Hono sends this to app.onError
}
// 2. Refuse when ok is false, except for reason "quota"
if (!result.ok && result.reason !== "quota") return c.text("Try again", 400);
await next();
});
}
export function createApp(
makeClient: MakeClient = (secretKey) => new Bot403({ secretKey, timeoutMs: 5000 }),
) {
const app = new Hono();
app.get("/contact", (c) => c.html(page));
app.post("/contact", requireBot403(makeClient), async (c) => {
// Hono keeps the parsed body, so you can read it again here
const { message } = await c.req.parseBody();
// A real visitor: send the email or store the message here
return c.redirect("/thanks", 303);
});
app.get("/thanks", (c) => c.text("Thanks."));
return app;
}A failed token is a normal answer, not an error: result.ok is false and result.reason says why. The verify page lists every reason. The quota reason comes only in a capped month. This code lets the form through then, so a capped month loses no message.
The middleware reads the body, and the route reads it again. In our test, the second parseBody() call returned the same fields.
3. One app, every runtime
Cloudflare Workers, Bun and Deno serve the default export as it is:
import { createApp } from "./app.ts";
// Cloudflare Workers, Bun and Deno serve the default export as it is
export default createApp();Node.js needs @hono/node-server (npm install @hono/node-server):
import { serve } from "@hono/node-server";
import { createApp } from "./app.ts";
serve({ fetch: createApp().fetch, port: 3000 }, (info) => {
console.log(`http://localhost:${info.port}/contact`);
});The trap: parseBody() ignores JSON
c.req.parseBody() reads multipart/form-data and application/x-www-form-urlencoded bodies (Hono docs, parseBody). For a JSON body, it returned an empty object in our test. Then verifyForm() sends no token, and every message gets the reason invalid, from real visitors too.
That happens when your front end sends the form with fetch and JSON.stringify. The middleware above checks the Content-Type header. For JSON, it reads the token with c.req.json() and calls bot403.verify(). This sends the token alone, so Bot403 cannot check the honeypot field. Put _bot403 in the JSON body, and read your own fields with c.req.json() in the route. If you can, send the form as new URLSearchParams(new FormData(form)) instead: it keeps the honeypot check.
A form with a file field works with no change. parseBody() reads multipart forms, and verifyForm() sends only the text fields. In our test, the file did not leave the server.
Where the secret key comes from
env(c) from hono/adapter reads the key from the right place on each runtime: c.env on Cloudflare Workers, and the environment on Node.js, Bun and Deno (Hono docs, adapter helper). Cloudflare's own note says that "by default, process.env is not available in Cloudflare Workers". That is why the middleware makes the client inside the request, not at the top of the module: on Workers, the key exists only there.
If the key is missing, new Bot403() throws "secretKey is required", and every form post gets a 500 error. On Workers, this usually means that wrangler secret put did not run. Do not catch this error and accept the form: then every form goes through unchecked. The same applies to a wrong key, which throws a Bot403Error with status 401.
Test it
Hono can answer a request with no server: app.request() returns the response. The tests give the app a real Bot403 client with a fake fetch that returns a fixed answer. They need no network and no key, and they use only node:test. Node.js 26 runs the .ts files as they are.
import { test } from "node:test";
import assert from "node:assert/strict";
import { Bot403 } from "@bot403/node";
import { createApp } from "../app.ts";
process.env.BOT403_SECRET_KEY = "sk_test";
// A real Bot403 client with a fake fetch: no network, no key
function fake(status: number, answer: object | Error) {
return (secretKey: string) =>
new Bot403({
secretKey,
fetch: async () => {
if (answer instanceof Error) throw answer;
return new Response(JSON.stringify(answer), { status });
},
});
}
const form = () => new URLSearchParams({ email: "[email protected]", message: "Hi", _bot403: "token" });
const post = (app: ReturnType<typeof createApp>, body: BodyInit, headers?: HeadersInit) =>
app.request("/contact", { method: "POST", body, headers });
test("a verified form goes through", async () => {
const res = await post(createApp(fake(200, { ok: true })), form());
assert.equal(res.status, 303);
});
test("a failed token is refused", async () => {
const res = await post(createApp(fake(200, { ok: false, reason: "invalid" })), form());
assert.equal(res.status, 400);
});
test("a filled honeypot is refused", async () => {
const res = await post(createApp(fake(200, { ok: false, reason: "honeypot" })), form());
assert.equal(res.status, 400);
});
test("a capped month lets the form through", async () => {
const res = await post(createApp(fake(402, { ok: false, reason: "quota" })), form());
assert.equal(res.status, 303);
});
test("bot403 down lets the form through", async () => {
const res = await post(createApp(fake(0, new Error("connect ECONNREFUSED"))), form());
assert.equal(res.status, 303);
});
test("a multipart form is checked too", async () => {
const body = new FormData();
body.set("email", "[email protected]");
body.set("_bot403", "token");
const res = await post(createApp(fake(200, { ok: true })), body);
assert.equal(res.status, 303);
});
test("a JSON body is checked with verify()", async () => {
const res = await post(createApp(fake(200, { ok: true })), JSON.stringify({ _bot403: "token" }), {
"Content-Type": "application/json",
});
assert.equal(res.status, 303);
});node --testThen run node server.ts. Open http://localhost:3000/contact and send the form. The browser goes to the thanks page, and the site in your dashboard shows one check. If you get a failure that you do not expect, read the troubleshooting page. It lists every reason and its fix. For Express on Node.js, read Stop form spam in Express. For a plain Cloudflare Worker in front of a static site, read Stop form spam in Hugo. All guides are in the guides index.
Stop spam, not customers.
The CAPTCHA your visitors never see.