Frameworks

FormRelay has no client library you must install: a form is one POST request. This page shows that request in React and Astro, and what to do in other frameworks.

Last updated

React

A component that sends its form with fetch and shows the result. It needs nothing besides React.

import { useState } from 'react';

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

  async function onSubmit(event) {
    event.preventDefault();
    const data = new FormData(event.target);
    data.append('access_key', 'YOUR_ACCESS_KEY');
    const response = await fetch('https://submit.formrelay.domodomu.com/submit', {
      method: 'POST',
      headers: { Accept: 'application/json' },
      body: data,
    });
    const result = await response.json();
    setStatus(result.success ? 'Thanks! Your message was sent.' : result.message);
    if (result.success) event.target.reset();
  }

  return (
    <form onSubmit={onSubmit}>
      <input type="text" name="name" required />
      <input type="email" name="email" required />
      <textarea name="message" required />
      <button type="submit">Send</button>
      <p>{status}</p>
    </form>
  );
}

When the form requires a captcha, the Install tab's React snippet also renders the widget and resets it after each send.

The @formrelay/react package

A small hook, useFormRelay, does the same and tracks whether the form is sending, sent or failed. It is coming to npm and isn't published there yet, so use the component above until it is.

import { useFormRelay } from '@formrelay/react';

export default function ContactForm() {
  const { submit, sending, sent, failed, message } = useFormRelay({
    endpoint: 'https://submit.formrelay.domodomu.com/submit',
    accessKey: 'YOUR_ACCESS_KEY',
  });

  if (sent) return <p>Thanks! Your message was sent.</p>;

  return (
    <form onSubmit={submit}>
      <input type="text" name="name" required />
      <input type="email" name="email" required />
      <textarea name="message" required />
      <button type="submit" disabled={sending}>{sending ? 'Sending…' : 'Send'}</button>
      {failed && <p role="alert">{message}</p>}
    </form>
  );
}
OptionWhat it does
endpoint, accessKeyRequired. Both are on the form's Install tab.
subjectThe email's subject line.
getCaptchaTokenWhen the form requires a captcha: a function that returns the widget's token. It is called before each send.
captchaFieldThe token's field name: cf-turnstile-response (the default), h-captcha-response, or g-recaptcha-response for reCAPTCHA v2 and v3.
resetOnSuccessClear the form after a successful send. On by default.
onSuccess(result), onError(result)Called after each send with the answer: success, message and, when accepted, id.

The hook returns submit, reset, status (idle, sending, sent or failed), message, and the booleans sending, sent and failed. A second submit while one is on its way is ignored. submit also takes a plain object or a FormData, so it works as the handler of a form library such as react-hook-form.

The package also has the same request as a plain function, for code that doesn't use React:

import { submitForm } from '@formrelay/react/submit';

const result = await submitForm({
  endpoint: 'https://submit.formrelay.domodomu.com/submit',
  accessKey: 'YOUR_ACCESS_KEY',
  data: { name: 'Ada', email: 'ada@example.com' },
});

Astro

An Astro page can hold a plain HTML form, so it works with no JavaScript in the browser.

---
const accessKey = 'YOUR_ACCESS_KEY';
---
<form action="https://submit.formrelay.domodomu.com/submit" method="POST">
  <input type="hidden" name="access_key" value={accessKey} />
  <input type="text" name="name" required />
  <input type="email" name="email" required />
  <textarea name="message" required></textarea>
  <input type="checkbox" name="botcheck" style="display:none" tabindex="-1" />
  <button type="submit">Send</button>
</form>

Vue, Svelte, Next.js and others

There is nothing framework-specific to learn. Either render a plain HTML form whose action is the submit address, as on HTML forms, or call fetch from your submit handler, as on JavaScript and fetch.

  • Send the request from the browser, not from your server. The rate limit and the allowed-sites check use the visitor's address and the page's origin.
  • Add access_key to the data you send. It is public, so it can sit in your client code.
  • Ask for JSON with Accept: application/json, then read success and message from the answer.
  • If the form requires a captcha, render the provider's widget and send its token with the form. See Spam protection.

Something missing or wrong? Email support@domodomu.com. New to FormRelay? See what it does or start free.