Skip to main content

CaptchaLa Python SDK

English · 简体中文 · 日本語 · 한국어 · Español · Português (BR) · Deutsch

Server-side SDK for validating CaptchaLa tokens from Python. Sync and async clients, typed, with optional Django and Flask helpers.

Installation

pip install captchala             # sync client, no dependencies
pip install "captchala[async]"    # + AsyncClient (httpx)
pip install "captchala[django]"   # + Django helpers
pip install "captchala[flask]"    # + Flask helpers

Requires Python 3.9+. The sync client uses only the standard library.

Quick Start

from captchala import Client

# Reads CAPTCHALA_APP_KEY / CAPTCHALA_APP_SECRET when not passed.
captchala = Client("your_app_key", "your_app_secret")

result = captchala.validate(
    token,
    client_ip=user_ip,          # optional, recommended
    expected_action="login",    # optional: reject tokens issued for another action
)

if result.valid:
    ...  # Verification passed
else:
    print(result.error)  # e.g. "token_expired"

Async:

from captchala import AsyncClient

async with AsyncClient() as captchala:
    result = await captchala.validate(token, client_ip=user_ip)

API Reference

Client(app_key=None, app_secret=None, **options) / AsyncClient(...)

Option Default Description
app_key $CAPTCHALA_APP_KEY App Key (from dashboard)
app_secret $CAPTCHALA_APP_SECRET App Secret (from dashboard). Server-side only
timeout 5.0 Timeout per attempt, in seconds
max_retries 2 Retries after the first attempt (0 disables)
retry_backoff 0.2 Base delay for exponential backoff, in seconds
reject_client_only False Always reject client-only tokens, see below
headers None Extra headers sent with every request
transport urllib (sync) Custom transport callable, see Testing
http_client new httpx.AsyncClient (async) Share your own httpx client

Raises ConfigurationError when the key or secret is missing. Create one client and reuse it; Client is thread-safe. Close an AsyncClient with await client.aclose() or async with.

client.validate(token, **options) -> ValidateResult

Validate a pass token from the client SDK. It never raises for invalid tokens, API errors or network failures. Check result.valid and result.error.

Option Description
client_ip (optional, recommended) The end-user's IP from your inbound request. Used for additional risk checks
keep_token True = check without consuming the token (default False)
expected_action Action you expect, e.g. "login". A token issued for another action fails with action_mismatch
expected_hostname Hostname (web) or package / bundle id (native) you expect, as a string or a list. *.example.com matches any subdomain. Mismatch: hostname_mismatch
bind_uid, bind_account, bind_email, bind_phone, scene End-user identity, compared with the identity bound at issue (see binding_match)
result = captchala.validate(
    request.POST["captcha_token"],
    client_ip=ip,
    expected_action="login",
    expected_hostname=["www.example.com", "*.example.com"],
    bind_uid=str(user.id),
)

ValidateResult

Attribute Type Description
valid bool Whether validation passed. The only field to gate on
error str | None Error code, e.g. token_expired, action_mismatch, request_failed
message str | None Human-readable API message, for API-level errors
action str | None Business action of the token
uid str | None User ID bound via bind_uid at issue
challenge_id str | None Challenge ID
hostname str | None Where the challenge was solved (hostname or package id)
challenge_ts str | None Solve time, ISO 8601 UTC
risk_level str | None low / medium / high / critical. Informational
risk_labels tuple[str, ...] Coarse risk categories, e.g. proxy. Informational
captcha_args CaptchaArgs | None Solve-context echo: platform, user_ip, referer, pkg, solved_at, risk_score. Informational
user_ip str | None End-user IP recorded at solve time. Informational
binding / binding_match Identity bound at issue, and the per-field comparison
binding_mismatch bool True when any binding_match field is False
degraded / degraded_reason dg_ token issued while the quota is exhausted, see below
offline / client_only / warning Outage fallbacks, see Token Types
test bool Answer from a public test key. Reject it in production
http_status, retry_after, retryable Failure details
cause CaptchaLaError | None Typed error for API / network failures
raw dict | None The raw data object

Methods: raise_if_invalid() raises ValidationFailedError unless valid; to_dict().

Verifying bind_uid

If you issued the server token with bind_uid="user_42", compare the result with the user you expect:

