Replace reCAPTCHA With Turnstile on a Contact Form
What changes when a contact form moves from reCAPTCHA to Cloudflare Turnstile: the script, the field name, the verify URL, token expiry and single use.
In this post
Moving a contact form from reCAPTCHA to Cloudflare Turnstile is a small change in the page and a larger one on the server. Cloudflare's migration mode keeps the form's field name and the reCAPTCHA API names, so the front end needs few edits. The server needs a new verification call with a new URL, a new secret and a POST request. This post walks through the changes for a plain contact form, the token rules that affect your handler, and the reason the server check must stay in place.
What stays the same
Whatever the reason for switching, the form still submits to your handler, the handler still needs a token from the widget, and the handler still has to check that token before it sends the message.
Cloudflare's migration guide sets a limit on the starting point. It notes that "Turnstile migration is currently compatible up to reCAPTCHA v2." If your form uses a later reCAPTCHA version, check the migration guide before you start.
Client side: the script and the compatibility mode
The first change is the script in the page head. Cloudflare's migration guide gives the tag: a script from challenges.cloudflare.com/turnstile/v0/api.js with the parameter compat=recaptcha. The parameter enables reCAPTCHA compatibility mode. Cloudflare lists three things it provides: implicit rendering for reCAPTCHA, the g-recaptcha-response input name for forms, and the Turnstile API registered under the name grecaptcha.
Those three points mean an existing form may run with little change to its markup. The widget still needs your Turnstile sitekey, not the reCAPTCHA one. Cloudflare's instruction is to locate the grecaptcha.render() calls and replace the sitekey with your Turnstile sitekey. Turnstile supports the render() call, and reCAPTCHA v2 invisible mode with execute().
You need the sitekey and the secret key from the Turnstile dashboard before you start. Without the secret, the server cannot complete the check described below.
The form field that carries the token
The widget places a token into the form, and the field that carries it is named g-recaptcha-response. In compatibility mode, Cloudflare keeps that name, so a handler that reads the field from a submitted form can keep reading it. The change in the handler is what it does with the value, not the name it reads.
Check the field name in the rendered form before you change the server. A form that submits without the field will fail the server check, and the failure will look like a bot rejection when it is a missing token.
Server side: the siteverify call and its parameters
The server call is the main change. The reCAPTCHA verify URL is google.com/recaptcha/api/siteverify. Cloudflare's instruction is to replace it with https://challenges.cloudflare.com/turnstile/v0/siteverify.
The method changes as well. reCAPTCHA accepts GET requests with query parameters. Turnstile's siteverify endpoint "only accepts POST requests with a FormData or JSON body." A handler that builds a query string for GET must send a POST with the body instead.
Cloudflare's server validation page lists the parameters. Two are required: secret, the widget's secret key from the dashboard, and response, the token from the client-side widget. Two are optional: remoteip, the visitor's IP address, and idempotency_key, a UUID you generate so that a retried validation request can be identified. Pass the visitor's IP where your handler has it. The idempotency key is a UUID you generate, and the documentation describes it as a way to retry validation requests safely.
The call returns a result that your handler must read and act on. Your handler should reject the submission when the check fails, not only log it.
Token lifetime and single use
Cloudflare's server validation page states two rules that affect a form handler. The first is validity: "each token is valid for 300 seconds (5 minutes) after generation." A visitor who waits longer than five minutes on the page before submitting will hold an expired token, and the verification will fail.
The second is single use: "Tokens are intended for single use." A token that is replayed, such as by a double submit or a retry from the browser, returns a timeout-or-duplicate error, which Cloudflare describes as the token having "expired or has already been validated."
These two rules shape the handler. A handler that retries a verification with the same token will hit the single-use rule on the second call. Handle the error as a rejection with a message that asks the visitor to submit again.
Keep the server check: why the widget alone protects nothing
The server check is not optional. Cloudflare states the point directly: "You must call the Siteverify API to complete your Turnstile implementation." It adds: "The client-side widget alone does not protect your forms."
This matters during a migration. Replacing the script and the sitekey makes the page look finished, and a form that submits with a widget but no server check will appear to work. The protection only exists once the handler calls siteverify and rejects a failed result. Test that the handler rejects a missing or invalid token, not only that the widget renders.
A short test plan
Test each path on a staging copy before the live switch.
- Submit with a valid widget and confirm the message is sent once.
- Submit with the widget removed from the page and confirm the handler rejects the request.
- Submit a token that has already been used and confirm the
timeout-or-duplicatepath returns a rejection. - Wait longer than five minutes on the page, submit and confirm the expired token is rejected.
- Check that the verify call is a POST with a body, not a GET with a query string.
- Confirm that the reCAPTCHA sitekey and secret are gone from the page and the server configuration.
For the spam layers that sit around a form like this, see how to stop contact form spam. For how a form reaches its destination, see how it works, and for the form setup itself, contact us if you want a second look.
Sources
- Cloudflare Turnstile: Migrate from reCAPTCHA, for the script with
compat=recaptcha, the input name, the sitekey replacement, the siteverify URL, the POST requirement and the reCAPTCHA v2 limit. - Cloudflare Turnstile: Server-side validation, for the siteverify parameters, the 300-second token validity, single use, the
timeout-or-duplicateerror and the mandatory server check.