Next.js CAPTCHA Integration
Add a Next.js CAPTCHA to a Route Handler or Server Action in a few lines of TypeScript. Our React widget runs on the client, the check happens on your server, and bot spam stays out of logins, sign-ups and contact forms. EU-hosted, GDPR-compliant, no image puzzles.
Quickstart
How the integration works
1. Create a CAPTCHA
Create a user account or log in with an existing one. Then create a new CAPTCHA or pick an existing one. If you want to try TrustCaptcha first, every plan comes with a 14-day free trial.
The CAPTCHA overview page shows your site key and license key, and you create your API key there. Add your website’s domains to the authorized domains in the CAPTCHA security rules so they can use your CAPTCHA.


2. Add the CAPTCHA widget to your form component
Use our React wrapper inside the client component that renders your form. The widget runs the CAPTCHA in the background and adds a hidden tc-verification-token field on submit, which Server Actions and Route Handlers receive as form data.
First, install our TrustCaptcha React package:
npm i @trustcomponent/trustcaptcha-reactThen add the <TrustcaptchaComponent> to the client component that renders your form:
"use client";
import { TrustcaptchaComponent } from "@trustcomponent/trustcaptcha-react";
export function ContactForm() {
return (
<form method="post" action="/api/contact">
<input type="email" name="email" required />
<TrustcaptchaComponent sitekey="<your_site_key>" />
<button type="submit">Send</button>
</form>
);
}The CAPTCHA widget will then be displayed inside your form:

Need detailed information about the CAPTCHA React widget integration?
Our documentation has the full widget reference, including themes, languages and custom design.
Read the documentation
3. Validate the token in a Route Handler or Server Action
In a Next.js Route Handler (or Server Action), take the verification token from the form data, look up the result via our Node.js library, and decide whether to accept the request.
First, install our TrustCaptcha Node.js library:
npm i @trustcomponent/trustcaptcha-nodejsThen validate the token inside your Route Handler and act on the result:
import { NextResponse } from "next/server";
import { TrustCaptcha } from "@trustcomponent/trustcaptcha-nodejs";
export async function POST(request: Request) {
const formData = await request.formData();
const token = String(formData.get("tc-verification-token") ?? "");
try {
const result = await TrustCaptcha.getVerificationResult(process.env.TRUSTCAPTCHA_API_KEY!, token);
if (!result.verificationPassed || result.score > 0.5) {
return NextResponse.json({ error: "CAPTCHA verification failed." }, { status: 400 });
}
} catch {
return NextResponse.json({ error: "CAPTCHA verification failed." }, { status: 400 });
}
// CAPTCHA passed — process the request
return NextResponse.json({ status: "ok" });
}Need detailed information about the Next.js CAPTCHA integration?
For full step-by-step instructions, including a Server Action variant and a reusable verifier utility, please read our documentation.
Read the documentation
Other backend framework instead of Next.js?
If you use a different framework, pick the matching recipe here. If your framework isn’t listed, your software developers can integrate the verification themselves using our documentation or ask our support team for a pre-built integration.
4. Test your integration
TrustCaptcha is now active. Send a test request and check that it goes through.

FAQs
Does the Next.js CAPTCHA work with the App Router and the Pages Router?
Can I verify the CAPTCHA from a Server Action?
Does the React widget work inside a server component?
Can I run the CAPTCHA verification on the Edge runtime?
Where do I store the CAPTCHA API key in a Next.js project?
How do I keep the CAPTCHA verification utility out of the client bundle?
TrustCaptcha blocks spam and bots, not customers. No puzzles, GDPR-compliant, EU-hosted.


Protect your Next.js application with TrustCaptcha in just a few steps!
- EU-hosted & GDPR-compliant
- No puzzles
- Try free for 14 days