result = captchala.validate(token)
if result.valid and result.uid != expected_user_id:
    ...  # pass token was issued for a different user: reject

Or send the identity at validate and check result.binding_mismatch.

client.issue_server_token(action, **options) -> IssueResult

Mint a one-time sct_ server token. Hand it to the client SDK as serverToken.

Option Description
bind_ip End-user IP. A challenge started from another network may be rejected
ttl Lifetime in seconds (server default 300, max 900)
max_uses How many pass flows the token may be used for (server default 10)
bind_uid Your user ID; echoed by validate as uid
bind_account, bind_email, bind_phone, scene End-user identity
bind_device_id, bind_fingerprint Bind to a device id / browser fingerprint
issue = captchala.issue_server_token("login", bind_uid=str(user.id), ttl=300)
if not issue.ok:
    return {"error": issue.error}, 503   # rate_limit_exceeded, ...
return {"server_token": issue.token}

IssueResult: ok, token, expires_in, issued_at, error, message, http_status, retry_after, retryable, cause.

client.moderation_check(input, user_id=None) / client.moderation_text(text, user_id=None)

Multi-modal content moderation. input is a list of OpenAI-compatible items; use the text_item() / image_url_item() helpers.

from captchala import text_item, image_url_item

m = captchala.moderation_check([text_item(comment), image_url_item(upload_url)], user_id=str(user.id))
if m.ok and m.flagged and m.has_category("violence", "csam"):
    ...  # hard block

t = captchala.moderation_text("user comment here")

ModerationResult: ok, flagged, categories, category_scores, content_type, raw, error, message, has_category(*names). Categories depend on the model, so iterate them defensively.

user_message(code)

A short, user-safe English message for an error code, for form errors ("CAPTCHA expired or was already used. Please refresh and try again."). Never show raw codes to end users. Unknown codes fall back to a generic message.

Errors, retries and timeouts

Methods return a result and do not raise for verification, API or network failures. result.error is a short code. result.cause holds a typed exception when the failure came from the transport or the API:

Class When
NetworkError (connect_failed) DNS failure, connection refused / reset
RequestTimeoutError (subclass of NetworkError) No answer within timeout
AuthenticationError Wrong or missing app key / secret
RateLimitError Rate limit or quota; see retry_after
InvalidResponseError Body was not the expected JSON
APIError Any other API error (base class of the three above)
ValidationFailedError Raised by result.raise_if_invalid()

All derive from CaptchaLaError and carry code.

Retries use exponential backoff with jitter. Connection failures and HTTP 502 / 503 are always retried. Timeouts, connection resets and HTTP 504 are retried for validate and issue_server_token, but not for moderation, where the content may already be processing. Rate-limit answers (HTTP 429) are not retried. Note that a pass token is single-use: if a first attempt reached the API and was consumed before the connection dropped, the retry reports token_already_used. It fails closed.

Django

# settings.py
CAPTCHALA_APP_KEY = "..."
CAPTCHALA_APP_SECRET = "..."
# optional: CAPTCHALA_TIMEOUT, CAPTCHALA_REJECT_CLIENT_ONLY

Form field. cleaned_data holds the ValidateResult; errors use user_message() texts and the API error code as the ValidationError code:

from django import forms
from captchala.contrib.django import CaptchaLaField, CaptchaLaFormMixin

class LoginForm(CaptchaLaFormMixin, forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)
    captcha_token = CaptchaLaField(expected_action="login")

form = LoginForm(request.POST, request=request)   # request= forwards the client IP
if form.is_valid():
    result = form.cleaned_data["captcha_token"]

View decorator, for sync and async views:

from captchala.contrib.django import captchala_required

@captchala_required(action="login")
def login_view(request):
    request.captchala  # ValidateResult
    ...

On failure it returns HTTP 400 {"error": "captcha_failed", "code": "<error code>"}, or whatever on_failure(request, result) returns. Other options: hostname, fields, allow_degraded, client. verify_request(request, ...) returns the result without a decorator.

Flask

from flask import Flask, g
from captchala.contrib.flask import CaptchaLa

app = Flask(__name__)
app.config.update(CAPTCHALA_APP_KEY="...", CAPTCHALA_APP_SECRET="...")
captcha = CaptchaLa(app)   # or captcha.init_app(app)

