INTEGRITY Cloudflare Docs

Widget configurations

Configure your Turnstile widget's appearance, behavior, and functionality using data attributes or JavaScript render parameters.

Rendering methods

Turnstile widgets can be implemented using implicit or explicit rendering.

Implicit rendering automatically scans your HTML for elements with the cf-turnstile class and renders the widget when the page loads. It is best used for simple implementations, static websites, or when you want widgets to appear immediately on page load.

How it works

  1. Add the Turnstile script to your page.
  2. Include <div class="cf-turnstile" data-sitekey="your-key"></div> elements.
  3. Widgets will render automatically when the page loads.
  4. Configure the widget using data-* attributes on the HTML element.
Example
	<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-theme="light"></div>

Explicit rendering gives you programmatic control over when and how widgets are created using JavaScript functions. It is best used for dynamic websites and single-page applications (SPAs), when you need to control timing of widget creation, conditional rendering based on visitor interactions, or for multiple widgets with different configurations.

How it works

  1. Add the Turnstile script with ?render=explicit parameter.
  2. Create container elements (without the cf-turnstile class).
  3. Call turnstile.render() function when you want to create widgets.
  4. Configure the widget using JavaScript object parameters.
Example
	<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" defer></script>
	<div id="my-widget"></div>
	
	<script>
	window.onload = function() {
		turnstile.render('#my-widget', {
			sitekey: '<YOUR-SITE-KEY>',
			theme: 'light',
			callback: function(token) {
				console.log('Success:', token);
			}
		});
	};
	</script>

Widget sizes

The Turnstile widget can have two different fixed sizes or a flexible width size when using the Managed or Non-Interactive modes.

Size Width Height Use case
Normal 300px 65px Standard implementation
Flexible 100% (min: 300px) 65px Responsive design
Compact 150px 140px Space-constrained layouts
Normal size (default)
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Flexible size
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-size="flexible"></div>
Compact size
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-size="compact"></div>
Normal size (default)
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>'
	});
Flexible size
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		size: 'flexible'
	});
Compact size
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		size: 'compact'
	});

Theme options

Customize the widget's visual appearance to match your website's design.

Auto theme (default)
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Light theme
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-theme="light"></div>
Dark theme
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-theme="dark"></div>
Auto theme (default)
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>'
	});
Light theme
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		theme: 'light'
	});
Dark theme
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		theme: 'dark'
	});

Appearance modes

Control when the widget becomes visible to visitors using the appearance mode.

Always visible (default)
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Visible only after challenge begins
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-appearance="execute"></div>
Visible only when interaction is needed
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-appearance="interaction-only"></div>
Always visible (default)
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>'
	});
Visible only after challenge begins
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		appearance: 'execute'
	});
Visible only when interaction is needed
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		appearance: 'interaction-only'
	});

Execution modes

Control when the challenge runs and a token is generated.

Auto execution (default)
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Manual execution
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-execution="execute"></div>
Auto execution (default)
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>'
	});
Manual execution
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		execution: 'execute'
	});
Execute the challenge later
	turnstile.execute('#widget-container');

Language configuration

Set the language for the widget interface.

Auto language (default)
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Specific language
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-language="es"></div>
Language and country
	<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-language="en-US"></div>
Auto language (default)
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>'
	});
Specific language
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		language: 'es'
	});

Callback configuration

Handle widget events with callbacks.

The success callback receives a token that must be validated on your server using the Siteverify API. Tokens are single-use and expire after 300 seconds (five minutes).

	<div class="cf-turnstile"
		data-sitekey="<YOUR-SITE-KEY>"
		data-callback="onSuccess"
		data-error-callback="onError"
		data-expired-callback="onExpired"
		data-timeout-callback="onTimeout"></div>
	<script>
	function onSuccess(token) {
	console.log('Challenge Success:', token);
	}
	function onError(errorCode) {
	console.log('Challenge Error:', errorCode);
	}
	function onExpired() {
	console.log('Token expired');
	}
	function onTimeout() {
	console.log('Challenge timed out');
	}
	</script>
	turnstile.render('#widget-container', {
		sitekey: '<YOUR-SITE-KEY>',
		callback: function(token) {
			console.log('Challenge Success:', token);
		},
		'error-callback': function(errorCode) {
			console.log('Challenge Error:', errorCode);
		},
		'expired-callback': function() {
			console.log('Token expired');
		},
		'timeout-callback': function() {
			console.log('Challenge timed out');
		}
	});

Best practices


Advanced configuration options

Retry behavior

Control how Turnstile handles failed challenges.

Auto retry (default)
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Disable retry
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-retry="never"></div>
Custom retry interval (8000ms default)
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-retry-interval="0000"></div>

