Stop form spam on Netlify
Updated
This guide adds Bot403 to a contact form on a static site hosted on Netlify. The browser solves a small proof of work. Then a Netlify Function checks the token with one call to https://bot403.com/api/v1/verify before it 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 site stays static: only the form post runs code.
The example uses @bot403/node 1.0.2, and the function's types come from @netlify/functions 6.1.1. On 12 November 2026, we typechecked the function with TypeScript 7.0.2 in strict mode. Then we ran it on Node.js 24.22.1, behind a small local server that served the static files and routed POST /api/contact to the function, as Netlify does. A local stand-in for bot403.com served the real Bot403 browser script and its challenges, and gave the documented verify answers. In headless Chromium, the form got a token and went through, and the same token sent a second time was refused. With curl, a form without a token and a filled honeypot were refused. A 402 quota answer, an HTML error page and an API that did not answer let the form through.
Should you use Netlify Forms instead?
Maybe. Netlify Forms needs no code. Netlify filters every submission with Akismet, and you can add a honeypot field (netlify-honeypot) or reCAPTCHA (data-netlify-recaptcha="true") to the form (Netlify docs, spam filters, read 2026-11-12). On credit-based plans, Netlify says "Forms are free and unlimited" (usage and billing). If that filter stops your spam and you are fine with Akismet, keep Netlify Forms.
Use the function in this guide when you want the form to go to your own code, for example to send the email through your own provider or to store it in your own database. Use it too when you want no puzzle and no reCAPTCHA script from Google on the page. Leave out the data-netlify and netlify attributes on the form, because they turn on Netlify Forms.
Before you start
Sign in. Create a site. Copy its public key (pk_) and its secret key (sk_). Add the package and the types to your site's package.json:
npm install @bot403/node
npm install --save-dev @netlify/functionsThe files of the example:
netlify.toml # publish directory
netlify/functions/contact.mts # the check
public/contact/index.html # the form
public/thanks/index.html[build]
publish = "public"1. 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 (_website by default). The form posts to /api/contact, the path of the function.
<!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="/api/contact">
<label>Email <input name="email" type="email" required></label>
<label>Message <textarea name="message" required></textarea></label>
<button type="submit">Send</button>
<p id="error" role="alert" hidden>Send the form again.</p>
</form>
<script>
if (new URLSearchParams(location.search).has("error")) document.getElementById("error").hidden = false;
</script>
</body>
</html>The small script at the end shows the error after a refused check. Without JavaScript, the form still posts. The error line then stays hidden.
2. The function
import type { Config } from "@netlify/functions";
import { Bot403, Bot403Error } from "@bot403/node";
const bot403 = new Bot403({ secretKey: process.env.BOT403_SECRET_KEY ?? "", timeoutMs: 5000 });
export default async (req: Request): Promise<Response> => {
const form = await req.formData();
// 1. Post the whole form. Bot403 reads the _bot403 field and the honeypot field.
let ok: boolean;
try {
const result = await bot403.verifyForm(form);
// 2. Refuse when ok is false, except for reason "quota".
ok = result.ok || result.reason === "quota";
} catch (err) {
if (!(err instanceof Bot403Error)) throw err;
if (err.status >= 400 && err.status < 500) {
// 401 wrong key, 413 body too large, 429 too many calls
console.error(`bot403 refused the verify call: ${err.code} (HTTP ${err.status})`);
ok = false;
} else {
// No answer (status 0), or a 5xx page: let the form through.
console.error(`bot403 did not answer, letting the form through: ${err.message}`);
ok = true;
}
}
const to = ok ? "/thanks/" : "/contact/?error=1";
// A real visitor: send the email or store the message here
return Response.redirect(new URL(to, req.url), 303);
};
export const config: Config = { path: "/api/contact", method: "POST" };config.path puts the function at /api/contact instead of /.netlify/functions/contact. Netlify's docs say that with a custom path, "the function is only available at that path" (functions configuration). method: "POST" limits it to form posts.
req.formData() reads both a normal form post and multipart/form-data. verifyForm sends only the text fields, so a file upload never goes to Bot403. The function answers with a 303 redirect to /thanks/, or back to the form with ?error=1.
The trap: variables in netlify.toml never reach the function
Set BOT403_SECRET_KEY in the Netlify UI, the CLI or the API, never in netlify.toml. Netlify's docs say: "Environment variables declared in a Netlify configuration file (netlify.toml) are not available to serverless functions" (functions environment variables, read 2026-11-12). The key also does not belong in a file in Git.
Two more rules from the same page. First, a new value needs a new deploy: "To apply updated environment variable values, create a new deploy." Second, on Pro and Enterprise plans, a variable has scopes, and its scope must include Functions. By default, a variable applies to all scopes.
If the key is empty, new Bot403() throws secretKey is required when the function loads. In the test run, the module failed to load, and no form reached the check. A missing key makes the function fail. It never lets a form through without a check.
When Bot403 does not answer, or the key is wrong
verifyForm resolves for every token that fails, also for quota with HTTP 402. It throws a Bot403Error only when the call itself fails. The function sorts these by err.status:
- 0, a network error or the 5-second timeout: the form goes through.
- 500 to 599, for example an HTML error page from a proxy: the form goes through.
- 400 to 499: the form is refused. That is 401 for a wrong key, 413 for a body over 64 KB, or 429 for too many verify calls in a minute (100 on the free plan).
Any other error is a bug, so the function throws it again instead of letting the form through. timeoutMs: 5000 keeps the call well inside Netlify's limit of 60 seconds for a synchronous function (functions configuration).
In the test run, an API that waited 30 seconds let the form through after 5.0 seconds, with this log line:
bot403 did not answer, letting the form through: @bot403/node: POST /api/v1/verify failed: This operation was abortedA wrong key refused every form and logged:
bot403 refused the verify call: unauthorized (HTTP 401)On Netlify, these lines are in the function's logs. Look for the second one after a deploy. Every form fails until you fix the key.
Test it
After the deploy, send a form without a token, the way a simple bot does. It must go back to the form:
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' -d '[email protected]' -d 'message=Hi' https://YOUR-SITE.netlify.app/api/contact
# 303 https://YOUR-SITE.netlify.app/contact/?error=1Then open /contact/ on your site in a browser and send the form. The browser goes to /thanks/, 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 a Hugo site with a Cloudflare Worker, read Stop form spam in Hugo. All guides are in the guides index.
Stop spam, not customers.
The CAPTCHA your visitors never see.