Skip to content
Lazyform

Contact form for Svelte

A small Svelte 5 contact form built with runes and fetch. Send every message to your email, Telegram or Slack without writing or hosting a backend.

Free plan: 250 submissions a month, forever.

Why a form backend for Svelte

Svelte compiles to small, fast JavaScript, and a Svelte app built with Vite is just static files. A contact form needs somewhere to send its data, and writing and hosting an API for one form is a lot of extra weight for a small project.

Lazyform keeps it small: one fetch to one URL. We handle spam, storage and delivery.

A working contact form

Replace YOUR_ACCESS_KEY with your key from /login.

src/lib/ContactForm.svelte
<script>
  let status = $state('idle')
  let errorMessage = $state('')

  async function handleSubmit(event) {
    event.preventDefault()
    const form = event.currentTarget
    status = 'sending'

    const res = await fetch('https://lazyform-api.xvelopers.com/submit', {
      method: 'POST',
      headers: { Accept: 'application/json' },
      body: new FormData(form),
    })
    const data = await res.json()

    if (data.success) {
      status = 'sent'
    } else {
      status = 'error'
      errorMessage = data.error.message
    }
  }
</script>

{#if status === 'sent'}
  <p>Thanks! Your message is on its way.</p>
{:else}
  <form onsubmit={handleSubmit}>
    <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
    <input type="text" name="name" placeholder="Name" required />
    <input type="email" name="email" placeholder="Email" required />
    <textarea name="message" placeholder="Message" required></textarea>
    <input type="checkbox" name="botcheck" class="hidden" style="display: none" />
    <button disabled={status === 'sending'}>
      {status === 'sending' ? 'Sending…' : 'Send'}
    </button>
    {#if status === 'error'}<p role="alert">{errorMessage}</p>{/if}
  </form>
{/if}

The form element is saved before the await, because event.currentTarget is cleared once the event has finished.

Get submissions where you want them

Each submission arrives by email. You can add Telegram, Slack, Discord or WhatsApp alerts, or send submissions to Google Sheets, Notion or Airtable. See all integrations.

Full guide

The Svelte guide covers Svelte 4 syntax, JSON bodies, file uploads and captcha.

Questions

Is this Svelte 5 or Svelte 4 syntax?

Svelte 5, using $state and the onsubmit attribute. In Svelte 4, use plain let variables and on:submit|preventDefault instead; the fetch call stays the same.

I'm using SvelteKit. Is there a different guide?

Yes. This component works in SvelteKit too, but the SvelteKit page shows a version that also works with JavaScript turned off and with prerendered pages.

Can I bind inputs to variables instead?

Yes. Bind each input to a $state value and send them as a JSON body. Include access_key and an empty botcheck in the JSON.