Skip to main content

CrewAI tool for solving captchas via the 2Captcha service (reCAPTCHA v2/v3, Cloudflare Turnstile, image, GeeTest).

Project description

crewai-twocaptcha

PyPI Python CI License: MIT

Languages: English · Русский

TwoCaptchaSolverTool — a CrewAI tool that solves captchas through the 2Captcha service and returns the solution token (e.g. g-recaptcha-response) to your agent.

Supported captcha types

captcha_type 2Captcha method Required input fields Verified end-to-end
recaptcha_v2 userrecaptcha website_url, sitekey
recaptcha_v3 userrecaptcha + version=v3 website_url, sitekey, action (optional min_score)
turnstile turnstile website_url, sitekey (optional action)
image base64 body (base64-encoded image)
geetest geetest website_url, gt, challenge unit-tested; live requires fresh challenge

End-to-end verification is performed against the official 2Captcha demo pages; reproduce it yourself with scripts/live_check.py (see below).

Installation

pip install crewai-twocaptcha

Or with uv:

uv add crewai-twocaptcha

Quick example

Set your 2Captcha API key:

export TWOCAPTCHA_API_KEY="your-2captcha-api-key"

Then use it like any other CrewAI tool:

from crewai_twocaptcha import TwoCaptchaSolverTool

tool = TwoCaptchaSolverTool()

token = tool.run(
    captcha_type="recaptcha_v2",
    website_url="https://example.com/login",
    sitekey="6Lc_aCMTAAAAABx7u2N0D1XVhC2VB3b7C3oFqOAx",
)

print(token)  # -> g-recaptcha-response value

Arguments

Constructor

  • api_key (str, optional): 2Captcha API key. Falls back to the TWOCAPTCHA_API_KEY environment variable.
  • config (TwoCaptchaConfig | dict, optional): polling/timeouts override.

Config fields (with defaults):

Field Default Description
poll_interval 10 Seconds between result polls
max_attempts 30 Maximum number of polls (≈5 min total by default)
timeout 20 Per-request HTTP timeout, seconds
api_base https://2captcha.com Base API URL

Environment variables

  • TWOCAPTCHA_API_KEY — your 2Captcha API key.

Examples per captcha type

reCAPTCHA v2

tool.run(
    captcha_type="recaptcha_v2",
    website_url="https://example.com/login",
    sitekey="6Lc_aCMTAAAAABx7u2N0D1XVhC2VB3b7C3oFqOAx",
)

reCAPTCHA v3

tool.run(
    captcha_type="recaptcha_v3",
    website_url="https://example.com/submit",
    sitekey="6Lc_aCMTAAAAABx7u2N0D1XVhC2VB3b7C3oFqOAx",
    action="submit",
    min_score=0.7,
)

Cloudflare Turnstile

tool.run(
    captcha_type="turnstile",
    website_url="https://example.com/",
    sitekey="0x4AAAAAAAA1bXxxxxxxxxxx",
    action="login",  # optional
)

Image (text) captcha

import base64

with open("captcha.png", "rb") as fh:
    body = base64.b64encode(fh.read()).decode()

tool.run(captcha_type="image", body=body)

GeeTest

tool.run(
    captcha_type="geetest",
    website_url="https://example.com/",
    gt="f2ae6cadcf7886856696502e1d55e00c",
    challenge="12345678abcdefghij",
)

Using inside a CrewAI agent

from crewai import Agent, Crew, Task
from crewai_twocaptcha import TwoCaptchaSolverTool

solver = TwoCaptchaSolverTool()

scraper = Agent(
    role="Web Scraper",
    goal="Log into protected pages that require captcha solving.",
    backstory="An expert at automating captcha-gated workflows.",
    tools=[solver],
)

task = Task(
    description=(
        "Solve the reCAPTCHA on https://example.com/login with sitekey "
        "6Lc_aCMTAAAAABx7u2N0D1XVhC2VB3b7C3oFqOAx and return the token."
    ),
    expected_output="The g-recaptcha-response token as a plain string.",
    agent=scraper,
)

Crew(agents=[scraper], tasks=[task]).kickoff()

Error handling

The tool raises exceptions instead of returning error strings (which would otherwise be indistinguishable from a real token for the agent):

  • ValueError — missing TWOCAPTCHA_API_KEY.
  • pydantic.ValidationError — the input doesn't match the selected captcha_type (e.g. recaptcha_v3 without action).
  • crewai_twocaptcha.TwoCaptchaError — the 2Captcha API returned an error (ERROR_WRONG_USER_KEY, ERROR_CAPTCHA_UNSOLVABLE, …) or we exceeded config.max_attempts * config.poll_interval seconds.
  • requests.HTTPError — non-2xx HTTP response from the API.

Reproducing the end-to-end check

The repository ships with scripts/live_check.py, which runs every captcha type against 2Captcha's official demo pages:

export TWOCAPTCHA_API_KEY="your-key"
python scripts/live_check.py

Total cost is roughly $0.01–0.02 per full run. Use --no-image to skip the image case or --geetest GT CHALLENGE to test GeeTest with a fresh challenge copied from your browser DevTools.

Links

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

crewai_twocaptcha-0.1.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

crewai_twocaptcha-0.1.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file crewai_twocaptcha-0.1.1.tar.gz.

File metadata

  • Download URL: crewai_twocaptcha-0.1.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for crewai_twocaptcha-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7adf945a6f83917c8eb1307b2ccd629efaea9abb4858126cd21c3d4bb0744992
MD5 53d66fe28e02232b26518e42991a0042
BLAKE2b-256 e0e697a259c9289a930693d1cef67dcb3763eef315cf1128ffcbe86b649e76e3

See more details on using hashes here.

File details

Details for the file crewai_twocaptcha-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for crewai_twocaptcha-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7c6bedbfc6d9215f7bb54e01deb00346f302ce81626571cfa483e4a95a99f6d6
MD5 177e0edb87d0f5be58202ff31a8685ed
BLAKE2b-256 8030d3ead59688c1a21ca9f80d09597d09fe77f2b50943d1ef5da04b771b9279

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