Skip to content
Lazyform
Docs menu

Frameworks and builders

SvelteKit

Add a Lazyform contact form to SvelteKit 2, posting from the browser or through a form action with use:enhance.

SvelteKit gives you two good options. Post straight from the browser to Lazyform (works on static and serverless hosts alike), or use a form action that posts from your server and keeps working without JavaScript.

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

src/routes/contact/+page.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>

From the browser or through a form action

svelte
<!-- src/routes/contact/+page.svelte -->
<script>
  import { PUBLIC_LAZYFORM_KEY } from '$env/static/public';

  let status = $state('idle');
  let error = $state('');

  async function handleSubmit(event) {
    event.preventDefault();
    const form = event.currentTarget;
    status = 'sending';
    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';
      } 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={PUBLIC_LAZYFORM_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}

Tips

  • Environment variables. Put PUBLIC_LAZYFORM_KEY (browser) or LAZYFORM_KEY (form action) in .env.
  • Which one to pick. The browser version works with adapter-static and keeps allowed websites and per-visitor rate limiting working as designed. The form action posts from your server, which sends no Origin from your site, so don't combine it with allowed websites.
  • Progressive enhancement. use:enhance submits without a page reload when JavaScript is on, and falls back to a normal post when it isn't.

Where to go next