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
- Create a form and copy its form ID, or let your AI tool create one without an account.
- Save the component as
src/lib/ContactForm.svelteand replaceYOUR_FORM_ID. - Use it in a route: in
src/routes/contact/+page.svelte,import ContactForm from "$lib/ContactForm.svelte";then<ContactForm />. - 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, oron:submit|preventDefaultin Svelte 4. empty_submission. Inputs neednameattributes;bind:valuealone doesn't add them.- Strict CSP. Add
connect-src https://wrenform.com.
Next steps
- My form isn't working: symptom, cause and fix.
- Spam: the timing field and Turnstile.
- Redirects: a plain form with a thank-you page, no JavaScript.
- Errors: every code and its fix.