Skip to content
Lazyform
Docs menu

Frameworks and builders

Gatsby

Add a reusable Lazyform contact form component to a Gatsby site, with a plain post or fetch with inline messages.

Gatsby builds static pages, so there's no server to handle a form. Lazyform fills that gap: make one contact form component and drop it into any page.

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

src/components/contact-form.jsx
import * as React from 'react';

export default function ContactForm() {
  return (
    <form action="https://lazyform-api.xvelopers.com/submit" method="POST">
      <input type="hidden" name="access_key" value={process.env.GATSBY_LAZYFORM_KEY} />
      <input type="hidden" name="redirect" value="https://example.com/thanks/" />
      <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>
  );
}

Put GATSBY_LAZYFORM_KEY=YOUR_ACCESS_KEY in .env.production and .env.development. Gatsby only exposes variables starting with GATSBY_ to the browser. Then use it in a page:

src/pages/contact.jsx
import * as React from 'react';
import ContactForm from '../components/contact-form';

export default function ContactPage() {
  return (
    <main>
      <h1>Contact us</h1>
      <ContactForm />
    </main>
  );
}

The redirect field sends visitors to your own /thanks/ page after submitting. Remove it to use Lazyform's thank-you page.

With fetch and inline messages

src/components/contact-form.jsx
import * as React from 'react';

export default function ContactForm() {
  const [status, setStatus] = React.useState('idle');
  const [error, setError] = React.useState('');

  async function handleSubmit(event) {
    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');
      } 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.GATSBY_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>
  );
}

Tips

  • No Gatsby Functions needed. The browser posts straight to Lazyform, so this works on any static host.
  • The key is safe to be public. It only allows submitting to your form. Turn on allowed websites in the form's settings to lock it to your domain.
  • Files. Send new FormData(form) as the body with only the Accept header. See File uploads.

Where to go next