An unofficial Cloudflare Turnstile library for Python. codeberg.org/OtakuEmily/cloudstile
async cloudflare sdk turnstile mirror
0

Configure Feed

Select the types of activity you want to include in your feed.

Python 100.0%
1 1 0

Clone this repository

https://tangled.org/emily.kemonomimi.moe/cloudstile https://tangled.org/did:plc:wnkeusekxaormakugosuajkb
git@tangled.org:emily.kemonomimi.moe/cloudstile git@tangled.org:did:plc:wnkeusekxaormakugosuajkb

For self-hosted knots, clone URLs may differ based on your setup.



README.md

Cloudstile#

An unofficial Cloudflare Turnstile library with both asynchronous and synchronous support out of the box.

📥 Installation#

Cloudstile is available for download via PyPI. To install it, simply do:

pip install cloudstile

🎭 Example#

Here are some basic examples of how to validate a user's turnstile token.

WARNING

These examples expect the user's IP to be transparent. If you're using something like Cloudflare's proxy service, then you'll need to access the corresponding header for your use case.

🍷 Quart (Asynchronous)#

from quart import Quart, request, jsonify
from cloudstile import AsyncTurnstile

app = Quart(__name__)
turnstile = AsyncTurnstile(token="...")

@app.route("/submit", methods=["POST"])
async def submit():

    body = await request.form

    response = await turnstile.validate(
        body.get("cf-turnstile-response", "..."),
        request.remote_addr,
    )

    return jsonify(response.model_dump()) # <- Response is a pydantic object

🏃‍♀️ FastAPI (Asynchronous)#

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from cloudstile import AsyncTurnstile

app = FastAPI()
turnstile = AsyncTurnstile(token="...")

@app.post("/submit")
async def submit(request: Request):

    body = await request.form()

    response = await turnstile.validate(
        body.get("cf-turnstile-response", "..."),
        request.client.host,
    )

    return JSONResponse(response.model_dump()) # <- Response is a pydantic object

🦥 Flask (Synchronous)#

from flask import Flask, request, jsonify
from cloudstile import SyncTurnstile

app = Flask(__name__)
turnstile = SyncTurnstile(token="...")

@app.route("/submit", methods=["POST"])
def submit():

    body = request.form

    response = turnstile.validate(
        body.get("cf-turnstile-response", "..."),
        request.remote_addr,
    )

    return jsonify(response.model_dump()) # <- Response is a pydantic object