Skip to content
Lazyform
Docs menu

Frameworks and builders

Svelte

Build a Svelte 5 contact form with runes that posts to Lazyform, with loading, success and error states.

This guide shows a Svelte 5 component that sends submissions to Lazyform using runes. It works in any Svelte app built with Vite. If you use SvelteKit, see the SvelteKit guide for a form action variant.

Before you start

Get an access key: type your email on the homepage for an instant key, or sign in and create a form. Replace YOUR_ACCESS_KEY with your key (it starts with lf_).

The minimal form

ContactForm.svelte
<form action="https://lazyform-api.xvelopers.com/submit" method="POST">
  <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" tabindex="-1" autocomplete="off" />
  <button type="submit">Send</button>
</form>

With fetch and inline messages

ContactForm.svelte
<script>
  let status = $state('idle'); // idle | sending | success | error
  let error = $state('');

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

    try {
      const response = await fetch('https://lazyform-api.xvelopers.com/submit', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
        body: JSON.stringify(Object.fromEntries(new FormData(form))),
      });
      const result = await response.json();

      if (result.success) {
        status = 'success';
        form.reset();
      } else {
        status = 'error';
        error = result.error?.message ?? 'Something went wrong.';
      }
    } catch {
      status = 'error';
      error = 'Network error. Please try again.';
    }
  }
</script>

{#if status === 'success'}
  <p>Thanks! Your message has been sent.</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" tabindex="-1" autocomplete="off" />
    <button type="submit" disabled={status === 'sending'}>
      {status === 'sending' ? 'Sending...' : 'Send'}
    </button>
    {#if status === 'error'}
      <p role="alert">{error}</p>
    {/if}
  </form>
{/if}

Svelte 5 uses plain onsubmit attributes instead of on:submit|preventDefault, so call event.preventDefault() yourself.

Tips

  • Environment variables. In Vite, use import.meta.env.VITE_LAZYFORM_KEY. The key is safe to be public; it only allows submitting to your form.
  • Files. Send new FormData(form) as the body with only the Accept header. See File uploads.
  • Lock it down. Turn on allowed websites in the form's settings so only your domain can submit.

Where to go next