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.
| Option | Needs an adapter / server? | Where the email is sent |
|---|---|---|
Form endpoint in action | No, works with output: 'static' | The form backend |
API route (src/pages/api/contact.ts) with POST | Yes, on-demand rendering | Your code, via an email API |
Astro Actions (astro:actions) | Yes, actions run on the server | Your code, via an email API |
Option 1: static Astro + form endpoint
Paste this into any .astro page or component. Nothing else to configure.
---
// 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
- Add a server adapter (Node, Vercel, Netlify, Cloudflare) and enable on-demand rendering for the route, per Astro's on-demand rendering guide.
- Create an API route that exports
POSTand readsawait request.formData(), or define an Action with input validation. - Send the email from the server with your email provider's SDK and a secret key in env vars.
- 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 endpointSources
- Astro Docs: Build forms with API routes
- Astro Docs: Actions
- Astro Docs: Endpoints
- Astro Docs: On-demand rendering
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.