Embed the widget
Learn how to add the Turnstile widget to your webpage using implicit or explicit rendering methods.
Turnstile offers two ways to add widgets to your page. Implicit rendering automatically scans your HTML for widget containers when the page loads. Explicit rendering gives you programmatic control to create widgets at any time using JavaScript. Use implicit rendering for static pages where forms exist at page load. Use explicit rendering for dynamic content and single-page applications (SPAs) where forms are created after the initial page load.
| Feature | Implicit rendering | Explicit rendering |
|---|---|---|
| Ease of setup | Simple, minimal code | Requires additional JavaScript |
| Control over timing | Renders automatically on page load | Full control over rendering timing |
| Use cases | Static content | Dynamic or interactive content |
| Customization | Limited to HTML attributes | Extensive via JavaScript API |
Before you begin, you must have:
- A Cloudflare account
- A Turnstile widget with a sitekey
- Access to edit your website's HTML
- Basic knowledge of HTML and JavaScript
- Page load: The Turnstile script loads and scans for elements or waits for programmatic calls.
- Widget rendering: Widgets are created and begin running challenges.
- Token generation: When a challenge is completed, a token is generated.
- Form integration: The token is made available via callbacks or hidden form fields.
- Server validation: Your server receives the token and validates it using the Siteverify API.
Implicit rendering automatically scans your HTML for elements with the cf-turnstile class and renders widgets without additional JavaScript code. This set up is ideal for static pages where you want the widget to load immediately when the page loads.
Cloudflare recommends using implicit rendering on the following scenarios:
- You have simple implementations and want a quick integration.
- You have static websites with straightforward forms.
- You want widgets to appear immediately on pageload.
- You do not need programmatic control of the widget.
Include the Turnstile Script: Add the Turnstile JavaScript API to your HTML file within the <head> section or just before the closing </body> tag.
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>Add resource hints to improve loading performance by establishing early connections to Cloudflare servers. Place this <link> tag in your HTML <head> section before the Turnstile script.
<link rel="preconnect" href="https://challenges.cloudflare.com" />Add widget containers where you want the challenges to appear on your website.
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>Customize your widgets using data attributes. Insert a div element where you want the widget to appear.
<div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>" data-theme="light" data-size="normal" data-callback="onSuccess"></div>Once a challenge has been solved, a token is passed to the success callback. This token must be validated against our Siteverify endpoint.
Basic login form
Turnstile is often used to protect forms on websites such as login forms or contact forms. You can embed the widget within your <form> tag.
<!DOCTYPE html><html><head> <title>Login Form</title> <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script></head><body> <form action="/login" method="POST"> <input type="text" name="username" placeholder="Username" required /> <input type="password" name="password" placeholder="Password" required />
<!-- Turnstile widget with basic configuration --> <div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div> <button type="submit">Log in</button> </form>
</body></html>