Frameworks and builders
Nuxt
Add a Lazyform contact form to a Nuxt 3 or 4 app, posting from the browser or through a server route.
Nuxt can post to Lazyform straight from the browser, so you don't need a server route. If you'd rather keep the key on the server, a small server route works too. The examples work in Nuxt 3 and Nuxt 4.
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
<template>
<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>
</template>In Nuxt 4 the default source folder is app/, so put components in app/components/.
From the browser or through a server route
<!-- components/ContactForm.vue -->
<script setup lang="ts">
const config = useRuntimeConfig();
const fields = reactive({ name: '', email: '', message: '', botcheck: false });
const status = ref<'idle' | 'sending' | 'success' | 'error'>('idle');
const error = ref('');
async function submit() {
status.value = 'sending';
try {
await $fetch('https://lazyform-api.xvelopers.com/submit', {
method: 'POST',
headers: { Accept: 'application/json' },
body: { access_key: config.public.lazyformKey, ...fields, botcheck: fields.botcheck ? 'on' : '' },
});
status.value = 'success';
} catch (err: any) {
status.value = 'error';
error.value = err?.data?.error?.message ?? 'Something went wrong.';
}
}
</script>
<template>
<p v-if="status === 'success'">Thanks! Your message has been sent.</p>
<form v-else @submit.prevent="submit">
<input v-model="fields.name" type="text" placeholder="Name" required />
<input v-model="fields.email" type="email" placeholder="Email" required />
<textarea v-model="fields.message" placeholder="Message" required></textarea>
<input v-model="fields.botcheck" type="checkbox" class="hidden" style="display: none" tabindex="-1" autocomplete="off" />
<button type="submit" :disabled="status === 'sending'">{{ status === 'sending' ? 'Sending...' : 'Send' }}</button>
<p v-if="status === 'error'" role="alert">{{ error }}</p>
</form>
</template>// server/api/contact.post.ts
export default defineEventHandler(async (event) => {
const body = await readBody(event);
// Honeypot ticked: pretend it worked, send nothing.
if (body.botcheck) return { success: true };
const config = useRuntimeConfig(event);
try {
return await $fetch('https://lazyform-api.xvelopers.com/submit', {
method: 'POST',
headers: { Accept: 'application/json' },
body: {
access_key: config.lazyformKey,
name: body.name,
email: body.email,
message: body.message,
},
});
} catch (err: any) {
throw createError({
statusCode: err?.statusCode ?? 500,
statusMessage: err?.data?.error?.message ?? 'Could not send the message.',
});
}
});
// In the component, post to your own route instead:
// await $fetch('/api/contact', { method: 'POST', body: { ...fields } });
// and read the error from err.statusMessage.$fetch sends objects as JSON and throws on error statuses, so the Lazyform error object is on err.data.error.
Tips
- Runtime config. Add the key to
nuxt.config.ts:runtimeConfig: { lazyformKey: '', public: { lazyformKey: '' } }, then setNUXT_LAZYFORM_KEYorNUXT_PUBLIC_LAZYFORM_KEYin.env. - Which one to pick. The browser version keeps allowed websites and per-visitor rate limiting working as designed. A server route posts from your server, which sends no
Originfrom your site, so don't combine it with allowed websites. - Static hosting. With
nuxt generatethere is no server, so use the browser version.
Where to go next
- Vue for the plain Vue component
- Responses for every error code
- Spam protection
- Send submissions to a webhook