justbetter / statamic-turnstile
Cloudflare Turnstile protection for Statamic forms
Requires
- php: ^8.4
- laravel/framework: ^12.40|^13.0
- statamic/cms: ^6.44
Requires (Dev)
- larastan/larastan: ^3.9
- laravel/pint: ^1.29
- orchestra/testbench: ^10.11|^11.0
- pestphp/pest: ^3.7
- phpstan/phpstan-mockery: ^2.0
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-12-02 06:27:10 UTC
README
Cloudflare Turnstile protection for Statamic 6 forms. It works with AJAX-submitted forms and with any frontend: plain JS, Alpine or Vue (including Rapidez).
- Per form: add a
Turnstilefield to a form blueprint. - Per environment: nothing is rendered or verified unless
TURNSTILE_ENABLED=trueand both keys are set. That way you can share blueprints between test and production and only protect production. - Validation: the token is checked in Statamic's regular form validation. A failed check comes back as a normal field error (
error.turnstile). - Storage: the token is never stored in the submission.
Installation
composer require justbetter/statamic-turnstile
TURNSTILE_ENABLED=true TURNSTILE_SITE_KEY= TURNSTILE_SECRET_KEY=
Optionally publish the config with php artisan vendor:publish --tag=justbetter-turnstile (published to config/justbetter/turnstile.php).
| Key | Default | |
|---|---|---|
enabled |
false |
Turns Turnstile on for this environment. |
sites |
[] |
Keys per site handle, see Multisite. |
fail_open |
false |
Accept submissions when Cloudflare can't be reached. Either way, the failure is logged. |
check_hostname |
true |
Reject tokens issued for a different hostname than the request. |
Multisite
One Turnstile widget can cover several hostnames, so most multisites only need the global keys. When sites need their own widget (for example sites in different Cloudflare accounts), set keys per site handle in the published config:
'sites' => [ 'nl' => [ 'site_key' => env('TURNSTILE_NL_SITE_KEY'), 'secret_key' => env('TURNSTILE_NL_SECRET_KEY'), ], ],
A key set for a site overrides the global key, and sites without an entry use the global keys. An empty key turns Turnstile off for that site. The widget uses the keys of the current site, and a submission is verified with the keys of the site it was submitted from, which is the same site Statamic uses for the submission.
Local testing
Use Cloudflare's test keys:
- Site key:
1x00000000000000000000AA - Secret that always passes:
1x0000000000000000000000000000000AA - Secret that always fails:
2x0000000000000000000000000000000AA
Test keys always report the hostname example.com, so also set TURNSTILE_CHECK_HOSTNAME=false locally.
Usage
Add a field of type Turnstile to the form blueprint. It is only available in form blueprints.
Rendering the widget
Statamic's own field rendering, such as {{ fields }}{{ field }}{{ /fields }}, renders the widget automatically.
For custom form markup:
<x-turnstile::widget handle="turnstile" /> <x-turnstile::widget handle="turnstile" theme="dark" size="compact" />
{{ turnstile:widget handle="turnstile" }}
Language
The widget is shown in the language of the current Statamic site (its lang, for example nl). Pass language to use another language, or auto to follow the visitor's browser:
<x-turnstile::widget handle="turnstile" language="auto" />
{{ turnstile:widget handle="turnstile" language="auto" }}
JavaScript
Import the script once in your site's JS. It loads Cloudflare's script itself, on demand, and watches the DOM, so widgets that Alpine, Vue or an AJAX request add later are rendered too. This works the same on plain Antlers sites, Alpine and Vue 3, including Rapidez.
import '../../vendor/justbetter/statamic-turnstile/resources/js/turnstile.js'
The widget dispatches bubbling turnstile:verified (event.detail.token), turnstile:expired and turnstile:error events, so you can listen to it from any framework:
<form @turnstile:verified="canSubmit = true" @turnstile:expired="canSubmit = false">
window.JustBetterTurnstile exposes reset(target), getToken(target), render(element, options) and remove(target), where target is the widget or any ancestor such as the form.
AJAX submissions
Turnstile tokens are single-use. Reset the widget after every completed submission, whether it succeeded or failed:
window.JustBetterTurnstile?.reset(formElement)
The token is posted under the field handle, so new FormData(form) already includes it.
For AJAX requests, Statamic returns validation errors as HTTP 400 with this body:
{ "errors": [], "error": { "turnstile": "…" } }
Make sure your submit handler shows these errors.
Quality
composer quality # phpunit, phpstan (level 9), pint composer coverage # 100% line coverage on src/
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.