Captcha
Cloudflare Turnstile
Free, privacy-friendly captcha. Usually invisible to visitors.
Cloudflare Turnstile checks that a visitor is human before Lazyform accepts the submission. Most visitors never see a puzzle: Turnstile decides in the background and only asks for a click when it isn't sure. You don't need to use Cloudflare for your website to use it.
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:
- Sign in to the Cloudflare dashboard (a free account is enough) and open Turnstile.
- Click Add widget (or Add site).
- Give it a name and add the domains your form runs on, for example
example.com. Addlocalhosttoo if you test locally. - Pick a widget mode. Managed is a good default.
- Create it, then copy the site key and the secret key.
Set it up
- In the dashboard, open your form, then Integrations, then Cloudflare Turnstile.
- Paste the site key and secret key, and save.
- Add the Turnstile script and widget to your form (below).
Once Turnstile is on, every submission to this form must include a valid Turnstile 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 cf-turnstile div inside your form, where you want the widget to appear. Replace YOUR_SITE_KEY with your site key.
<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="cf-turnstile" data-sitekey="YOUR_SITE_KEY"></div>
<button type="submit">Send</button>
</form>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>The widget adds a hidden field called cf-turnstile-response to your form. Lazyform reads that token, checks it with Cloudflare 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 cf-turnstile-response field for you:
<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="cf-turnstile" data-sitekey="YOUR_SITE_KEY"></div>
<button type="submit">Send</button>
<p id="status"></p>
</form>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
<script>
const form = document.getElementById('contact')
const status = document.getElementById('status')
form.addEventListener('submit', async (event) => {
event.preventDefault()
// Includes "cf-turnstile-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: get a fresh one for the next try
turnstile.reset()
})
</script>If you build the JSON yourself, add the token as "cf-turnstile-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:
{ "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, so its hidden field is sent. Check the site key in your HTML and the secret key in Lazyform come from the same Turnstile widget. - The widget shows an error or doesn't load. Your site's domain must be listed in the widget's settings in Cloudflare. Add
localhostfor local testing. - It fails on the second try. Tokens work once. After a failed
fetchsubmission, callturnstile.reset()to get a new token. - Submissions from my server or scripts fail. With Turnstile 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.