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
<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
<!-- 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}// src/routes/contact/+page.server.ts
import { fail } from '@sveltejs/kit';
import { LAZYFORM_KEY } from '$env/static/private';
import type { Actions } from './$types';
export const actions = {
default: async ({ request, fetch }) => {
const data = await request.formData();
if (data.get('botcheck')) return { success: true }; // honeypot ticked
const response = await fetch('https://lazyform-api.xvelopers.com/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({
access_key: LAZYFORM_KEY,
name: data.get('name'),
email: data.get('email'),
message: data.get('message'),
}),
});
const result = await response.json();
if (!result.success) return fail(response.status, { error: result.error?.message ?? 'Something went wrong.' });
return { success: true };
},
} satisfies Actions;
// src/routes/contact/+page.svelte
// <script>
// import { enhance } from '$app/forms';
// let { form } = $props();
// </script>
//
// {#if form?.success}
// <p>Thanks! Your message has been sent.</p>
// {:else}
// <form method="POST" use:enhance>
// <input type="text" name="name" required />
// <input type="email" name="email" required />
// <textarea name="message" required></textarea>
// <input type="checkbox" name="botcheck" class="hidden" style="display:none" tabindex="-1" />
// <button type="submit">Send</button>
// {#if form?.error}<p role="alert">{form.error}</p>{/if}
// </form>
// {/if}Tips
- Environment variables. Put
PUBLIC_LAZYFORM_KEY(browser) orLAZYFORM_KEY(form action) in.env. - Which one to pick. The browser version works with
adapter-staticand keeps allowed websites and per-visitor rate limiting working as designed. The form action posts from your server, which sends noOriginfrom your site, so don't combine it with allowed websites. - Progressive enhancement.
use:enhancesubmits without a page reload when JavaScript is on, and falls back to a normal post when it isn't.
Where to go next
- Svelte for a plain Svelte component
- Responses for every error code
- Spam protection
- Get submissions in Slack