Add a Contact Form to Next.js (App Router)
Add a working contact form to the Next.js 15 App Router without a route handler or server action. Free, with email notifications and spam protection.
- Create a free W3Forms account and a form. Copy the access key shown once.
- Optional: in Form → Settings, list your site under Allowed domains, so posts from other sites are marked as spam. Include the port when testing locally (for example
localhost:5500). - Paste the markup below. Replace
YOUR_ACCESS_KEYwith your key. - Submit a test. Check the dashboard inbox and your notification email.
Works with static export — you do not need a Route Handler or Server Action. Put your access key in NEXT_PUBLIC_W3FORMS_ACCESS_KEY or paste it as a string for a quick test.
Always send Accept: application/json so W3Forms returns JSON instead of a browser redirect.
ContactForm.tsx
"use client";
import { useState } from "react";
export function ContactForm() {
const [status, setStatus] = useState("");
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
setStatus("Sending…");
const form = e.currentTarget;
const body = JSON.stringify(Object.fromEntries(new FormData(form)));
const res = await fetch("https://api.w3forms.com/submit", {
method: "POST",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body,
});
const data = await res.json();
if (res.ok && data.success) {
setStatus("Thanks — your message was sent.");
form.reset();
} else {
setStatus(data.message || data.error || "Something went wrong.");
}
}
return (
<form onSubmit={handleSubmit}>
<input type="hidden" name="access_key" value={process.env.NEXT_PUBLIC_W3FORMS_ACCESS_KEY} />
<label htmlFor="name">Name</label>
<input id="name" name="name" required />
<label htmlFor="email">Email</label>
<input id="email" type="email" name="email" required />
<label htmlFor="message">Message</label>
<textarea id="message" name="message" required />
<input type="text" name="_gotcha" tabIndex={-1} autoComplete="off"
aria-hidden="true" style={{ position: "absolute", left: "-9999px" }} />
<button type="submit">Send</button>
<p role="status">{status}</p>
</form>
);
}Next steps
- Generate a correct honeypot
- Validate your form HTML
- Configure redirects and spam settings
- Add a signed webhook
- Compare W3Forms to Formspree & others