Docs / Frameworks
Contact form for Next.js
A client component that posts with fetch. No server action, no API route and no secret key.
// app/contact/contact-form.tsx — no server action or API route needed
"use client";
import { useState } from "react";
export function ContactForm() {
const [status, setStatus] = useState<"idle" | "sending" | "sent" | "error">("idle");
async function onSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
const form = e.currentTarget;
setStatus("sending");
const res = await fetch("https://wrenform.com/f/YOUR_FORM_ID", {
method: "POST",
headers: { Accept: "application/json" },
body: new FormData(form),
});
setStatus(res.ok ? "sent" : "error");
if (res.ok) form.reset();
}
return (
<form onSubmit={onSubmit}>
<input type="text" name="name" required />
<input type="email" name="email" required />
<textarea name="message" required />
<input type="text" name="_honey" style={{ display: "none" }} tabIndex={-1} autoComplete="off" />
<button type="submit" disabled={status === "sending"}>Send</button>
{status === "sent" && <p>Thanks, your message was sent.</p>}
{status === "error" && <p>Something went wrong. Please try again.</p>}
</form>
);
}Steps
- Create a form and copy its form ID from the Setup page.
- Save the component as
app/contact/contact-form.tsxand replaceYOUR_FORM_ID. - Render it from a page:
import { ContactForm } from "./contact-form";then<ContactForm />. The page itself can stay a server component. - Deploy. Then add your site's origin under Allowed origins in the form's settings.
You don't need a server action or an API route. The browser posts straight to Wrenform. There is no secret key to hide: the form ID is public by design, and allowed origins stop other sites from using it.
The form ID can live in an environment variable that starts with NEXT_PUBLIC_, for example process.env.NEXT_PUBLIC_WRENFORM_ID. Next.js inlines it at build time.
Show each error
The snippet shows a generic error. To show the reason, read the JSON body. Every error has a stable code and a plain message.
const res = await fetch("https://wrenform.com/f/YOUR_FORM_ID", {
method: "POST",
headers: { Accept: "application/json" },
body: new FormData(form),
});
const result = await res.json();
if (result.ok) {
setStatus("sent");
} else if (result.error.code === "rate_limited") {
setMessage("Too many messages. Wait a minute and try again.");
} else {
setMessage(result.error.message); // see /docs/errors for every code
}Without JavaScript
A plain <form action="https://wrenform.com/f/YOUR_FORM_ID" method="POST"> works in any server component too, with no "use client". Visitors leave the page and come back through a redirect. See the HTML guide and redirects.
Test it
Run next dev, open the page and send the form. You should see the success message, the submission in your dashboard, and an email. While you test on localhost, leave Allowed origins empty or add http://localhost:3000.
Common problems
- CORS error in the console. The page's origin isn't in the form's allowed origins. Add it, including the port in development.
empty_submission. The inputs have nonameattributes, soFormDatais empty.- Hydration warning on the form. A browser extension changed the inputs. It doesn't affect the submission.
- Strict CSP. The fetch version needs
connect-src https://wrenform.com. The plain form needsform-action https://wrenform.cominstead.
Next steps
- My form isn't working: symptom, cause and fix.
- Spam: sensitivity, the timing field and Turnstile.
- Webhooks: receive each submission in your own route handler (Pro and Agency).
- Errors: every code and its fix.
- Deploying on Vercel: allowed origins for production and preview addresses.