Skip to content
Wrenform

Docs / Frameworks

Contact form for SvelteKit

A Svelte 5 component that posts with fetch and shows the result inline. No form action or server endpoint.

Last updated 2026-11-14View as Markdown
<!-- ContactForm.svelte (Svelte 5 / SvelteKit) -->
<script>
  let status = $state("idle");

  async function onsubmit(e) {
    e.preventDefault();
    const form = e.currentTarget;
    status = "sending";
    try {
      const res = await fetch("https://wrenform.com/f/YOUR_FORM_ID", {
        method: "POST",
        headers: { Accept: "application/json" },
        body: new FormData(form),
      });
      status = res.ok ? "sent" : "error";
      if (res.ok) form.reset();
    } catch {
      status = "error";
    }
  }
</script>

<form {onsubmit}>
  <label>Name <input type="text" name="name" autocomplete="name" required /></label>
  <label>Email <input type="email" name="email" autocomplete="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>
  <input type="text" name="_honey" style="display:none" tabindex="-1" autocomplete="off" />
  <button type="submit" disabled={status === "sending"}>Send</button>
  <p role="status">
    {#if status === "sent"}Thanks, your message was sent.{/if}
    {#if status === "error"}Something went wrong. Please try again.{/if}
  </p>
</form>

Steps

  1. Create a form and copy its form ID, or let your AI tool create one without an account.
  2. Save the component as src/lib/ContactForm.svelte and replace YOUR_FORM_ID.
  3. Use it in a route: in src/routes/contact/+page.svelte, import ContactForm from "$lib/ContactForm.svelte"; then <ContactForm />.
  4. Deploy. Then add your site's origin under Allowed origins in the form's settings.

No form action or +server.js needed. The browser posts straight to Wrenform, so the page can be prerendered. The form ID is public by design. To keep it in configuration, use PUBLIC_WRENFORM_ID from $env/static/public.

The snippet uses Svelte 5 syntax ($state, onsubmit). For Svelte 4, use let status = "idle" and on:submit|preventDefault.

Test it

Run npm run dev, open /contact and send the form. You should see the thank-you line, the submission in your dashboard and an email. While testing, leave Allowed origins empty or add http://localhost:5173.

Common problems

  • CORS error. The page's origin isn't in the form's allowed origins.
  • The page reloads instead. The submit handler isn't attached: check {onsubmit} on the form, or on:submit|preventDefault in Svelte 4.
  • empty_submission. Inputs need name attributes; bind:value alone doesn't add them.
  • Strict CSP. Add connect-src https://wrenform.com.

Next steps

Something wrong or unclear on this page? Report a problem: write to hello [at] wrenform.com with the page address (/docs/frameworks/sveltekit).