keepcalculator

06 Numbers and Powers

Astro Contact Forms: Netlify Endpoints or Server Logic?

Card No. 06·66604 · Updated · Standard formula, shown below

For Astro contact forms, the direct answer is: use Netlify’s built-in form handling (hosted endpoints) for most projects, and reserve custom server logic for specific cases like spam filtering, integrations, or when you’re not on Netlify. Hosted endpoints require no backend code, handle submissions reliably, and integrate with Netlify’s dashboard; server logic gives you full control but adds complexity and maintenance.

How Netlify Forms Work with Astro

Netlify Forms is a hosted service that intercepts form submissions to your site. With Astro, you simply add a netlify attribute to your form tag, and Netlify’s build system automatically detects it. When a user submits, Netlify stores the submission in your dashboard, sends email notifications, and can trigger webhooks. You don’t need any server-side code—your Astro site remains static, and Netlify handles the backend.

To set it up:

  • Add the netlify attribute to your <form> tag in an Astro component.
  • Include hidden inputs like form-name if you’re using a single-page app or dynamic rendering.
  • Deploy to Netlify; the service picks up the form automatically.

This approach works for both static and server-rendered Astro sites, as long as the HTML form is present in the final output.

When to Use Server Logic Instead

Server logic (e.g., an Astro API endpoint, a Netlify Function, or an external server) becomes necessary when you need:

  • Custom validation beyond simple required fields (e.g., checking against a database).
  • Integration with third-party services that require server-side secrets (e.g., sending data to a CRM, Slack, or a custom API).
  • Advanced spam protection like honeypot fields, rate limiting, or CAPTCHA verification that must be server-side.
  • Processing file uploads that need to be stored outside Netlify’s default handling.

In Astro, you can create API endpoints in the src/pages/api/ directory. These run on Netlify Functions when deployed, or on a Node server if you’re using SSR. You’d then use fetch from your form’s onSubmit handler to POST to that endpoint.

Comparing the Two Approaches

Here’s a practical comparison:

  • Setup time: Netlify Forms is nearly zero-config; server logic requires writing and deploying a function.
  • Cost: Netlify Forms is included in free plans (with limits); server functions also have free tiers but may incur costs at scale.
  • Data access: Netlify Forms gives you a dashboard and export; server logic lets you store data anywhere (e.g., a database).
  • Spam filtering: Netlify provides basic spam filtering (Akismet integration); server logic allows custom rules.
  • Flexibility: Server logic can transform data, send custom emails, or integrate with any API; Netlify Forms is limited to its built-in actions.

For most contact forms—name, email, message—Netlify Forms is sufficient and faster to implement. If you need to do something with the data beyond receiving an email, server logic is the way.

Common Problems and Solutions

Here are typical issues and how to resolve them:

  • Form not appearing in Netlify dashboard: Ensure the netlify attribute is present and the form is rendered in the static HTML. If using Astro’s client-side routing, add a hidden form-name input.
  • Submissions going to spam: Enable Netlify’s spam filter or add a honeypot field (a hidden input that bots fill).
  • Custom success message: Use JavaScript to intercept the submit event and display a message without reloading.
  • Handling file uploads: Netlify Forms supports file uploads, but you must set the enctype to multipart/form-data and add a file input.

Costs and Limits

Netlify Forms has a free tier that includes 100 submissions per site per month, with additional submissions available on paid plans. Server functions on Netlify also have a free tier (125,000 requests per month) and then scale with usage. For a typical contact form, the free tiers are more than enough. If you expect high volume, consider server logic to control costs and data storage.

Alternatives to Netlify

If you’re not on Netlify, other static hosts offer similar features: Vercel has Forms (now integrated into Vercel’s dashboard), and Cloudflare Pages has a form handling feature. Alternatively, you can use third-party services like Formspree, Getform, or Basin that work with any static site. Each has its own free tier and limits, so choose based on your hosting and volume needs.

Frequently Asked Questions

Do I need a server for an Astro contact form? No, if you use Netlify Forms or a third-party form backend, you don’t need to manage a server. The service handles submissions for you.

How do I add a honeypot to a Netlify form? Add a hidden input field that humans won’t see (e.g., <input name="website" type="text" style="display:none" />). Netlify automatically ignores submissions where this field is filled.

Can I use Netlify Forms with Astro’s SSR mode? Yes, as long as the form HTML is rendered in the response. Netlify’s build process detects the form during the build, so it works with static or SSR.

What happens if I exceed the free submission limit? Netlify will stop accepting submissions for that form until the next month or until you upgrade. You’ll get an email notification.

Is server logic more secure than hosted endpoints? Not necessarily. Netlify Forms handles spam filtering and basic security. Server logic gives you more control but also more responsibility for security (e.g., sanitizing inputs, protecting secrets).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →