Python 3.8+ client library for the Capsolver captcha-solving API. Single-package src/ layout, dual sync (requests) / async (aiohttp) execution, msgspec for serialization, tenacity for async retries.
./
├── src/python3_capsolver/ # Main library
│ ├── core/ # Base classes, instruments, serializers, enums
│ ├── control.py # Raw API: get_balance, create_task, get_task_result
│ ├── recaptcha.py # ReCaptcha V2/V3/Enterprise
│ ├── cloudflare.py # Cloudflare Turnstile/Challenge
│ └── *.py # Other captcha services (see package AGENTS.md)
├── tests/ # Pytest suite mirroring source structure
│ ├── conftest.py # BaseTest class, fixtures, rate-limiting delays
│ └── test_*.py # One file per service + test_core.py + test_instrument.py
├── docs/ # Sphinx documentation (make doc)
├── ARCHITECTURE.md # Layered architecture, data flow, invariants
├── pyproject.toml # Build, deps, black/isort/pytest config
└── Makefile # make tests, make refactor, make build, make doc
Four layers with strict dependency direction (top → bottom only):
- Service layer (
src/python3_capsolver/*.py) — thin wrappers inheritingCaptchaParams, zero HTTP logic - Base layer (
core/base.py) —CaptchaParamsmerges payloads, delegates to instruments - Instrument layer (
core/*_instrument.py) —SIOCaptchaInstrument(sync) andAIOCaptchaInstrument(async) handle all HTTP, retries, and polling - Support layer (
core/serializer.py,core/enum.py,core/const.py) —msgspec.Structclasses, enums, constants
Forbidden: service files importing requests/aiohttp directly; support layer depending on upper layers.
- Every new captcha service must inherit
CaptchaParams, providecaptcha_handler()+aio_captcha_handler(), and register its type inCaptchaTypeEnm - All serialization must use
msgspec.Struct— never rawjsonmodule - Dual sync/async is mandatory for any new instrument or service
- Service classes are thin: only
__init__with captcha-type-specific params; all HTTP goes through instruments - Context manager support (
with/async with) is required on all service classes
make tests # pytest + coverage (HTML + XML)
make refactor # autoflake + black + isort on src/ and tests/
make lint # autoflake --check, black --check, isort --check-only
uv run pytest tests/ -k <name> # run specific testsTests require the API_KEY environment variable. Rate-limiting fixtures (delay_func 1s, delay_class 2s) prevent API throttling.
ARCHITECTURE.md— full system map, data flow, invariantssrc/python3_capsolver/AGENTS.md— service-level conventionssrc/python3_capsolver/core/AGENTS.md— core module internalstests/AGENTS.md— test patterns and fixtures
- Empty
__init__.pyfiles: users import via full path (from python3_capsolver.recaptcha import ReCaptcha), never from top-level package AGENTS.mdin package dirs: these ship with the wheel unless excluded inpyproject.toml— do not add more insidesrc/control.pyis the largest file (~431 lines) and provides direct API access without the captcha-handling abstraction- Toolchain is
uv: useuv run,uv sync,uv build— not barepiporpytest captcha_instrument.pyis ~221 lines: contains bothCaptchaInstrumentBaseandFileInstrument; edits here affect all services