Gatsby Contact Form: A React Component With No Backend Code

Add a working contact form to a Gatsby site with a small React component, an environment variable for the key and instant notifications. Works on Netlify, Vercel, Gatsby Cloud successors and any static host.

· 3 min read

A Gatsby React contact form component sending submissions to an inbox

Gatsby builds your site into static files, which is why it is fast and cheap to host. It also means there is no server to receive a form. You can write a serverless function for each host, or point the form at a form backend and keep your site fully static.

This guide does the second: one React component that works on every host.

1. Add your key as an environment variable

Create a free Formiz account and copy your access key. In Gatsby, browser-visible variables must start with GATSBY_. Create .env.production (and .env.development for local testing):

bash
GATSBY_FORMIZ_KEY=fmz_your_key_here

The key is public by design; it can only send submissions to you. See public API keys in frontend code. Add the same variable in your hosting dashboard so production builds pick it up.

2. Create the component

src/components/ContactForm.jsx:

jsx
import React, { useState } from 'react';

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

  async function onSubmit(e) {
    e.preventDefault();
    setStatus('sending');
    const data = new FormData(e.currentTarget);
    data.append('access_key', process.env.GATSBY_FORMIZ_KEY);
    try {
      const res = await fetch('https://api.formiz.app/submit', { method: 'POST', body: data });
      const json = await res.json();
      if (json.success) { e.target.reset(); setStatus('sent'); } else setStatus('error');
    } catch {
      setStatus('error');
    }
  }

  if (status === 'sent') return <p role="status">Thanks! We will get back to you soon.</p>;

  return (
    <form onSubmit={onSubmit} className="contact-form">
      <input type="checkbox" name="_honey" style={{ display: 'none' }} tabIndex={-1} autoComplete="off" />
      <label htmlFor="name">Name</label>
      <input id="name" name="name" autoComplete="name" required />
      <label htmlFor="email">Email</label>
      <input id="email" name="email" type="email" autoComplete="email" required />
      <label htmlFor="message">Message</label>
      <textarea id="message" name="message" rows={5} required />
      <button type="submit" disabled={status === 'sending'}>
        {status === 'sending' ? 'Sending…' : 'Send message'}
      </button>
      {status === 'error' && <p role="alert">Something went wrong. Please try again.</p>}
    </form>
  );
}

3. Use it on a page

src/pages/contact.jsx:

jsx
import React from 'react';
import ContactForm from '../components/ContactForm';

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

export const Head = () => <title>Contact us</title>;

Run gatsby develop, open /contact and send a test message. It appears in your inbox and in your email straight away.

Why not a serverless function?

You can write a Gatsby Function or a host-specific function that sends email. It means managing an email provider, secrets, spam filtering and error handling yourself, and rewriting it if you change hosts. A form backend keeps the site portable; see Netlify Forms alternative for the same reasoning.

Next steps

Get your form working in five minutes

Create a free Formiz key, add it to your form and receive submissions by email and Telegram. No server, no SMTP, no card.