Skip to content
Lazyform

Contact form for SvelteKit

Add a contact form to SvelteKit that works on prerendered pages and with JavaScript off. No form actions or server code, and messages reach email or Slack.

Free plan: 250 submissions a month, forever.

Why a form backend for SvelteKit

SvelteKit's form actions are great when you have a server. Many SvelteKit sites don't: they're prerendered with adapter-static and served from a CDN, where actions can't run. Even with a server, an action still needs an email provider, secrets and spam checks.

Posting to Lazyform sidesteps all of that. The form below works as a plain HTML form when JavaScript is off, and upgrades itself to an inline message when it's on.

A working contact form

Replace YOUR_ACCESS_KEY with your key from /login, and the redirect URL with your own domain.

src/routes/contact/+page.svelte
<script>
  let status = $state('idle')
  let errorMessage = $state('')

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

    const res = await fetch(form.action, {
      method: 'POST',
      headers: { Accept: 'application/json' },
      body: new FormData(form),
    })
    const data = await res.json()
    status = data.success ? 'sent' : 'error'
    if (!data.success) errorMessage = data.error.message
  }
</script>

<svelte:head><title>Contact</title></svelte:head>

{#if status === 'sent'}
  <p>Thanks! We'll reply soon.</p>
{:else}
  <form action="https://lazyform-api.xvelopers.com/submit" method="POST" onsubmit={enhance}>
    <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
    <input type="hidden" name="redirect" value="https://example.com/thanks" />
    <input name="name" placeholder="Name" required />
    <input name="email" type="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'}>Send</button>
    {#if status === 'error'}<p role="alert">{errorMessage}</p>{/if}
  </form>
{/if}
src/routes/thanks/+page.svelte
<h1>Thanks for your message</h1>
<p>We'll get back to you soon. <a href="/">Back to the home page</a></p>

To prerender both pages, add export const prerender = true to src/routes/+layout.js. The redirect field is only used when the form posts without JavaScript.

Get submissions where you want them

Messages arrive by email. Add Telegram, Slack, Discord or WhatsApp alerts, or copy each submission to Google Sheets, Notion or Airtable. See all integrations.

Full guide

The SvelteKit guide covers environment variables, file uploads, captcha and showing field-level errors.

Questions

Why not use a SvelteKit form action?

Form actions need a server, so they don't run on prerendered pages or with adapter-static. Posting to Lazyform works on any adapter, and you don't have to keep email credentials on your server.

Can I use use:enhance with this form?

No. use:enhance only works with your own SvelteKit actions. The example uses its own onsubmit handler instead, which gives you the same no-reload experience.

Does it work with adapter-static?

Yes. The page can be fully prerendered, because the form talks to Lazyform directly from the browser.

Where should I keep the access key?

Inline it, or use a PUBLIC_ variable from $env/static/public. The key is public by design: it can send submissions but never read them.