Why a CAPTCHA is the wrong first answer
Most teams add reCAPTCHA or Turnstile the day the first spam lands in their inbox. It works, but it taxes every real visitor: a widget to load, a checkbox or a puzzle, a few percent of people who give up. And the spam that matters most, hand-typed offers and lead bots that solve captchas, still gets through.
The better order is the opposite: check every submission on the server first, against what is known about the sender, and only think about a visible challenge if that is not enough. A sender email, an IP address and the message text are enough to catch the vast majority of spam, because the same bots hit hundreds of thousands of other sites first.
What you need
- A Next.js app (App Router, 13.4 or newer) with a contact form.
- A CleanTalk access key: free 7-day trial, no card, at https://cleantalk.org/register?product_name=anti-spam&platform=npm.
- The SDK:
npm install cleantalk-antispam. No runtime dependencies, works on Node.js 18+, the Edge runtime and Bun.
Put the key in .env.local:
CLEANTALK_API_KEY=your_access_key
Option 1: Route Handler
Create app/api/contact/route.ts:
import { NextResponse } from "next/server";
import { CleanTalk, getClientIp, getEventToken } from "cleantalk-antispam";
const cleantalk = new CleanTalk({ apiKey: process.env.CLEANTALK_API_KEY! });
export async function POST(request: Request) {
const form = await request.formData();
const verdict = await cleantalk.checkMessage({
email: String(form.get("email") ?? ""),
name: String(form.get("name") ?? ""),
message: String(form.get("message") ?? ""),
ip: getClientIp(request.headers),
eventToken: getEventToken(form),
senderInfo: {
REFFERRER: request.headers.get("referer") ?? undefined,
USER_AGENT: request.headers.get("user-agent") ?? undefined,
},
});
if (verdict.isSpam) {
return NextResponse.json({ error: "Your message looks like spam." }, { status: 400 });
}
// send the email, save to the database, notify Slack
return NextResponse.json({ ok: true });
}
That is the whole integration. verdict.reason holds the explanation (“Sender blacklisted”, “Submitted too quickly”), verdict.codes the rule codes, verdict.requestId a link to the request in your CleanTalk dashboard.
Option 2: Server Action
If your form posts to a Server Action, the check looks the same. app/contact/actions.ts:
"use server";
import { headers } from "next/headers";
import { CleanTalk, getClientIp, getEventToken } from "cleantalk-antispam";
const cleantalk = new CleanTalk({ apiKey: process.env.CLEANTALK_API_KEY! });
export async function submitContact(form: FormData) {
const h = await headers();
const verdict = await cleantalk.checkMessage({
email: String(form.get("email") ?? ""),
message: String(form.get("message") ?? ""),
ip: getClientIp(h),
eventToken: getEventToken(form),
});
if (verdict.isSpam) return { ok: false, error: "Your message looks like spam." };
return { ok: true };
}
Test it in 30 seconds
Submit the form with the email stop_email@example.com. CleanTalk always treats this address as spam, so you can confirm the rejection path without waiting for a real bot. Then submit with your own address: it goes through.
Step 2, optional: catch bots that look human
The server-side check already knows the sender’s email and IP reputation and reads the text. To also catch bots that drive a real browser, add the CleanTalk Bot Detector to the page with the form. It is one script tag:
// app/layout.tsx
import Script from "next/script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script src="https://fd.cleantalk.org/ct-bot-detector-wrapper.js" strategy="afterInteractive" />
</body>
</html>
);
}
The script watches mouse and keyboard activity, headless browser signs and timing, and adds a hidden field ct_bot_detector_event_token to every form. The SDK helper getEventToken(form) picks it up and sends it with the check, as in the examples above. Nothing changes for the visitor.
If your form is a client component that posts JSON with fetch, or you render inputs yourself, use the small front-end package instead of the raw script tag. It loads the script once and gives you the token as React state:
"use client";
import { useBotDetector } from "cleantalk-bot-detector/react";
export function ContactForm() {
const token = useBotDetector();
return (
<form action="/api/contact" method="post">
<input type="hidden" name="ct_bot_detector_event_token" value={token ?? ""} />
<input name="email" type="email" required />
<textarea name="message" required />
<button type="submit">Send</button>
</form>
);
}
npm install cleantalk-bot-detector. It also exports waitForEventToken() for fetch-based submits and works with Vue and Svelte: https://www.npmjs.com/package/cleantalk-bot-detector.
What happens when CleanTalk is unreachable
By default the SDK fails open: if the cloud does not answer within 5 seconds, verdict.skipped is true, isSpam is false and your form keeps working. If you prefer to fail closed, pass onError: "throw" and handle CleanTalkError.
Registration forms
For sign-ups, newsletter forms and anything without a message field, call checkNewUser() with the same fields minus message. It uses the check_newuser method, tuned for registrations.
Summary
- One server-side call per submission, no widget for visitors.
npm install cleantalk-antispam, 15 lines in a Route Handler or Server Action.- Optional Bot Detector (script tag or
cleantalk-bot-detectorpackage) for behaviour signals. - Fails open by default, so a network issue never blocks a real customer.
SDK and examples: https://github.com/CleanTalk/node-antispam. Package: https://www.npmjs.com/package/cleantalk-antispam. CleanTalk Anti-Spam is $12 per year per website with unlimited API calls, 7-day free trial.