Skip to main content

A Python library for validating Cloudflare Turnstile tokens with async and sync support

Project description

PyTurnstile

Cloudflare Turnstile widget

A Python library for validating Cloudflare Turnstile tokens with both async and sync support.

Test Package version Supported Python versions Ruff License: MIT PRs are welcome

Features

  • 🔄 Async & Sync Support
  • 🚀 Simple & Intuitive API
  • ✅ Type-safe response handling
  • 🛡️ Enhanced security validation

What is PyTurnstile?

PyTurnstile simplifies Cloudflare Turnstile token validation. It handles all communication with Cloudflare's API.

Sequence diagram showing how PyTurnstile works

Learn more at: https://developers.cloudflare.com/turnstile/

Installation

Install the package using your preferred dependency manager.

uv

uv add pyturnstile

pip

pip install pyturnstile

Usage

💡 TIP

You can follow this documentation and create your own Turnstile secret key at the Cloudflare Turnstile dashboard.

Quick Start

PyTurnstile provides two ways to validate tokens:

1. Using the Turnstile class (Recommended)

from pyturnstile import Turnstile

turnstile = Turnstile(secret="your-secret-key")

response = await turnstile.async_validate(token="user-token-from-frontend")

# or validate synchronously
# response = turnstile.validate(token="user-token-from-frontend")

if response.success:
    print("✅ Token is valid!")

2. Using functions directly

from pyturnstile import validate, async_validate

response = await async_validate(
    token="user-token-from-frontend",
    secret="your-secret-key"
)

# or validate synchronously
# response = validate(
#     token="user-token-from-frontend",
#     secret="your-secret-key"
# )

if response.success:
    print("✅ Token is valid!")

Optional Parameters

ℹ️ NOTE

For more details on all available parameters, see the Cloudflare documentation

response = turnstile.validate(
    token="user-token",               # The token from the client-side widget
    idempotency_key="unique-uuid",    # Optional: UUID for retry protection
    expected_remoteip="203.0.113.1",  # Optional: The visitor's IP address that the challenge response must match
    expected_hostname="example.com",  # Optional: The hostname that the challenge response must match
    expected_action="submit_form",    # Optional: The action identifier that the challenge must match
    timeout=10                        # Optional: request timeout in seconds
)

Response Object

ℹ️ NOTE

For more details on all response fields, see the Cloudflare documentation

The TurnstileResponse object contains:

response.success                   # bool: Whether validation succeeded
response.error_codes               # list[TurnstileErrorCodes]: Error codes (if any)
response.challenge_ts              # str: ISO timestamp of challenge completion
response.hostname                  # str: Hostname where challenge was served
response.action                    # str: Custom action identifier
response.cdata                     # str: Custom data payload from client-side
response.metadata["ephemeral_id"]  # Device fingerprint ID (Enterprise only)

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.

License

Published under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyturnstile-0.5.0.tar.gz (368.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyturnstile-0.5.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pyturnstile-0.5.0.tar.gz.

File metadata

  • Download URL: pyturnstile-0.5.0.tar.gz
  • Upload date:
  • Size: 368.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyturnstile-0.5.0.tar.gz
Algorithm Hash digest
SHA256 9e41107f2c28944484a3497ee51cb83df697c3686ec79c0d02d527d2bbe49f8c
MD5 54af48e145866ea67e2e5d29fd02d306
BLAKE2b-256 86329ab488bf22cef9e8e40e668cab3459cd15ed980c0689f2aa4f86d0c3d9ae

See more details on using hashes here.

File details

Details for the file pyturnstile-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: pyturnstile-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyturnstile-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe766144c1ef5bc3eb72e2f77ebb552354dc6d84c219dacb9b6632929806f79d
MD5 9e4b27a4c05afcf954f40b50ff021700
BLAKE2b-256 34349542c85f14645a89737e082ce9490f9e3a480c07f7f32b476cfd9a002fd3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page