https://github.com/maaaathis/turnstile-verify
Simple server-side implementation for validating Cloudflare Turnstile captchas
https://github.com/maaaathis/turnstile-verify
Last synced: about 2 months ago
JSON representation
Simple server-side implementation for validating Cloudflare Turnstile captchas
- Host: GitHub
- URL: https://github.com/maaaathis/turnstile-verify
- Owner: maaaathis
- License: mit
- Created: 2023-11-12T00:00:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-13T11:43:40.000Z (5 months ago)
- Last Synced: 2025-04-12T03:36:57.401Z (2 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/turnstile-verify
- Size: 1.27 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# Turnstile Verify
This npm package provides a simple server-side implementation for validating [Cloudflare Turnstile](https://challenges.cloudflare.com/) captchas.
## Installation
```shell
npm i turnstile-verify
```## Example usage
```javascript
const { TurnstileVerify } = require('turnstile-verify');// Your Cloudflare Turnstile secret key
const SECRET_KEY = '1x0000000000000000000000000000000AA';// ...
// Access Cloudflare Turnstile response and remote IP address
const cfTurnstileResponse = req.body['cf-turnstile-response'];
const remoteip =
req.headers['CF-Connecting-IP'] ||
req.headers['x-forwarded-for'] ||
req.socket.remoteAddress; // optional// Create a Turnstile instance with the secret key
const turnstile = new TurnstileVerify({ token: SECRET_KEY });// Validate the Turnstile response
const turnstileResponse = await turnstile.validate({
response: cfTurnstileResponse,
remoteip,
});// Handle an invalid captcha response
if (!turnstileResponse.valid) {
// ....Handle invalid captcha response
}// ...
```### Response
```
{
valid: boolean, => Indicates if capcha solved correctly?
messages: string[] => In case of an error the messages from cloudflare are passed here.
}
```## Client side implementation
For client side implementaion see [official documentation](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/), [react-turnstile](https://www.npmjs.com/package/react-turnstile) or use other npm package suitable for your framework.
## License
[MIT](https://choosealicense.com/licenses/mit/)