Overview
After receiving a token from the Turnstile widget, you must validate it on your server by calling Cloudflare’s siteverify API.Validation Endpoint
TurnstileServerValidationResponse
Response Fields
Indicates if the token validation was successful or not.
true: Token is validfalse: Token is invalid (checkerror-codesfor details)
A list of error codes that occurred during validation. Empty array if validation succeeded.See Error Codes below for all possible values.
The ISO timestamp for when the challenge was solved.Example:
"2023-10-15T12:34:56.789Z"The hostname for which the challenge was served.Example:
"example.com"The customer widget identifier passed to the widget on the client side. Used to differentiate widgets using the same sitekey in analytics.Its integrity is protected from modifications by an attacker. It is recommended to validate that the action matches an expected value.
The customer data passed to the widget on the client side. This can be used to convey state.Its integrity is protected from modifications by an attacker.
Additional metadata about the challenge.
interactive: Whether an interactive challenge was issued by Cloudflare
Error messages returned from the validation.
Error Codes
Error Code Descriptions
The secret parameter was not passed in the request.
The secret parameter was invalid or does not exist.
The response parameter (token) was not passed in the request.
The response parameter (token) is invalid or has expired.
The widget ID extracted from the parsed site secret key was invalid or does not exist.
The secret extracted from the parsed site secret key was invalid.
The request was rejected because it was malformed.
The response parameter has already been validated before. Tokens can only be validated once.
An internal error happened while validating the response. The request can be retried.
Validation Example
Client-Side (React)
Server-Side (Node.js/Express)
Server-Side (Next.js API Route)
Best Practices
- Always validate server-side: Never trust client-side validation alone
- Validate once: Tokens can only be validated once. Store the result if needed
- Check action field: Validate that the
actionmatches your expected value - Handle errors gracefully: Reset the widget on validation failure
- Use environment variables: Store your secret key securely
- Set timeout: Don’t wait indefinitely for token generation
Security Considerations
- Keep secret key secure: Never expose your Turnstile secret key in client-side code
- Validate hostname: Check that the
hostnamefield matches your domain - Use HTTPS: Always validate tokens over HTTPS
- Rate limiting: Implement rate limiting on your validation endpoint
- Token expiry: Tokens expire after a few minutes. Validate promptly
Related Documentation
- Cloudflare Turnstile Server-side Validation
- Cloudflare Error Codes
- TurnstileInstance - Client-side methods