Skip to content
Lazyform
Docs menu

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.

app/contact/page.tsx
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

tsx
// 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>
  );
}

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_KEY for the client component, LAZYFORM_KEY for the server action.
  • Server-side requests. A server action posts from your server, not the visitor's browser, so it sends no Origin or Referer from 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 the Accept header. See File uploads.

Where to go next