Frameworks and builders
Next.js
Add a Lazyform contact form to a Next.js App Router site, as a client component with fetch or as a server action.
Lazyform works with Next.js without an API route. You can post from the browser with a client component, or post from the server with a server action. Both examples use the App Router.
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 below with your key (it starts with lf_).
The minimal form
A plain form in a server component already works. The browser posts it and shows a thank-you page.
export default function ContactPage() {
return (
<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 />
<input type="checkbox" name="botcheck" className="hidden" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
<button type="submit">Send</button>
</form>
);
}Client component or server action
// app/contact/contact-form.tsx
'use client';
import { useState, type FormEvent } from 'react';
type Status = 'idle' | 'sending' | 'success' | 'error';
export function ContactForm() {
const [status, setStatus] = useState<Status>('idle');
const [error, setError] = useState('');
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = event.currentTarget;
setStatus('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) {
setStatus('success');
form.reset();
} else {
setStatus('error');
setError(result.error?.message ?? 'Something went wrong.');
}
} catch {
setStatus('error');
setError('Network error. Please try again.');
}
}
if (status === 'success') return <p>Thanks! Your message has been sent.</p>;
return (
<form onSubmit={handleSubmit}>
<input type="hidden" name="access_key" value={process.env.NEXT_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 />
<input type="checkbox" name="botcheck" className="hidden" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
<button type="submit" disabled={status === 'sending'}>
{status === 'sending' ? 'Sending...' : 'Send'}
</button>
{status === 'error' && <p role="alert">{error}</p>}
</form>
);
}// app/contact/actions.ts
'use server';
export type ContactState = { ok: boolean; message: string } | null;
export async function sendContact(_prev: ContactState, formData: FormData): Promise<ContactState> {
// Bots that tick the honeypot get a fake success and nothing is sent.
if (formData.get('botcheck')) return { ok: true, message: 'Thanks! Your message has been sent.' };
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: process.env.LAZYFORM_KEY,
name: formData.get('name'),
email: formData.get('email'),
message: formData.get('message'),
}),
});
const result = await response.json();
return result.success
? { ok: true, message: 'Thanks! Your message has been sent.' }
: { ok: false, message: result.error?.message ?? 'Something went wrong.' };
}
// app/contact/contact-form.tsx
'use client';
import { useActionState } from 'react';
import { sendContact } from './actions';
export function ContactForm() {
const [state, formAction, pending] = useActionState(sendContact, null);
return (
<form action={formAction}>
<input type="text" name="name" placeholder="Name" required />
<input type="email" name="email" placeholder="Email" required />
<textarea name="message" placeholder="Message" required />
<input type="checkbox" name="botcheck" className="hidden" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
<button type="submit" disabled={pending}>{pending ? 'Sending...' : 'Send'}</button>
{state && <p role={state.ok ? 'status' : 'alert'}>{state.message}</p>}
</form>
);
}Render either one from app/contact/page.tsx with <ContactForm />.
Tips
- Which one to pick. The client component is the simplest and keeps per-visitor rate limiting and allowed websites working as designed. The server action keeps the key out of the page and works without JavaScript.
- Environment variables. Put the key in
.env.local:NEXT_PUBLIC_LAZYFORM_KEYfor the client component,LAZYFORM_KEYfor the server action. - Server-side requests. A server action posts from your server, not the visitor's browser, so it sends no
OriginorRefererfrom your site. If you turn on allowed websites for this form, use the client component instead. - Files. Send
new FormData(form)as the body with only theAcceptheader. See File uploads.
Where to go next
- AJAX and JSON and Responses
- Spam protection for captchas and allowed websites
- Get submissions in Slack or Telegram