A Next.js contact form without an API route
Drop one client component into your app and every message is spam filtered, stored in Germany and emailed to you. There is no route handler, no Server Action and no email API key to keep on a server, so it also works on a static export.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
"use client";
import { useState, type FormEvent } from "react";
type Problem = { field?: string; message: string };
export function ContactForm() {
const [sent, setSent] = useState(false);
const [problems, setProblems] = useState<Problem[]>([]);
async function send(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.currentTarget;
const response = await fetch(form.action, {
method: "POST",
headers: { Accept: "application/json" },
body: new FormData(form),
});
const result = await response.json();
setSent(result.ok);
setProblems(result.ok ? [] : result.errors);
if (result.ok) form.reset();
}
const problem = (field: string) => problems.find((p) => p.field === field)?.message;
return (
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST" onSubmit={send}>
<input type="email" name="email" required />
{problem("email") && <p>{problem("email")}</p>}
<textarea name="message" required />
<input type="text" name="_gotcha" style={{ display: "none" }} tabIndex={-1} autoComplete="off" />
<button type="submit">Send</button>
<p role="status">{sent ? "Thanks, your message is on its way." : problems.find((p) => !p.field)?.message}</p>
</form>
);
}
Set it up
-
1
Create a form in FormHaven
Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.
-
2
Add the component
Save the code above as app/contact/ContactForm.tsx, paste your address into the action, and render <ContactForm /> from app/contact/page.tsx.
-
3
Read the answer
With Accept: application/json the reply is {"ok": true}, or {"ok": false, "errors": [...]} where each error has a code, a message and, when one input caused it, the field name.
-
4
Send a test
Run next dev, fill in the form and check your inbox. Or press Send a test submission in the dashboard.
Static export works too
The Next.js docs list Server Actions among the features a static export does not support. This form never calls your own server, so you can build with output: "export" and host the out folder anywhere, GitHub Pages included.
/** @type {import("next").NextConfig} */
const nextConfig = {
output: "export",
};
module.exports = nextConfig;
On the Pages Router
The same component works in a pages/ app. Drop the "use client" line, save it under components/ and render it from pages/contact.tsx.
import { ContactForm } from "../components/ContactForm";
export default function Contact() {
return (
<main>
<h1>Contact us</h1>
<ContactForm />
</main>
);
}
Questions about this setup
- Why not a route handler with an email API?
- That works on a server, but you write and run the code, keep an API key secret, and still need spam filtering and somewhere to read old messages. It also rules out a static export.
- Can an agent set this up for me?
- Yes. Ask Claude Code or Cursor to add a FormHaven contact form. Through our MCP server it can fetch a ready React component for your form with get_form_code.
- Does it still work without JavaScript?
- Yes. The form has a real action and method, so the browser posts it the normal way and the visitor sees our thank-you page, or yours if you set one on the form.
- Can my client get the messages instead of me?
- Yes. Add up to 5 more addresses to the form. Each one confirms by a link first, and their copy carries no FormHaven branding. You can turn your own copy off.
- Where is the data stored?
- On our server at Hetzner in Falkenstein, Germany, for 1 to 365 days as you choose per form, or not at all. A data processing agreement covers every account.
More to read
Get your form address in a minute
14 days free, no card. Then €1 per form a month, €0.50 from the 11th form.