🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
CMS & Integrations••By Komal Nakrani

Astro Form Backend Options for Static Sites

Astro form backend options for static sites: Netlify Forms, Formspree, Web3Forms, Basin, Formspark, or Astro Actions on a server, plus spam protection and code.

#Astro#Astro Form Backend#Forms#Contact Form#Actions

A static Astro site has no server to receive form submissions, so you need an Astro form backend: a hosted form service that accepts the POST and emails or stores the data, or a small server route of your own. The easy options are Netlify Forms (if you host on Netlify), Formspree, Web3Forms, Basin and Formspark, which all work by pointing a plain HTML form’s action at their endpoint. If you’d rather own the logic, add an adapter and handle the form with Astro Actions or an API endpoint.

This guide compares the hosted services and the self-hosted route, with working code for each and the spam protection every public form needs.

Key Takeaways

  • Hosted form backends need no adapter: set the form’s action to their URL and keep the site fully static.
  • Netlify Forms needs only a data-netlify="true" attribute, but only works on sites hosted by Netlify.
  • Astro Actions give you validated, type-safe form handling, but require an adapter and on-demand rendering.
  • Every public form needs spam protection: a honeypot field at minimum, Turnstile or similar for busier sites.
  • Progressive enhancement: build the form to work without JavaScript, then add fetch for an inline success message.

Astro form backend options compared

