Skip to content
Lazyform
Docs menu

Frameworks and builders

Astro

Add a Lazyform contact form to an Astro 5 site with a plain HTML form and an optional script for inline messages.

Astro ships HTML by default, which is exactly what Lazyform wants. A plain form works with zero JavaScript, and a small <script> adds inline success and error messages if you want them.

Before you start

Get an access key: type your email on the homepage for an instant key, or sign in and create a form. Replace YOUR_ACCESS_KEY with your key (it starts with lf_).

The minimal form

src/components/ContactForm.astro
---
const accessKey = import.meta.env.PUBLIC_LAZYFORM_KEY ?? 'YOUR_ACCESS_KEY';
---

<form action="https://lazyform-api.xvelopers.com/submit" method="POST">
  <input type="hidden" name="access_key" value={accessKey} />
  <input type="hidden" name="redirect" value="https://example.com/thanks" />

  <label>Name <input type="text" name="name" required /></label>
  <label>Email <input type="email" name="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>

  <input type="checkbox" name="botcheck" class="hidden" style="display:none" />

  <button type="submit">Send</button>
</form>

Use it in any page with <ContactForm />. Remove the redirect field to use Lazyform's thank-you page, or point it at your own page. See Redirect after submit.

Add inline messages with a script

Astro processes and bundles <script> tags in components, so this runs once per page and works with multiple forms.

src/components/ContactForm.astro
---
const accessKey = import.meta.env.PUBLIC_LAZYFORM_KEY ?? 'YOUR_ACCESS_KEY';
---

<form class="lazyform" action="https://lazyform-api.xvelopers.com/submit" method="POST">
  <input type="hidden" name="access_key" value={accessKey} />
  <input type="text" name="name" placeholder="Name" required />
  <input type="email" name="email" placeholder="Email" required />
  <textarea name="message" placeholder="Message" required></textarea>
  <input type="checkbox" name="botcheck" class="hidden" style="display:none" />
  <button type="submit">Send</button>
  <p class="status" role="status"></p>
</form>

<script>
  document.querySelectorAll<HTMLFormElement>('form.lazyform').forEach((form) => {
    const status = form.querySelector<HTMLElement>('.status')!;
    const button = form.querySelector<HTMLButtonElement>('button[type="submit"]')!;

    form.addEventListener('submit', async (event) => {
      event.preventDefault();
      button.disabled = true;
      status.textContent = 'Sending...';

      try {
        const response = await fetch(form.action, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
          body: JSON.stringify(Object.fromEntries(new FormData(form))),
        });
        const result = await response.json();
        if (result.success) {
          status.textContent = 'Thanks! Your message has been sent.';
          form.reset();
        } else {
          status.textContent = result.error?.message ?? 'Something went wrong.';
        }
      } catch {
        status.textContent = 'Network error. Please try again.';
      } finally {
        button.disabled = false;
      }
    });
  });
</script>

Tips

  • Static or server output. Both examples work with output: 'static', since the browser posts straight to Lazyform. You don't need an Astro action or API endpoint.
  • Environment variables. Put PUBLIC_LAZYFORM_KEY=lf_... in .env. The PUBLIC_ prefix makes it available at build time in components.
  • View transitions. If you use Astro's <ClientRouter />, wrap the script body in a document.addEventListener('astro:page-load', ...) handler so it reattaches after navigation.
  • UI frameworks. If your form lives in a React, Vue or Svelte island, follow that framework's guide instead.

Where to go next