Add a contact form to an Astro site

An Astro page can hold an ordinary HTML form that posts to FormKeep. It needs no adapter, endpoint or server rendering, so it works on a fully static site.

What you need

Steps

  1. Create a form in FormKeep and copy its URL from the Setup tab.
  2. Create src/pages/contact.astro and paste the form, with your URL as the action.
  3. Optionally add the script below the form to keep visitors on the page.
  4. Run the site, send a test message and open the form in FormKeep to see it.

The form

The simplest version is the form alone:

<form action="https://formkeep.com/f/YOUR-TOKEN" method="POST">
  <input name="email" type="email" required />
  <textarea name="message" required></textarea>
  <button type="submit">Send</button>
</form>

To stay on the page and show your own message, add the script. Without JavaScript the form still posts normally. This is the whole page:

---
// src/pages/contact.astro
---
<form id="contact" action="https://formkeep.com/f/YOUR-TOKEN" method="POST">
  <label>
    Email
    <input name="email" type="email" required />
  </label>
  <label>
    Message
    <textarea name="message" required></textarea>
  </label>
  <button type="submit">Send</button>
  <p id="contact-status" role="status"></p>
</form>

<script>
  const form = document.getElementById('contact') as HTMLFormElement;
  const status = document.getElementById('contact-status') as HTMLElement;

  form.addEventListener('submit', async (event) => {
    event.preventDefault();
    status.textContent = 'Sending...';
    try {
      const response = await fetch(form.action, {
        method: 'POST',
        headers: { Accept: 'application/json' },
        body: new FormData(form),
      });
      if (!response.ok) throw new Error(`FormKeep answered ${response.status}`);
      form.reset();
      status.textContent = 'Thanks, we got your message.';
    } catch (error) {
      status.textContent = 'Something went wrong. Please try again.';
    }
  });
</script>

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.

Without the script, or if it fails to load, 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

Add a file input and enctype="multipart/form-data" to the <form>. The script's FormData sends multipart on its own.

<form action="https://formkeep.com/f/YOUR-TOKEN" method="POST" enctype="multipart/form-data">
  <input name="resume" type="file">
  <button type="submit">Send</button>
</form>

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