Choose a server-capable deployment
A Server Action is useful when you already have a Next.js server, need private validation, or want to enrich the submission. A static export can submit directly from the browser.
Define the action and form
Put this Server Component in app/contact/page.tsx. Set FORMS4WEB_ENDPOINT and FORMS4WEB_ACCESS_KEY in the deployment environment. For a restricted form, set FORMS4WEB_SITE_ORIGIN to the public origin of your Next.js site. This example assumes CAPTCHA is disabled.
TSX
import { redirect } from "next/navigation";
async function sendContact(values: FormData) {
"use server";
const endpoint = process.env.FORMS4WEB_ENDPOINT;
const key = process.env.FORMS4WEB_ACCESS_KEY;
if (!endpoint || !key) throw new Error("Missing form configuration");
const email = values.get("email");
const message = values.get("message");
if (typeof email !== "string" || !email.includes("@") ||
typeof message !== "string" || !message.trim() || message.length > 4000) {
redirect("/contact?sent=0");
}
const headers = new Headers({ "Content-Type": "application/json", Accept: "application/json" });
const siteOrigin = process.env.FORMS4WEB_SITE_ORIGIN;
if (siteOrigin) headers.set("Origin", siteOrigin);
let accepted = false;
try {
const response = await fetch(endpoint, {
method: "POST",
headers,
body: JSON.stringify({ access_key: key, email, message }),
});
accepted = response.ok && (await response.json()).success === true;
} catch {
accepted = false;
}
redirect(accepted ? "/contact?sent=1" : "/contact?sent=0");
}
export default function Contact() {
return <form action={sendContact}>
<label>Email <input name="email" type="email" required /></label>
<label>Message <textarea name="message" maxLength={4000} required /></label>
<button>Send message</button>
</form>;
}Render outcome on the contact page
Use the sent query parameter or an action state to show whether the message was accepted. Verify the deployment allows outbound HTTPS to your Forms4Web endpoint.