Skip to content
Lazyform
Docs menu

Captcha

hCaptcha

Add hCaptcha to your form so only real visitors can submit. A privacy-focused captcha.

hCaptcha checks that a visitor is human before Lazyform accepts the submission. Visitors tick a checkbox and, when hCaptcha isn't sure, solve a short image puzzle. It's a privacy-focused alternative to Google reCAPTCHA.

Available on all plans. See pricing.

Lazyform already blocks most spam without a captcha (see spam protection). Add one if bots still get through.

What you need

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

To get them:

  1. Sign up or sign in at dashboard.hcaptcha.com.
  2. Open Sites and add a new site. Add the domains your form runs on, for example example.com.
  3. Copy the site's sitekey.
  4. Open your account Settings and copy your secret key.

Set it up

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

Once hCaptcha is on, every submission to this form must include a valid hCaptcha 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 h-captcha 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="h-captcha" data-sitekey="YOUR_SITE_KEY"></div>

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

<script src="https://js.hcaptcha.com/1/api.js" async defer></script>

When the visitor completes the check, the widget adds a hidden field called h-captcha-response to your form. Lazyform reads that token, checks it with hCaptcha 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 h-captcha-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="h-captcha" data-sitekey="YOUR_SITE_KEY"></div>
  <button type="submit">Send</button>
  <p id="status"></p>
</form>

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

  form.addEventListener('submit', async (event) => {
    event.preventDefault()
    // Includes "h-captcha-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
    hcaptcha.reset()
  })
</script>

If you build the JSON yourself, add the token as "h-captcha-response": token. 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 checkbox before sending. Check the site key in your HTML and the secret key in Lazyform belong to the same hCaptcha account.
  • The widget doesn't load or shows a domain error. Add your site's domain to the site's settings in the hCaptcha dashboard.
  • It fails on the second try. Tokens work once. After a failed fetch submission, call hcaptcha.reset().
  • Submissions from my server or scripts fail. With hCaptcha 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.