Skip to content
Lazyform
All integrations

Captcha

Google reCAPTCHA v2

The "I'm not a robot" checkbox.

ProNeeds the Pro plan

Google reCAPTCHA v2 adds the familiar "I'm not a robot" checkbox to your form. Lazyform checks the visitor passed it before accepting the submission.

Needs the Pro plan.

Lazyform already blocks most spam without a captcha (see spam protection). Add one if bots still get through. If you'd rather not show a checkbox, see reCAPTCHA v3 or Cloudflare Turnstile.

What you need

  • Site key: public, goes in your HTML.
  • Secret key: private, goes only into Lazyform.

To get them:

  1. Go to the reCAPTCHA admin console and sign in with a Google account.
  2. Give the site a label and choose reCAPTCHA v2, then "I'm not a robot" Checkbox.
  3. Add the domains your form runs on, for example example.com. Add localhost too if you test locally.
  4. Submit, then copy the site key and secret key.

Keys are tied to the reCAPTCHA type: v2 keys don't work with v3, and the other way round.

Set it up

  1. In the dashboard, open your form, then Integrations, then Google reCAPTCHA v2.
  2. Paste the site key and secret key, and save.
  3. Add the reCAPTCHA script and widget to your form (below).

Once reCAPTCHA is on, every submission to this form must include a valid reCAPTCHA token. Add the widget to your form before (or right after) you switch the integration on, or submissions will be refused.

Add it to your form

Add the script once on the page, and the g-recaptcha div inside your form, where you want the checkbox. Replace YOUR_SITE_KEY with your site key.

html
<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="Your name" required>
  <input type="email" name="email" placeholder="Your email" required>
  <textarea name="message" placeholder="Your message" required></textarea>

  <input type="checkbox" name="botcheck" class="hidden" style="display:none">

  <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>

  <button type="submit">Send</button>
</form>

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

When the visitor ticks the box, the widget adds a hidden field called g-recaptcha-response to your form. Lazyform reads that token, checks it with Google using your secret key, and removes it before storing the submission.

Sending with JavaScript (fetch)

If you send the form with fetch, include the token in the request. Reading the form with FormData picks up the hidden g-recaptcha-response field for you:

html
<form id="contact">
  <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY">
  <input type="email" name="email" required>
  <textarea name="message" required></textarea>
  <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
  <button type="submit">Send</button>
  <p id="status"></p>
</form>

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
  const form = document.getElementById('contact')
  const status = document.getElementById('status')

  form.addEventListener('submit', async (event) => {
    event.preventDefault()
    // Includes "g-recaptcha-response" with the token
    const data = Object.fromEntries(new FormData(form))

    const response = await fetch('https://lazyform-api.xvelopers.com/submit', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
      body: JSON.stringify(data),
    })
    const result = await response.json()

    if (result.success) {
      status.textContent = 'Thanks! Your message was sent.'
      form.reset()
    } else {
      status.textContent = result.error.message
    }
    // Each token works only once: clear the checkbox for the next try
    grecaptcha.reset()
  })
</script>

If you build the JSON yourself, add the token as "g-recaptcha-response": grecaptcha.getResponse(). See AJAX submissions for more.

If the check fails

When the token is missing, expired, already used or invalid, Lazyform refuses the submission with the error code captcha_failed (status 400). JSON requests get:

json
{ "success": false, "error": { "code": "captcha_failed", "message": "…" } }

HTML forms send the visitor to an error page that explains the problem and links back to your form.

Troubleshooting

  • Every submission fails with captcha_failed. Check the widget is inside the <form> tags, and that the visitor ticked the box before sending. Check both keys come from the same reCAPTCHA site, and that it's a v2 Checkbox site.
  • "Invalid domain for site key". Add your site's domain in the reCAPTCHA admin console.
  • It fails after the visitor waited a while. The token expires about two minutes after the box is ticked. The visitor needs to tick it again.
  • Submissions from my server or scripts fail. With reCAPTCHA on, every submission to the form needs a token from a real browser. Use a separate form (and access key) for server-to-server submissions.