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 API
  • 📦 Lightweight - Only requires httpx

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyturnstile-0.3.0.tar.gz
  • Upload date:
  • Size: 8.9 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.3.0.tar.gz
Algorithm Hash digest
SHA256 5c900de870886d42c2338d578b98110f5b8d1ecaca67e4219a2f6bf1da09bf92
MD5 d7f96be6401f2f9bf789b239190a60b1
BLAKE2b-256 5ae92fe713814d7684635a1c75ba930b79f8ad52e24b20c5b8506913c52846dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyturnstile-0.3.0-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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95a23619ee520512e285ef38d9aab22c11bb6270ce1aff0489da22d627747067
MD5 dc27067b3a9b000494bb79ca5afbf878
BLAKE2b-256 c6c1939ecbad0e9e1bf86169d0b5bfc59cd8f765b109298d02492d5c8f416e9f

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