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.1.tar.gz (368.3 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.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyturnstile-0.5.1.tar.gz
  • Upload date:
  • Size: 368.3 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.1.tar.gz
Algorithm Hash digest
SHA256 5aca85cf66a07f5618df9a94c4588df0a76f4917f7786379795ce353dd080575
MD5 7ad8127c02f81c0052d8587737f3a87b
BLAKE2b-256 0b74f496c49605f4c9f31acc83152ce94c1d6803da61ed2337ebd17876c69189

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyturnstile-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 393fc5380698040cecee1c9a6c41770f3f222c9a19c90e5228e254f819f89130
MD5 0f2ea069a6070c6e14bd7515159bb417
BLAKE2b-256 26e69f88ce9f647a382a9b7894aca21a84edac283bbb3aa18f2fd93814932fa9

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