Skip to content
Formlander

Astro contact form on your own server

Add a contact form to an Astro site with one component. It posts to a Formlander server that you host and needs no client JavaScript.

An Astro site does not need an API route or an adapter for a contact form. The form can be one .astro component that posts to Formlander, a form backend that you run on your own server. A plain POST needs no client JavaScript.

  1. A Formlander server. See Deployment.
  2. A form in the Formlander dashboard with the address contact. Put the domain of your Astro site in its Allowed sites. Add localhost if you want to test with astro dev.
  3. The Endpoint of the form. Copy it from the page of the form in the dashboard. It contains the token of the form, which is public.
src/components/ContactForm.astro
---
const action = 'https://forms.example.com/forms/contact/submit?token=YOUR_TOKEN';
const successUrl = 'https://example.com/thanks/';
---
<form id="contact-form" action={action} method="post">
<input type="hidden" name="_success_url" value={successUrl}>
<input type="text" name="__fl_hp" value="" tabindex="-1" autocomplete="off" aria-hidden="true" style="position:absolute;left:-9999px">
<label for="name">Name</label>
<input type="text" id="name" name="name" autocomplete="name" required>
<label for="email">Email</label>
<input type="email" id="email" name="email" autocomplete="email" required>
<label for="message">Message</label>
<textarea id="message" name="message" rows="5" required></textarea>
<button type="submit">Send</button>
</form>

Set the two constants at the top. action is the Endpoint of your form. successUrl is the full URL of a page on your site. A local test also ends on this URL, so publish the thank-you page first.

Import the component in a page:

src/pages/contact.astro
---
import Layout from '../layouts/Layout.astro';
import ContactForm from '../components/ContactForm.astro';
---
<Layout>
<h1>Contact</h1>
<ContactForm />
</Layout>

Use your own layout, and add src/pages/thanks.astro for the visitor to land on. You can import the component in an MDX page too. A plain .md page cannot import components.

The browser posts the fields to your Formlander server. Formlander checks the token and Allowed sites, stores the fields as JSON in its SQLite file, and redirects the browser to successUrl. It does not store _success_url or __fl_hp.

Formlander rejects the submission if successUrl is on a site that is not in Allowed sites.

The submission is in the dashboard. Formlander also sends it by email or to a webhook if you set that up for the form.

If you want the result on the same page, add this to the end of ContactForm.astro:

src/components/ContactForm.astro
<p id="contact-status" role="status"></p>
<script>
const form = document.querySelector<HTMLFormElement>('#contact-form')!;
const status = document.querySelector<HTMLElement>('#contact-status')!;
const button = form.querySelector<HTMLButtonElement>('button[type="submit"]')!;
form.addEventListener('submit', async (event) => {
event.preventDefault();
const data = new FormData(form);
data.delete('_success_url');
button.disabled = true;
let sent = false;
try {
const response = await fetch(form.action, { method: 'POST', body: data });
const result = await response.json();
sent = result.ok === true;
} catch {
// Network error. The message below tells the visitor.
}
button.disabled = false;
status.textContent = sent ? 'Thanks, your message was sent.' : 'Your message was not sent. Please try again.';
if (sent) form.reset();
});
</script>

Formlander answers fetch with JSON. ok is true when it stored the submission. The script deletes _success_url because fetch follows the redirect to your thank-you page and does not get the JSON. Without JavaScript, the form still works as a plain POST.

The __fl_hp input is a honeypot. Spam and security explains it, Allowed sites, the rate limit, and Turnstile.

With Turnstile and the fetch script, call turnstile.reset() after each send. A Turnstile token works once.

Form integration has the response format, file uploads, and a React example.