Contact form for Next.js
Add a contact form to a Next.js App Router site without writing an API route or server action. Submissions go to email, Telegram, Slack and more.
Why a form backend for Next.js
Next.js can run server code, so you could write a route handler that sends email. Then you need an email provider, secret keys, spam protection, and somewhere to look when a message goes missing. With static export there's no server at all.
With Lazyform, a small client component posts to one URL. You get spam filtering, a submissions inbox and delivery to email or chat apps without any server code in your project.
A working contact form
Save this component next to your page. Replace YOUR_ACCESS_KEY with your key from /login.
'use client'
import { useState, type FormEvent } from 'react'
type Result =
| { success: true; id: string }
| { success: false; error: { code: string; message: string } }
export function ContactForm() {
const [state, setState] = useState<'idle' | 'sending' | 'sent'>('idle')
const [error, setError] = useState<string | null>(null)
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
setState('sending')
setError(null)
const res = await fetch('https://lazyform-api.xvelopers.com/submit', {
method: 'POST',
headers: { Accept: 'application/json' },
body: new FormData(event.currentTarget),
})
const result: Result = await res.json()
if (result.success) {
setState('sent')
} else {
setState('idle')
setError(result.error.message)
}
}
if (state === 'sent') return <p>Thanks, your message is on its way.</p>
return (
<form onSubmit={onSubmit}>
<input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />
<input name="name" placeholder="Name" required />
<input name="email" type="email" placeholder="Email" required />
<textarea name="message" placeholder="How can we help?" rows={5} required />
<input type="checkbox" name="botcheck" className="hidden" style={{ display: 'none' }} />
<button type="submit" disabled={state === 'sending'}>
{state === 'sending' ? 'Sending…' : 'Send message'}
</button>
{error && <p role="alert">{error}</p>}
</form>
)
}import { ContactForm } from './contact-form'
export const metadata = { title: 'Contact' }
export default function ContactPage() {
return <ContactForm />
}The page stays a server component, so its metadata and layout render on the server. Only the form ships JavaScript to the browser.
Get submissions where you want them
Each submission is emailed to you. Add Telegram, Slack, Discord or WhatsApp alerts, or send rows to Google Sheets and records to Notion or Airtable. See all integrations.
Full guide
The Next.js guide covers the Pages Router, environment variables, captcha and file uploads.
Questions
Do I need an API route or a server action?
No. The form is a client component that posts straight to Lazyform from the browser. That also means it works with static export (output: 'export').
Does it work with the Pages Router?
Yes. Drop the 'use client' line and use the same component inside any page in the pages folder.
Can I deploy somewhere other than Vercel?
Yes. Nothing in the form depends on the host, so it works on Vercel, Netlify, Cloudflare, a Docker container or a static export.
Can I keep the key in an environment variable?
Yes. Use a NEXT_PUBLIC_ variable, such as NEXT_PUBLIC_LAZYFORM_KEY, because the browser needs it. The key can only send submissions, never read them.