Add a Contact Form to SvelteKit

Add a SvelteKit contact form that submits to W3Forms from the browser, no +server.ts required. Free, with email notifications and spam protection.

  1. Create a free W3Forms account and a form. Copy the access key shown once.
  2. Optional: in Form → Settings, list your site under Allowed domains, so posts from other sites are marked as spam. Include the port when testing locally (for example localhost:5500).
  3. Paste the markup below. Replace YOUR_ACCESS_KEY with your key.
  4. Submit a test. Check the dashboard inbox and your notification email.

No +server.ts needed for the basic path — the browser posts to W3Forms. Use PUBLIC_W3FORMS_ACCESS_KEY from $env/static/public instead of a hardcoded key.

+page.svelte
<script>
  let status = $state("");

  async function handleSubmit(e) {
    e.preventDefault();
    status = "Sending…";
    const form = e.currentTarget;
    const res = await fetch("https://api.w3forms.com/submit", {
      method: "POST",
      headers: { "Content-Type": "application/json", Accept: "application/json" },
      body: JSON.stringify(Object.fromEntries(new FormData(form))),
    });
    const data = await res.json();
    if (res.ok && data.success) {
      status = "Thanks — your message was sent.";
      form.reset();
    } else {
      status = data.message || data.error || "Something went wrong.";
    }
  }
</script>

<form onsubmit={handleSubmit}>
  <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
  <label for="name">Name</label>
  <input id="name" name="name" required />
  <label for="email">Email</label>
  <input id="email" type="email" name="email" required />
  <label for="message">Message</label>
  <textarea id="message" name="message" required></textarea>
  <input type="text" name="_gotcha" tabindex="-1" autocomplete="off"
         aria-hidden="true" style="position:absolute;left:-9999px" />
  <button type="submit">Send</button>
  <p role="status">{status}</p>
</form>

Next steps

Other stacks