Captcha
Google reCAPTCHA v3
Invisible score-based captcha from Google.
Google reCAPTCHA v3 runs invisibly. It never shows a checkbox or puzzle: instead Google gives each visit a score from 0.0 (very likely a bot) to 1.0 (very likely a human). Lazyform checks the score 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.
What you need
- Site key: public, goes in your HTML.
- Secret key: private, goes only into Lazyform.
- Minimum score (optional): submissions scoring below this are refused. Defaults to
0.5.
To get the keys:
- Go to the reCAPTCHA admin console and sign in with a Google account.
- Give the site a label and choose reCAPTCHA v3 (score based).
- Add the domains your form runs on, for example
example.com. Addlocalhosttoo if you test locally. - Submit, then copy the site key and secret key.
Keys are tied to the reCAPTCHA type: v3 keys don't work with v2, and the other way round.
Choosing a minimum score
0.5 suits most forms. If real visitors get refused, lower it (for example to 0.3). If spam still gets through, raise it (for example to 0.7). Scores are often lower for visitors who arrive and submit straight away, use a VPN, or block cookies, so don't set it too high.
Set it up
- In the dashboard, open your form, then Integrations, then Google reCAPTCHA v3.
- Paste the site key and secret key. Change the minimum score if you want, and save.
- Add the reCAPTCHA script and the token code to your form (below).
Once reCAPTCHA is on, every submission to this form must include a valid reCAPTCHA token. Add the code to your form before (or right after) you switch the integration on, or submissions will be refused.
Add it to your form
reCAPTCHA v3 has no widget. Load the script with your site key in render=, and when the visitor submits, ask Google for a token, put it in a hidden input named g-recaptcha-response, then send the form. Replace both YOUR_SITE_KEY values with your site key.
<form id="contact" 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">
<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
<button type="submit">Send</button>
</form>
<script src="https://www.google.com/recaptcha/api.js?render=YOUR_SITE_KEY"></script>
<script>
const form = document.getElementById('contact')
form.addEventListener('submit', (event) => {
event.preventDefault()
grecaptcha.ready(() => {
grecaptcha.execute('YOUR_SITE_KEY', { action: 'submit' }).then((token) => {
document.getElementById('g-recaptcha-response').value = token
form.submit()
})
})
})
</script>Get the token when the visitor submits, not when the page loads: tokens expire after two minutes and work only once.
form.submit() doesn't work if any element in the form is named or has the id submit. Keep name="submit" off your button.
Lazyform reads the g-recaptcha-response token, checks it with Google using your secret key, compares the score with your minimum, and removes the token before storing the submission.
Sending with JavaScript (fetch)
With fetch, get a token the same way and add it to the JSON you send:
<form id="contact">
<input type="hidden" name="access_key" value="YOUR_ACCESS_KEY">
<input type="email" name="email" required>
<textarea name="message" required></textarea>
<button type="submit">Send</button>
<p id="status"></p>
</form>
<script src="https://www.google.com/recaptcha/api.js?render=YOUR_SITE_KEY"></script>
<script>
const form = document.getElementById('contact')
const status = document.getElementById('status')
function getToken() {
return new Promise((resolve) => {
grecaptcha.ready(() => {
grecaptcha.execute('YOUR_SITE_KEY', { action: 'submit' }).then(resolve)
})
})
}
form.addEventListener('submit', async (event) => {
event.preventDefault()
const data = Object.fromEntries(new FormData(form))
data['g-recaptcha-response'] = await getToken()
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
}
})
</script>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:
{ "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 hidden input is named exactlyg-recaptcha-responseand sits inside the form, and that bothYOUR_SITE_KEYvalues were replaced. Check both keys come from the same reCAPTCHA site, and that it's a v3 site. - Real visitors are refused. Lower the minimum score in the integration's settings. The reCAPTCHA admin console shows the score spread for your site.
- It fails after the page was open a while. Make sure you call
grecaptcha.executewhen the visitor submits, not on page load. - 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.