Get Turnstile Credentials
To use Cloudflare Turnstile for bot protection in your Mindful Auth portal, you need to obtain Turnstile credentials (site key and secret key) for each hostname you onboard. Each tenant must provide their own Cloudflare Turnstile credentials during onboarding.
1. Create a Cloudflare Account
Section titled “1. Create a Cloudflare Account”If you don’t have one, create a free Cloudflare account at cloudflare.com.
2. Create a Turnstile Widget
Section titled “2. Create a Turnstile Widget”- Go to Cloudflare Dashboard → Turnstile
- Click Add Widget
- Configure the widget:
- Name: Your portal name (e.g., “My Company Portal”)
- Domains: Add your portal domain (e.g.,
portal.yourcompany.com) — This is required before onboarding - Mode: Managed (recommended)
- Pre-clearance: Disabled (recommended)
- Click Create
- Copy the Site Key and Secret Key
⚠️ Important: You must add your hostname to the widget’s domain list in Cloudflare before onboarding. The worker does not automatically add hostnames to your Turnstile widget.
3. Provide Credentials During Onboarding
Section titled “3. Provide Credentials During Onboarding”When onboarding your hostname, provide both keys:
turnstileSitekey: The public site key (starts with0x4A...)turnstileSecretKey: The secret key (starts with0x4AAA...)
Example onboarding request:
{ "hostname": "portal.yourcompany.com", "backendType": "tape", "turnstileSitekey": "0x4AAAAAAAxyz123...", "turnstileSecretKey": "0x4AAAAAAAsecret456...", ...}How It Works
Section titled “How It Works”Frontend Flow
Section titled “Frontend Flow”- Portal page loads
turnstile-init.jsscript - Script calls
GET /auth/get-tenant-configto fetch the tenant’s sitekey - Script renders Turnstile widget with tenant-specific sitekey
- User completes challenge, token is added to form
Backend Flow
Section titled “Backend Flow”- Form submission includes
cf-turnstile-responsetoken - Worker decrypts tenant’s secret key from Cloudflare KV storage
- Worker verifies token with Cloudflare using tenant’s secret key
- Request proceeds if verification passes
Frontend Integration
Section titled “Frontend Integration”Add these scripts to your portal pages that need bot protection:
<!-- Turnstile library --><script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
<!-- Auto-init script (fetches tenant sitekey automatically) --><script src="https://cdn.mindfulauth.com/scripts/turnstile-init.js"></script>
<!-- Widget container --><div id="turnstile-container"></div>Widget Customization
Section titled “Widget Customization”Customize the widget using data attributes on the container div:
<!-- Dark theme --><div id="turnstile-container" data-theme="dark"></div>
<!-- Compact size --><div id="turnstile-container" data-size="compact"></div>
<!-- Spanish language --><div id="turnstile-container" data-language="es"></div>
<!-- Invisible mode --><div id="turnstile-container" data-appearance="execute"></div>
<!-- Multiple customizations --><div id="turnstile-container" data-theme="dark" data-size="compact" data-language="fr"></div>Available Options:
data-theme:light(default),dark,autodata-size:flexible(default),normal,compactdata-language:auto(default), or language codedata-appearance:always(default),execute,interaction-only
Updating Turnstile Credentials
Section titled “Updating Turnstile Credentials”To rotate or update Turnstile credentials go to the edit hostname page here. More information here Edit Hostname Guide. Note: Both keys must be provided together when updating.
Troubleshooting
Section titled “Troubleshooting””Bot protection validation failed”
Section titled “”Bot protection validation failed””- Cause: Invalid or expired Turnstile token
- Solution: Ensure widget is properly rendered, check for JavaScript errors
”Turnstile widget not appearing”
Section titled “”Turnstile widget not appearing””- Causes:
- Domain not added to widget in Cloudflare dashboard (must be done before onboarding)
- Tenant config not found
- Solutions:
- Add your portal domain to Turnstile widget settings in Cloudflare, then re-test
- Verify tenant is onboarded with correct hostname
”Missing turnstileSitekey in config”
Section titled “”Missing turnstileSitekey in config””- Cause: Tenant onboarded without Turnstile credentials
- Solution: Update hostname with Turnstile credentials via admin endpoint
Token Always Rejected
Section titled “Token Always Rejected”- Causes:
- Wrong secret key provided during onboarding
- Sitekey/secret key mismatch
- Solutions:
- Verify you copied both keys from the same widget
- Re-onboard or update with correct credentials
Security Notes
Section titled “Security Notes”- Secret key is encrypted at rest: Stored with AES-GCM encryption in Cloudflare KV
- Never expose secret key in frontend: Only the sitekey is public
- Rotate keys if compromised: Use update endpoint to change credentials
- Multiple hostnames per widget: You can add multiple of your hostnames to a single Turnstile widget if you prefer to manage them together