Home / Gidsen / Integratie van Astro-contactformulieren (statisch en SSR)
INTEGRATIEGIDS · ASTRO & JAMSTACK

Integratie van Astro-contactformulieren (statisch en SSR)

Hoe u contactformulieren kunt toevoegen aan Astro statische sites (SSG) en hybride serverroutes met behulp van Lugatu-eindpunten zonder e-mailservers of databases te onderhouden.

1. Inname van Pure Static Site (SSG)-formulieren

Astro staat standaard bekend om zijn nul-JavaScript. Met Lugatu kunt u uw Astro-site 100% statisch houden terwijl u vragen verzamelt met standaard HTML-formulieren en lichtgewicht ophalen aan de clientzijde.

src/pages/contact.astro astro
---
// src/pages/contact.astro
---
<form id="astro-contact-form" class="space-y-4 max-w-md">
  <div>
    <label class="block text-sm font-semibold mb-1">Your Name</label>
    <input type="text" name="name" required class="w-full border rounded-xl p-2.5" />
  </div>
  <div>
    <label class="block text-sm font-semibold mb-1">Email Address</label>
    <input type="email" name="email" required class="w-full border rounded-xl p-2.5" />
  </div>
  <div>
    <label class="block text-sm font-semibold mb-1">Message</label>
    <textarea name="message" rows="4" required class="w-full border rounded-xl p-2.5"></textarea>
  </div>

  <input type="text" name="_gotcha" style="display:none !important" tabindex="-1" autocomplete="off" />

  <button type="submit" class="btn-primary-editorial py-2.5 px-6">Send Message</button>
</form>

<script>
  const form = document.getElementById('astro-contact-form') as HTMLFormElement;
  form?.addEventListener('submit', async (e) => {
    e.preventDefault();
    const formData = new FormData(form);

    const payload = {
      fields: {
        name: formData.get('name'),
        email: formData.get('email'),
        message: formData.get('message'),
      },
      honeypot: formData.get('_gotcha') || '',
    };

    const res = await fetch('https://www.lugatu.com/api/v1/public/forms/YOUR_PUBLIC_FORM_ID/submissions', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
      body: JSON.stringify(payload),
    });

    if (res.ok) {
      alert('Thank you! Message delivered.');
      form.reset();
    }
  });
</script>

2. Astro Hybrid / SSR API-route met server-API-sleutel

Als uw Astro-site in hybride of SSR-modus draait op Cloudflare Pages, Netlify of Vercel, kunt u inzendingen veilig doorsturen via een Astro API-route.

src/pages/api/contact.ts typescript
import type { APIRoute } from 'astro';

export const POST: APIRoute = async ({ request }) => {
  const data = await request.formData();
  const fields = {
    name: data.get('name')?.toString(),
    email: data.get('email')?.toString(),
    message: data.get('message')?.toString(),
  };

  const res = await fetch(`https://www.lugatu.com/api/v1/forms/${import.meta.env.LUGATU_FORM_ID}/submissions`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${import.meta.env.LUGATU_API_KEY}`,
      'Content-Type': 'application/json',
      'Idempotency-Key': crypto.randomUUID(),
    },
    body: JSON.stringify({ fields }),
  });

  return new Response(JSON.stringify({ success: res.ok }), {
    status: res.status,
    headers: { 'Content-Type': 'application/json' },
  });
};

Beveiliging en authenticatie invariant

Beveiligingsinstructie: Stel import.meta.env.LUGATU_API_KEY nooit bloot in Astro-scripttags aan de clientzijde. Clientscripts mogen alleen het openbare eindpunt gebruiken.

Problemen oplossen en veel voorkomende inslikfouten

Form werkt lokaal, maar faalt bij de productie van Cloudflare / Netlify-build

Zorg ervoor dat de variabelen van uw productieomgeving (LUGATU_FORM_ID en LUGATU_API_KEY) zijn geconfigureerd in uw hostingdashboard.

Veelgestelde vragen

Kan ik mijn Astro-site 100% statisch houden met Lugatu?

Ja. Zuiver statische (SSG) sites worden rechtstreeks verzonden naar het openbare Lugatu-eindpunt zonder dat daarvoor enige servercomputers van uw hostingprovider nodig zijn.

Klaar om uw formuliereindpunt te maken?

Ga binnen enkele seconden gratis aan de slag. Geen creditcard vereist.

Begin gratis →