Skip to content
Wrenform

Docs / Frameworks

Contact form for Astro

A static Astro component that emails you every submission, plus an optional version that stays on the page.

Last updated 2026-11-14View as Markdown
---
// src/components/ContactForm.astro
const endpoint = "https://wrenform.com/f/YOUR_FORM_ID";
---
<form action={endpoint} method="POST">
  <label>Name <input type="text" name="name" required /></label>
  <label>Email <input type="email" name="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>
  <input type="text" name="_honey" style="display:none" tabindex="-1" autocomplete="off" />
  <button type="submit">Send</button>
</form>

Steps

  1. Create a form and copy its form ID from the Setup page.
  2. Save the component as src/components/ContactForm.astro and replace YOUR_FORM_ID.
  3. Use it in any page: import ContactForm from "../components/ContactForm.astro"; then <ContactForm />.
  4. Build and deploy as usual. The form works with output: "static": you don't need an adapter, an API endpoint or server output.
  5. Add your site's origin under Allowed origins in the form's settings.

To keep the form ID out of the component, put it in an environment variable that starts with PUBLIC_ and read it with import.meta.env.PUBLIC_WRENFORM_ID. The ID is public anyway; this is only for convenience.

Stay on the page (optional)

The component above leaves the page and comes back through a redirect. To show the result inline instead, add a small script. Astro bundles <script> tags in components, and the form still works without JavaScript.

---
const endpoint = "https://wrenform.com/f/YOUR_FORM_ID";
---
<form id="contact" action={endpoint} method="POST">
  <label>Name <input type="text" name="name" required /></label>
  <label>Email <input type="email" name="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>
  <input type="text" name="_honey" style="display:none" tabindex="-1" autocomplete="off" />
  <button type="submit">Send</button>
  <p role="status"></p>
</form>

<script>
  const form = document.querySelector<HTMLFormElement>("#contact")!;
  const status = form.querySelector<HTMLParagraphElement>("[role=status]")!;
  form.addEventListener("submit", async (event) => {
    event.preventDefault();
    status.textContent = "Sending…";
    try {
      const res = await fetch(form.action, {
        method: "POST",
        headers: { Accept: "application/json" },
        body: new FormData(form),
      });
      const result = await res.json();
      if (result.ok) {
        form.reset();
        status.textContent = "Thanks, your message was sent.";
      } else {
        status.textContent = result.error.message;
      }
    } catch {
      status.textContent = "Network error. Please try again.";
    }
  });
</script>

Using React, Svelte or Vue islands? The Next.js guide shows a React component that works in an island with client:load.

Test it

Run astro dev, open the page and send the form. Then check your dashboard and inbox. On localhost, leave Allowed origins empty, or add http://localhost:4321 while you test.

Common problems

  • origin_not_allowed in development. Your allowed origins list your live site only. Add http://localhost:4321, or test on the live site.
  • The status line says nothing. The script didn't run. Check that the <script> tag sits in the component, not inside a <template> or a framework island.
  • Submissions are marked as spam. Someone, or an autofill tool, filled the hidden _honey field. Keep it hidden and keep autocomplete="off".

Next steps

  • My form isn't working: symptom, cause and fix.
  • Redirects: send visitors to your own thank-you page.
  • Spam: sensitivity, the timing field and Turnstile.
  • Webhooks: send submissions to your own code (Pro and Agency).

Something wrong or unclear on this page? Report a problem: write to hello [at] wrenform.com with the page address (/docs/frameworks/astro).