Refresh behavior

Control how Turnstile handles token expiration and interactive timeouts.

Benefits

Different strategies can be used for token expiration versus interactive timeouts based on your visitor experience requirements.

Auto refresh expired tokens (default)
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>
Manual refresh
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-refresh-expired="manual"></div>
Auto refresh timeouts (default for Managed mode)
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-refresh-timeout="auto"></div>

Custom data

Add custom identifiers and data to your challenges.

Use cases

Add custom action identifier
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-action="login"></div>
Add custom data payload
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-cdata="user-cdata"></div>

Form integration

Configure how Turnstile integrates with HTML forms.

When enabled, Turnstile automatically creates a hidden <input> element with the verification token. This gets submitted along with your other form data, making server-side validation straightforward.

Benefits

Custom response field name
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-response-field-name="turnstile-token"></div>
Disable response field
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-response-field="false"></div>

Complete configuration reference

JavaScript Render Parameters Data Attribute Description
sitekey data-sitekey Every widget has a sitekey. This sitekey is associated with the corresponding widget configuration and is created upon the widget creation.
action data-action A customer value that can be used to differentiate widgets under the same sitekey in analytics and which is returned upon validation. This can only contain up to 32 alphanumeric characters including _ and -.
cData data-cdata A customer payload that can be used to attach customer data to the challenge throughout its issuance and which is returned upon validation. This can only contain up to 255 alphanumeric characters including _ and -.
callback data-callback A JavaScript callback invoked upon success of the challenge. The callback is passed a token that can be validated.
error-callback data-error-callback A JavaScript callback invoked when there is an error (e.g. network error or the challenge failed). Refer to Client-side errors.
execution data-execution Execution controls when to obtain the token of the widget and can be on render (default) or on execute. Refer to Execution Modes for more information.
expired-callback data-expired-callback A JavaScript callback invoked when the token expires and does not reset the widget.
before-interactive-callback data-before-interactive-callback A JavaScript callback invoked before the challenge enters interactive mode.
after-interactive-callback data-after-interactive-callback A JavaScript callback invoked when challenge has left interactive mode.
unsupported-callback data-unsupported-callback A JavaScript callback invoked when a given client/browser is not supported by Turnstile.
theme data-theme The widget theme. Can take the following values: light, dark, auto.

The default is auto, which respects the visitor preference. This can be forced to light or dark by setting the theme accordingly.
language data-language Language to display, must be either: auto (default) to use the language that the visitor has chosen, or an ISO 639-1 two-letter language code (e.g. en) or language and country code (e.g. en-US). Refer to the list of supported languages for more information.
tabindex data-tabindex The tabindex of Turnstile's iframe for accessibility purposes. The default value is 0.
timeout-callback data-timeout-callback A JavaScript callback invoked when the challenge presents an interactive challenge but was not solved within a given time. A callback will reset the widget to allow a visitor to solve the challenge again.
response-field data-response-field A boolean that controls if an input element with the response token is created, defaults to true.
response-field-name data-response-field-name Name of the input element, defaults to cf-turnstile-response.
size data-size The widget size. Can take the following values: normal, flexible, compact.
retry data-retry Controls whether the widget should automatically retry to obtain a token if it did not succeed. The default is auto, which will retry automatically. This can be set to never to disable retry on failure.
retry-interval data-retry-interval When retry is set to auto, retry-interval controls the time between retry attempts in milliseconds. Value must be a positive integer less than 900000, defaults to 8000.
refresh-expired data-refresh-expired Automatically refreshes the token when it expires. Can take auto, manual, or never, defaults to auto.
refresh-timeout data-refresh-timeout Controls whether the widget should automatically refresh upon entering an interactive challenge and observing a timeout. Can take auto (automatically refreshes upon encountering an interactive timeout), manual (prompts the visitor to manually refresh) or never (will show a timeout), defaults to auto. Only applies to widgets of Managed mode.
appearance data-appearance Appearance controls when the widget is visible. It can be always (default), execute, or interaction-only. Refer to Appearance modes for more information.
feedback-enabled data-feedback-enabled Allows Cloudflare to gather visitor feedback upon widget failure. It can be true (default) or false.
offlabel-show-privacy data-offlabel-show-privacy Displays privacy link for unbranded Turnstile widgets. Can be true (default) or false.
offlabel-show-help data-offlabel-show-help Displays help link for unbranded Turnstile widgets. Can be true (default) or false.

Examples

Responsive design widget
<div style="max-width: 500px;">
  <div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-size="flexible" data-theme="auto"></div>
</div>
Mobile-optimized compact widget
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-size="compact" data-theme="light" data-language="en">
</div>