@app.post("/login")
@captcha.required(action="login")
def login():
    g.captchala  # ValidateResult
    ...

On failure it returns HTTP 400 {"error": "captcha_failed", "code": "<error code>"}, or on_failure(result). captcha.verify(action=...) validates the current request yourself. Async views are supported with flask[async].

The helpers read the token from the captcha_token (or captchala_token) form / JSON field, then from the X-Captcha-Token header. They pass the end-user IP from CF-Connecting-IP, the first X-Forwarded-For entry, X-Real-IP or the remote address. Anyone can set these headers when your app is not behind a proxy that overwrites them. The IP is only an extra risk signal.

Token Types

Prefix Source Handling
pt_ Main API Verified by validate()
offline_ Backup service, during a service disruption Verified automatically against the backup service; offline=True
dg_ Main API, degraded (quota exhausted) valid is always False; check degraded and decide yourself
client_ Client-only placeholder (native SDKs) See below
co_ Client-only placeholder (web SDK) See below

Degraded tokens

When an app's quota is exhausted the client SDK completes with a dg_ token so your users are not blocked. valid stays False. To keep the flow open while you upgrade:

if result.valid or result.degraded:
    ...  # allow

The Django / Flask helpers do this with allow_degraded=True.

Client-only tokens (client_ / co_)

The client SDK mints these only when it could reach neither the API nor the backup service. They carry no signature, so the SDK accepts one only if your server cannot reach the API either:

  1. It probes the API (2 s timeout). Any HTTP answer means there is no outage: valid=False, client_only=True, error="client_only_rejected_api_reachable".
  2. Otherwise it probes the backup service the same way.
  3. Only when both are unreachable: valid=True, client_only=True, offline=True, with a warning.

Probe results are cached per process (30 s when reachable, 5 s when not), and concurrent validations share one probe. For sensitive actions (login, payment) refuse them even during an outage:

strict = Client(reject_client_only=True)
# client_ / co_ -> valid=False, error="client_only_rejected_by_policy"

Testing your integration

The API provides public test keys that always pass or always fail, without solving a CAPTCHA. See Test keys. Results from them have test == True. Never configure them in production.

In unit tests, inject a transport (sync) or an httpx mock transport (async):

import json
from captchala import Client

def fake(url, body, headers, timeout):
    return 200, {}, json.dumps({"code": 0, "data": {"valid": True, "action": "login"}}).encode()

client = Client("k", "s", transport=fake)
import httpx
from captchala import AsyncClient

mock = httpx.MockTransport(lambda req: httpx.Response(200, json={"code": 0, "data": {"valid": True}}))
client = AsyncClient("k", "s", http_client=httpx.AsyncClient(transport=mock))

Development

python -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/pytest
CAPTCHALA_APP_KEY=xxx CAPTCHALA_APP_SECRET=xxx .venv/bin/python scripts/live_smoke.py   # live check, no real challenge

License

MIT

Metadata

Release files for captchala 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for captchala 1.0.1
File Size Uploaded Provenance Actions
captchala-1.0.1.tar.gz 36.3 kB Without provenance Details for captchala-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for captchala 1.0.1
File Interpreter ABI Platform Actions
captchala-1.0.1-py3-none-any.whl Python 3 none any Details for captchala-1.0.1-py3-none-any.whl

Total release size: 66.4 kB

Release files / captchala-1.0.1.tar.gz

Download URL captchala-1.0.1.tar.gz
Size 36.3 kB
Tags Source
SHA-256 checksum
How to use checksums
833a9834a3bf0ae42b7d281038cfa53eb8a613ef1efac1b92d3ee2183c04a33b
BLAKE2b-256 checksum
How to use checksums
a1d67cf381076f3a2f4720fc3e40e39f9d96f76ef7faa83265d53428ec2161d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / captchala-1.0.1-py3-none-any.whl

Download URL captchala-1.0.1-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87e6e08fe19df48fe1757352714729d9aa0f64905d2673ceb292e52f253d5e62
BLAKE2b-256 checksum
How to use checksums
b029f5d2b62a2b72cabbdcdcc2ac5e8ba73cca01b428b7cf986fbd25b7f137a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for captchala 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page