2Captcha Python Client

2.0.6 · active · verified Tue Apr 14

The 2captcha-python library provides an easy-to-use Python module for integrating with the 2Captcha API. It simplifies the process of automating captcha solving for various types like reCAPTCHA, Arkose, GeeTest, and image-to-text captchas. Currently at version 2.0.6, the library maintains an active release cadence, regularly adding support for new captcha types and enhancing existing features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to solve a basic image-to-text captcha using the synchronous `TwoCaptcha` client. It emphasizes loading the API key from environment variables for security and includes comprehensive error handling for common API and network issues. Replace `'path/to/captcha.jpg'` with your captcha image file path or URL for a runnable example.

import os
from twocaptcha import TwoCaptcha
from twocaptcha.solver import ValidationException, NetworkException, ApiException

# Best practice: Load API key from environment variables
API_KEY = os.environ.get('TWO_CAPTCHA_API_KEY', 'YOUR_2CAPTCHA_API_KEY')

solver = TwoCaptcha(API_KEY)

try:
    # Example: Solving a normal image-to-text captcha
    # Replace 'path/to/captcha.jpg' with your captcha image file path or URL
    # You can also pass base64 encoded image data
    result = solver.normal('path/to/captcha.jpg')
    print(f"Captcha solved: {result['code']}")
    # The 'id' field can be used to report incorrect solutions later
    # print(f"Captcha ID: {result['id']}")
except ValidationException as e:
    print(f"Validation error: {e}")
except NetworkException as e:
    print(f"Network error: {e}")
except ApiException as e:
    print(f"API error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →