How-to · Checked November 22, 2026

How to add a contact form to a Next.js static export

With output: 'export', Next.js builds plain files and nothing runs on a server. Server Actions and route handlers that accept a POST are not available, so the form has to post from the browser to an outside endpoint.

Make the form a client component and send it with fetch to a form backend. It builds and deploys like any other static page.

Why the usual Next.js answers do not apply

  • Server Actions need a running Next.js server. A static export has none.
  • Route handlers in a static export can only produce static GET responses at build time. They cannot receive a form.
  • Environment variables without the NEXT_PUBLIC_ prefix are not available in the browser, so you cannot hide an email API key in the page.

Steps

  • Create a form at a form backend and copy the endpoint. In FormTango it looks like https://formtango.com/f/YOUR_FORM_ID.
  • Create a component file that starts with the 'use client' directive.
  • Add the form and a submit handler that posts to the endpoint with fetch.
  • Use the component on your contact page, then run your build and deploy the out folder.
  • Send one message from the deployed site.

The client component

The handler is the same as in any React app:

'use client';

const [status, setStatus] = useState('idle');

async function send(event) {

event.preventDefault();

setStatus('sending');

const response = await fetch('https://formtango.com/f/YOUR_FORM_ID', { method: 'POST', headers: { Accept: 'application/json' }, body: new FormData(event.currentTarget) });

setStatus(response.ok ? 'sent' : 'error');

}

The Next.js guide has the whole file in TypeScript, including the form markup and the spam trap.

Or use no JavaScript at all

A plain form with an action also works in a static export, and then the component does not need to be a client component:

<form action="https://formtango.com/f/YOUR_FORM_ID" method="POST">

The visitor goes to a thank-you page after sending. On FormTango's paid plans a hidden _redirect field sends them to a page of your own, such as a /thanks route in the same export.

Keeping the endpoint in configuration

If you want different forms for preview and production, read the endpoint from a variable named with the NEXT_PUBLIC_ prefix. It is inlined at build time. That is fine here, because a form endpoint is not a secret.

If it does not work

  • The build fails mentioning Server Actions or dynamic route handlers: remove them. The form does not need them.
  • A hook error says the component must be a client component: add 'use client' at the top of the file.
  • The request fails in the console: check the endpoint for typos and see how to fix a CORS error when submitting a form.
  • Moving an existing API route? See replace a contact API route.

Common questions

Can I use Server Actions with a Next.js static export?
No. Server Actions need a Next.js server, and a static export produces only files. Post the form from the browser to a form endpoint.
Does a form backend work with the App Router and the Pages Router?
Yes. The form posts from the browser, so it does not depend on which router you use or where the site is hosted.
Do I need an API key in my Next.js app for this?
No. The form endpoint is the only thing in your code, and it can only receive submissions.

Written by the FormTango team. Facts about other services were read from their own pages on November 22, 2026. Spotted something out of date? Tell us and we will fix it.

Keep reading