Skip to content
FormToMail

Static sites & frameworks

How do I add a contact form to an Astro site?

By the FormToMail team · Checked against official sources · Updated

Astro builds static HTML by default, and a static page can't receive a POST. You have two options: point the form at a form endpoint (works on any static Astro build, zero server code), or enable on-demand rendering with an adapter and handle the POST in an Astro Action or API endpoint that sends the email through a provider such as Resend.

Astro form options (per the Astro docs)
OptionNeeds an adapter / server?Where the email is sent
Form endpoint in actionNo, works with output: 'static'The form backend
API route (src/pages/api/contact.ts) with POSTYes, on-demand renderingYour code, via an email API
Astro Actions (astro:actions)Yes, actions run on the serverYour code, via an email API

Option 1: static Astro + form endpoint

Paste this into any .astro page or component. Nothing else to configure.

astro
---
// src/components/Contact.astro
---
<form action="https://getformtomail.com/f/YOUR_FORM_ID" method="POST">
  <input name="name" required>
  <input name="email" type="email" required>
  <textarea name="message" required></textarea>
  <input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
  <button type="submit">Send</button>
</form>

Option 2: Action or API route

  1. Add a server adapter (Node, Vercel, Netlify, Cloudflare) and enable on-demand rendering for the route, per Astro's on-demand rendering guide.
  2. Create an API route that exports POST and reads await request.formData(), or define an Action with input validation.
  3. Send the email from the server with your email provider's SDK and a secret key in env vars.
  4. Add your own spam checks: honeypot, Turnstile, rate limiting.

Quick answers

How do I add a contact form to an Astro site?

On a static build, set the form's action to a form endpoint. On a server build, handle it in an Action or API route.

Do Astro Actions work on a fully static site?

Actions run on the server, so the action routes need on-demand rendering and an adapter; see Astro's Actions and on-demand rendering docs.

Can I use Formspree or Web3Forms with Astro?

Yes, any form endpoint works in plain HTML inside an .astro file.

What to do next

Keep Astro fully static: create an endpoint and paste it into your form.

Get your form endpoint

Sources

Checked: . Prices, limits and features of other services change: confirm current values at the official links above. Product names belong to their owners; we are not affiliated with them.