Skip to main content

Async-first, fully type-hinted, minimal captcha library with adapters for aiogram, FastAPI and Discord.

Project description

captchakit

Async-first, fully type-hinted captcha library for Python 3.10+. Zero runtime deps beyond Pillow. Drop-in adapters for FastAPI, aiogram and discord.py.

PyPI Python CI mypy: strict Ruff License: MIT


Why another one?

Feature lepture/captcha claptcha multicolorcaptcha captchakit
Async API
py.typed + mypy strict ⚠️
TTL & attempt tracking ✅ built-in
Pluggable storage Protocol
Framework adapters ✅ FastAPI · aiogram · discord.py
Runtime deps (core) +Pillow +Pillow +Pillow +Pillow

Install

pip install captchakit                  # core
pip install "captchakit[fastapi]"       # + FastAPI adapter
pip install "captchakit[aiogram]"       # + aiogram adapter
pip install "captchakit[discord]"       # + discord.py adapter
pip install "captchakit[redis]"         # + Redis storage backend
pip install "captchakit[metrics]"       # + Prometheus metrics adapter

See ROADMAP.md for what's next.

30-second example

import asyncio
from captchakit import (
    CaptchaManager, ImageRenderer, MemoryStorage, TextChallengeFactory,
)

async def main() -> None:
    manager = CaptchaManager(
        factory=TextChallengeFactory(length=5),
        renderer=ImageRenderer(),
        storage=MemoryStorage(),
        ttl=120.0,
        max_attempts=3,
    )
    challenge_id, png_bytes = await manager.issue()
    # ... show png_bytes to the user, receive their answer ...
    ok = await manager.verify(challenge_id, user_input="ABCDE")
    print("verified" if ok else "wrong answer, more attempts remain")

asyncio.run(main())

FastAPI in 10 lines

from fastapi import Depends, FastAPI
from captchakit import CaptchaManager, ImageRenderer, MathChallengeFactory, MemoryStorage
from captchakit.adapters.fastapi import captcha_router, verify_captcha

manager = CaptchaManager(MathChallengeFactory(), ImageRenderer(), MemoryStorage())
app = FastAPI()
app.include_router(captcha_router(manager, prefix="/captcha"))

@app.post("/register")
async def register(_: None = Depends(verify_captcha(manager))) -> dict[str, bool]:
    return {"ok": True}

Run the bundled demo:

uv run python -m uvicorn examples.fastapi_login:app --reload
# open http://127.0.0.1:8000

Design

┌─────────────────────────────────────────────────┐
│  Adapters (fastapi / aiogram / discord)         │
├─────────────────────────────────────────────────┤
│  CaptchaManager  (public facade)                │
├──────────────┬──────────────┬───────────────────┤
│  Challenge   │  Renderer    │  Storage          │
│  Text / Math │  Image (PIL) │  Memory / Redis   │
└──────────────┴──────────────┴───────────────────┘
  • Protocol-based, so you can drop in your own storage (Redis, Postgres, Memcached), your own renderer (SVG, WebP, audio), or your own challenge factory (wordlists, emoji grids, …).
  • Constant-time comparison via hmac.compare_digest.
  • Crypto-safe randomness via secrets for solution generation.
  • CPU-bound Pillow drawing offloaded to a worker thread via asyncio.to_thread, so the event loop never blocks.

Security scope

captchakit is a lightweight human-check, not a bot-farm-grade security layer. It is aimed at Telegram/Discord verification, simple FastAPI registration flows and similar use cases where you just want to raise the cost for casual spam.

For high-value forms (login, payment) use hCaptcha, Cloudflare Turnstile or reCAPTCHA Enterprise in addition. Combine captchakit with proper rate limiting at the edge of your application.

Roadmap

See ROADMAP.md for the full development plan.

Version Highlights
0.1 ✅ Core + Text/Math challenges + Image renderer + Memory storage + FastAPI
0.2 ✅ aiogram adapter + RedisStorage + EmojiGridChallenge
0.3 ✅ discord.py adapter + WordChallenge + docs site
0.4 ✅ AudioRenderer + Theme presets + i18n + Prometheus metrics
0.5 PostgresStorage + SVGRenderer + rate-limit protocol + Django adapter
1.0 Stable API, semver commitment

Contributing

Contributions welcome. Local development:

git clone https://github.com/akerem16/captchakit
cd captchakit
uv sync --all-extras
uv run ruff check .
uv run mypy
uv run pytest

License

MIT — see 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

captchakit-0.4.1.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

captchakit-0.4.1-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file captchakit-0.4.1.tar.gz.

File metadata

  • Download URL: captchakit-0.4.1.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for captchakit-0.4.1.tar.gz
Algorithm Hash digest
SHA256 2fd5a9d84a4fc442e31717c40c50da264276b122b20266c8efca8e6e7a59f303
MD5 c864a614daf95ff9cd6d658d332f3592
BLAKE2b-256 f5a25fee3ec1151973e62a25dadc92af30a664d4c99e649f8985b599a5cd205c

See more details on using hashes here.

Provenance

The following attestation bundles were made for captchakit-0.4.1.tar.gz:

Publisher: release.yml on akerem16/captchakit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file captchakit-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: captchakit-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 29.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for captchakit-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc2e32a07c5f97dcf2f8588d7760c3418d2003e9449b64bc1d7a513dcec6d179
MD5 0dd593ad5c8e887cbfcacdbb34f4224f
BLAKE2b-256 b69dc5c477b836c7ff4c3b487fc2b354171e36fd31e8d2e2c2096015e5b3916b

See more details on using hashes here.

Provenance

The following attestation bundles were made for captchakit-0.4.1-py3-none-any.whl:

Publisher: release.yml on akerem16/captchakit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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