Option Needs adapter Setup Where data goes Good for
Netlify Forms No data-netlify="true" Netlify dashboard, email, webhooks Sites already on Netlify
Formspree No Form endpoint URL Dashboard, email, integrations Most static sites
Web3Forms No Access key in a hidden field Email Simple contact forms
Basin No Form endpoint URL Dashboard, email, webhooks Agencies with many sites
Formspark No Form endpoint URL Dashboard, email Simple, low-volume forms
Astro Actions Yes src/actions/index.ts Wherever your code sends it Custom logic, databases, CRMs
API endpoint Yes src/pages/api/*.ts Wherever your code sends it Non-Astro clients, webhooks

Free tiers and submission limits differ and change, so check each service’s pricing page for your expected volume. For a portfolio or small-business contact form, every hosted option above has a plan that fits.

Hosted form services: the general pattern

Every hosted backend works the same way: a normal HTML form posts to their URL.

---
// src/components/ContactForm.astro
const endpoint = 'https://formspree.io/f/your-form-id';
---
<form action={endpoint} method="POST" class="contact-form">
  <label>
    Name
    <input type="text" name="name" required autocomplete="name" />
  </label>
  <label>
    Email
    <input type="email" name="email" required autocomplete="email" />
  </label>
  <label>
    Message
    <textarea name="message" rows="5" required></textarea>
  </label>
  <!-- Honeypot: hidden from people, filled in by many bots -->
  <input type="text" name="_gotcha" tabindex="-1" autocomplete="off" class="hp" />
  <button type="submit">Send</button>
</form>

<style>
  .contact-form { display: grid; gap: 1rem; max-width: 32rem; }
  .hp { position: absolute; left: -9999px; }
</style>

Without JavaScript, the browser posts the form and the service shows its own thank-you page or redirects to one you configure, such as /thanks/. Field names and honeypot names vary by service, so check its docs.

Netlify Forms

On Netlify, you don’t need an endpoint. Netlify parses your built HTML at deploy time and wires up any form with the attribute:

---
// src/components/NetlifyContactForm.astro
---
<form name="contact" method="POST" data-netlify="true" netlify-honeypot="bot-field" action="/thanks/">
  <input type="hidden" name="form-name" value="contact" />
  <p class="hp"><label>Leave this empty <input name="bot-field" /></label></p>
  <label>Email <input type="email" name="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>
  <button type="submit">Send</button>
</form>

The hidden form-name field matters: it tells Netlify which form a submission belongs to, especially if you submit with fetch. Because detection happens on the built HTML, the form must be in a prerendered page, not created only by client-side JavaScript. It won’t work on any other host.

Web3Forms

Web3Forms identifies your form by an access key in a hidden field rather than a unique URL, and emails submissions to the address tied to that key. It’s a good fit for one simple contact form. The key is visible in the HTML, which is expected; the service ties it to your email so it can’t be redirected elsewhere.

Formspree, Basin and Formspark

These give each form its own endpoint and a dashboard of submissions, with email notifications. They differ in integrations (webhooks, Zapier-style connectors, autoresponders), team features and spam filtering. Pick based on how many forms and sites you manage and where the data needs to go.

Adding an inline success message

Posting to another site’s thank-you page works, but it takes the visitor away. A small script submits with fetch and shows a message in place, while the form still works without JavaScript:

<p class="form-status" role="status" hidden></p>

<script>
  const form = document.querySelector<HTMLFormElement>('.contact-form');
  const status = document.querySelector<HTMLParagraphElement>('.form-status');

  form?.addEventListener('submit', async (event) => {
    event.preventDefault();
    const response = await fetch(form.action, {
      method: 'POST',
      body: new FormData(form),
      headers: { Accept: 'application/json' },
    });
    if (status) {
      status.hidden = false;
      status.textContent = response.ok
        ? 'Thanks, your message was sent.'
        : 'Something went wrong. Please try again or email us directly.';
    }
    if (response.ok) form.reset();
  });
</script>

Most services return JSON when you send Accept: application/json. For Netlify Forms, post URL-encoded data to / instead; their docs show the format. Our static contact forms guide builds a complete, accessible version.

Your own backend with Astro Actions

If you need custom validation, want to write to a database or CRM, or don’t want a third party holding submissions, handle the form yourself. Actions are Astro’s built-in way to do it, with input validated by a Zod schema:

// src/actions/index.ts
import { defineAction, ActionError } from 'astro:actions';
import { z } from 'astro/zod';

export const server = {
  contact: defineAction({
    accept: 'form',
    input: z.object({
      name: z.string().min(1),
      email: z.email(),
      message: z.string().min(10).max(5000),
    }),
    handler: async ({ name, email, message }) => {
      const res = await fetch('https://api.example-mail.com/send', {
        method: 'POST',
        headers: { Authorization: `Bearer ${import.meta.env.MAIL_API_KEY}` },
        body: JSON.stringify({ to: '[email protected]', replyTo: email, subject: `Message from ${name}`, text: message }),
      });
      if (!res.ok) {
        throw new ActionError({ code: 'INTERNAL_SERVER_ERROR', message: 'Could not send message' });
      }
      return { sent: true };
    },
  }),
};
---
// src/pages/contact.astro
export const prerender = false;
import { actions } from 'astro:actions';
const result = Astro.getActionResult(actions.contact);
---
{result && !result.error && <p role="status">Thanks, your message was sent.</p>}
<form method="POST" action={actions.contact}>
  <input name="name" required />
  <input name="email" type="email" required />
  <textarea name="message" required></textarea>
  <button>Send</button>
</form>

Actions need a server, so install an adapter (npx astro add netlify, cloudflare, vercel or node). The rest of the site can stay prerendered; only the contact page opts out with prerender = false. Without an adapter, the build fails with “Actions must be used with server output.” See the fix for that error and the Astro Actions guide.

Spam protection

A public form gets spam within days of launch. Layer these:

  1. Honeypot field. A hidden input that people never fill but many bots do. Free and invisible.
  2. Service-side filtering. Most hosted backends include spam scoring; turn it on.
  3. A challenge. Cloudflare Turnstile, hCaptcha or reCAPTCHA for forms that still get hit. Turnstile is usually invisible to visitors. Verify the token server-side, either through the form service’s built-in support or in your action.
  4. Rate limiting. If you run your own endpoint, limit submissions per IP.

Don’t rely on client-side JavaScript validation for security. It improves the experience, but anything in the browser can be skipped.

Choosing an Astro form backend

  • Hosted on Netlify, simple forms: Netlify Forms.
  • Any host, contact or quote forms: Formspree, Basin or Formspark for a dashboard; Web3Forms for email-only.
  • Custom logic, databases or CRMs: Astro Actions with an adapter.
  • Newsletter signups: post directly to your email provider’s form endpoint instead; see the newsletter signup tip.

Komal themes ship with contact and enquiry forms as plain HTML in static pages, so you connect them by setting the form action to one of these services; browse Astro themes or see the getting started docs. The official Astro Actions docs cover form handling in depth.

FAQ

How do I handle forms on a static Astro site?

Point the form’s action at a hosted form backend such as Formspree, Web3Forms or Basin, or use Netlify Forms if you host on Netlify. The service receives the POST and emails or stores the submission, so no server or adapter is needed.

Does Astro have built-in form handling?

Yes, with Astro Actions. Define an action with accept: 'form' and a Zod input schema, then set the form’s action to it. Actions run on the server, so the project needs an adapter and the page must render on demand.

Do Netlify Forms work with Astro?

Yes. Add data-netlify="true" and a hidden form-name input to a form in a prerendered page. Netlify detects it in the built HTML at deploy time. It only works on sites deployed to Netlify.

How do I stop spam on an Astro contact form?

Add a hidden honeypot field, turn on the form service’s spam filtering, and add a challenge such as Cloudflare Turnstile if spam continues. Verify the challenge token on the server, not in the browser.

Can I send email from an Astro form without a third-party form service?

Yes, by handling the form in an Astro Action or API endpoint and calling an email provider’s API from the server. This requires an adapter and on-demand rendering for that route.