v1.5.0
Cloudflare Turnstile integration for React.
Repository
Current version released
a day ago
Features
- ๐ต๏ธโโ๏ธ Privacy-first - No user tracking, no cookies, GDPR-friendly by design
- ๐ฆ Lightweight - Only 6.3 KB minified
- ๐ช Smart verification - Often invisible to users with minimal interaction
- ๐ Zero config - Automatic script injection, works out of the box
- ๐ Multiple widget sizes - normal, compact, flexible, invisible
- ๐ฎ Imperative API - Control via ref:
reset(),execute(),getResponse(),isExpired() - โก๏ธ SSR ready - Works with Next.js, Remix, and any React SSR framework
- ๐ป Full TypeScript support
๐ Read the docs โ ย ย ๐ See the demo โ
Getting started
- Follow these steps to obtain a free site key and secret key from Cloudflare.
- Install
@marsidev/react-turnstileinto your React project.
pnpm add @marsidev/react-turnstileUsage
Basic
import { Turnstile } from '@marsidev/react-turnstile'
function Widget() {
return <Turnstile siteKey='1x00000000000000000000AA' />
}With form and imperative API
import { Turnstile } from '@marsidev/react-turnstile'
import type { TurnstileInstance } from '@marsidev/react-turnstile'
import { useRef } from 'react'
function LoginForm() {
const turnstileRef = useRef<TurnstileInstance | null>(null)
async function handleSubmit(e: FormEvent) {
e.preventDefault()
const token = turnstileRef.current?.getResponse()
// Send token to your server...
turnstileRef.current?.reset() // Reset after submission
}
return (
<form onSubmit={handleSubmit}>
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<Turnstile
ref={turnstileRef}
siteKey='1x00000000000000000000AA'
/>
<button type="submit">Login</button>
</form>
)
}Contributing
Any contributions are greatly appreciated. If you have a suggestion that would make this project better, please fork the repo and create a Pull Request. You can also open an issue.
Development
- Fork or clone this repository.
- Install dependencies with
pnpm install. - Run
pnpm devto start both the library (with HMR) and the Next.js demo app concurrently
For reference, the project structure is as follows:
.
โโโ packages/lib/ # Library source code
โโโ demos/nextjs/ # Next.js demo app
โโโ docs/ # DocumentationSupport
If you find this library useful, you can buy me a coffee:
Contributors
License
Published under the MIT License.