Add a contact form to a Next.js site

A Next.js form can post straight to FormKeep from the browser. You do not need an API route or a server action, and there is no API key to keep secret.

What you need

Steps

  1. Create a form in FormKeep and copy its URL from the Setup tab.
  2. Add the component below as a client component, and put your URL in FORM_URL.
  3. Render <ContactForm /> from a page, for example app/contact/page.tsx.
  4. Send a test message and open the form in FormKeep to see it.

The client component

Save this as app/contact/ContactForm.tsx. It sends the fields with fetch and shows sending, sent and error states.

'use client';

import { useState, type FormEvent } from 'react';

const FORM_URL = 'https://formkeep.com/f/YOUR-TOKEN';

export default function ContactForm() {
  const [status, setStatus] = useState<'idle' | 'sending' | 'sent' | 'error'>('idle');

  async function handleSubmit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    const form = event.currentTarget;
    setStatus('sending');
    try {
      const response = await fetch(FORM_URL, {
        method: 'POST',
        headers: { Accept: 'application/json' },
        body: new FormData(form),
      });
      if (!response.ok) throw new Error(`FormKeep answered ${response.status}`);
      form.reset();
      setStatus('sent');
    } catch (error) {
      setStatus('error');
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Email
        <input name="email" type="email" required />
      </label>
      <label>
        Message
        <textarea name="message" required />
      </label>
      <button type="submit" disabled={status === 'sending'}>
        {status === 'sending' ? 'Sending...' : 'Send'}
      </button>
      {status === 'sent' && <p role="status">Thanks, we got your message.</p>}
      {status === 'error' && <p role="alert">Something went wrong. Please try again.</p>}
    </form>
  );
}

Do not set a Content-Type header yourself with FormData: the browser adds the multipart boundary. The URL is not a secret, so it can sit in client code; there is no API key to hide.

The plain form instead

If you do not need to stay on the page, a form with an action works as ordinary HTML. It needs no JavaScript and can live in a server component such as app/contact/page.tsx:

export default function ContactPage() {
  return (
    <form action="https://formkeep.com/f/YOUR-TOKEN" method="POST">
      <label>
        Email
        <input name="email" type="email" required />
      </label>
      <label>
        Message
        <textarea name="message" required />
      </label>
      <button type="submit">Send</button>
    </form>
  );
}

The browser posts the form and leaves the page, so see the next section for where it lands.

What happens on submit

The request asks for JSON with Accept: application/json, so FormKeep does not redirect. A stored submission answers 200 with the body null; the page stays where it is and shows its own message. A wrong token answers 404 and a form switched off answers 403, and the code above shows the error message for both. The submission appears in your dashboard and your inbox.

With the plain form, there is no JSON: the browser is redirected to the redirect URL set in the form's Redirects tab, or to FormKeep's own thank-you page if none is set.

Add file uploads

With fetch, add a file input; FormData sends the form as multipart on its own. In the plain form, also add enctype="multipart/form-data" to the <form>.

<input name="resume" type="file">

Each file can be up to 10 MB and a submission can carry up to 50 files. Whether attachments are kept depends on your plan.

Troubleshooting

Looking for something else?

Email us