# 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-14. Web version: https://wrenform.com/docs/frameworks/sveltekit

```svelte
<!-- 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](/sign-in) and copy its form ID, or let your AI tool create one [without an account](/docs/quick-forms).
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

- [My form isn't working](/docs/troubleshooting): symptom, cause and fix.
- [Spam](/docs/spam): the timing field and Turnstile.
- [Redirects](/docs/redirects): a plain form with a thank-you page, no JavaScript.
- [Errors](/docs/errors): every code and its fix.
