Form backend for Next.js
Send Next.js forms straight from the browser to a Formboost endpoint — no API route and no Server Action needed. It works with the App Router, the Pages Router and static export, and every submission keeps the visitor's own browser details for spam screening.
Reviewed by Ritu Fadadu · Last reviewed
Why Next.js forms need a backend
Next.js gives you two places to handle a form — an API route or a Server Action — and both mean your server receives the submission, then has to store it, send email and filter spam. And a statically exported site (output: "export") has no server at all, so Server Actions and API routes are not available.
One form endpoint instead
Post the form from the browser to your Formboost endpoint with fetch() in a Client Component. Formboost stores it, screens it for spam and delivers it, and the submission carries the visitor's own browser details. Use a Server Action only when you need server-side work before sending — and forward the visitor's headers when you do.
Formboost is a form backend: the form endpoint receives the submission, screens it for spam, stores it and delivers it — the same for Next.js as for any other frontend.
Setup in 5 Steps
Create a free form in the Formboost dashboard and copy its endpoint
Add NEXT_PUBLIC_FORMBOOST_ENDPOINT to .env.local and to your host's environment variables
Put the form in a Client Component ("use client") and post JSON with fetch()
Render the component from any page — it works the same on static export
Deploy, submit once, and check the submission in your dashboard
Code Example
"use client";
// app/contact/ContactForm.tsx — a Client Component that posts straight to Formboost.
import { useState, type FormEvent } from "react";
const ENDPOINT = process.env.NEXT_PUBLIC_FORMBOOST_ENDPOINT!;
export default function ContactForm() {
const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle");
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.currentTarget;
// JSON, not the FormData object itself: the endpoint refuses multipart bodies (415).
const data: Record<string, FormDataEntryValue> = Object.fromEntries(new FormData(form));
if (data.email) data._replyto = data.email;
setStatus("sending");
try {
const response = await fetch(ENDPOINT, {
method: "POST",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body: JSON.stringify(data),
});
setStatus(response.ok ? "sent" : "error");
if (response.ok) form.reset();
} catch {
setStatus("error");
}
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="name">Name</label>
<input id="name" name="name" autoComplete="name" required />
<label htmlFor="email">Email</label>
<input id="email" name="email" type="email" autoComplete="email" required />
<label htmlFor="message">Message</label>
<textarea id="message" name="message" rows={5} required />
<input type="text" name="_honey" tabIndex={-1} autoComplete="off" aria-hidden="true"
style={{ position: "absolute", left: "-9999px" }} />
<button type="submit" disabled={status === "sending"}>
{status === "sending" ? "Sending…" : "Send"}
</button>
<p role="status">
{status === "sent" && "Thanks — your message has been sent."}
{status === "error" && "Something went wrong. Please try again."}
</p>
</form>
);
}Next.js: the details
Where the request comes from matters
Formboost reads the browser's details — its address, user agent, Origin and Referer — when it rate-limits and screens a submission. Who sends the request decides whose details those are:
| Approach | Static export | Rate limit counts | Spam screening sees | Your server code |
|---|---|---|---|---|
| Client Component + `fetch()` (recommended) | Works | Each visitor | The visitor's browser | None |
Plain <form action> to the endpoint | Works | Each visitor | The visitor's browser | None — the visitor leaves for a thank-you page |
| Server Action posting to the endpoint | Not available | Your server, shared by everyone | Your server, unless you forward headers | A Server Action |
| API route posting to the endpoint | Not available | Your server, shared by everyone | Your server, unless you forward headers | A route handler |
If you use a Server Action
A Server Action is the right choice when you must do something on the server before sending — check a value against your database, add data the visitor should not see. Three things change when your server, not the browser, sends the form:
- •Every visitor shares your server's IP address. The endpoint limits each address to 10 submissions a minute by default, so a busy form can start returning
429. - •The request carries no `Origin` or `Referer`. That adds 15 points towards the 50-point spam threshold; an HTTP client that identifies itself as
axiosornode-fetchadds 25 more. The built-infetchin Next.js sendsUser-Agent: node, which is not penalised. - •Submission details describe your server. The location and device recorded with each submission come from the request, so they will be your host's.
Forwarding the visitor's headers restores the spam-screening context. It cannot change the rate-limit address, which only Formboost's own edge sets:
"use server";
// app/contact/actions.ts
import { headers } from "next/headers";
export async function sendMessage(_state: string, formData: FormData): Promise<string> {
const incoming = await headers();
const response = await fetch(process.env.FORMBOOST_ENDPOINT!, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
// The visitor's browser context; without these the request describes your server.
"User-Agent": incoming.get("user-agent") ?? "",
Referer: incoming.get("referer") ?? "",
},
body: JSON.stringify({
name: formData.get("name"),
email: formData.get("email"),
_replyto: formData.get("email"),
message: formData.get("message"),
}),
});
return response.ok ? "sent" : "error";
}Use it with useActionState(sendMessage, "idle") in a Client Component to show the result. Fields are read by name rather than spread from formData, so nothing Next.js adds to the form data is forwarded. headers() is async from Next.js 15 — see the Next.js headers() reference.
Static export and environment variables
- •With
output: "export"there is no server, so use the Client Component — it is the same code on every host. Next.js lists Server Actions among the features a static export does not support. - •
NEXT_PUBLIC_FORMBOOST_ENDPOINTis inlined at build time: set it in your host's environment variables, not only in.env.local, or the deployed form posts toundefined. - •The step-by-step version, including
useFormState-style feedback, is in the Next.js contact form tutorial; the reference is Next.js forms. For other fields, start from a form template — each has a React version that drops into a Client Component.
Features That Work Great
Keep going
Next.js setup reference
The endpoint contract, responses and reserved fields, with examples.
Next.js contact form with the App Router
A complete worked example, step by step.
AI spam protection for forms
What screens the submissions before they reach you — no CAPTCHA.
All framework guides
Contact form tutorials for every stack.
Frequently Asked Questions
Common questions about using Formboost with Next.js
Do I need an API route to handle forms in Next.js?
No. A Client Component can post straight to your Formboost endpoint with fetch(). Formboost does the server-side work, and the page can still be statically rendered.
Should I use a Server Action or fetch() from the browser?
fetch() from the browser, unless you need to do something on the server first — validate against your database, add data the visitor cannot see. A Server Action sends the form from your server, so every visitor shares your server's IP address for rate limiting and the submission arrives without the visitor's browser details unless you forward them.
Does it work with output: "export"?
Yes, with the Client Component approach. A statically exported site has no server, so Server Actions and API routes are not available there; posting from the browser needs neither.
Where should the endpoint live?
In NEXT_PUBLIC_FORMBOOST_ENDPOINT. It is public by design, like any form's action, so exposing it to the browser is fine. Remember to add it to your host's environment variables as well as .env.local.
Start building with Formboost and Next.js
It takes less than a minute to set up. No credit card required.