The easiest way to quickly integrate the 2Captcha captcha-solving service into your code and automate the solving of any type of captcha.
Examples of API requests for different captcha types are available on the Python captcha solver page.
URL of your web server that receives the captcha recognition result. The URL should be first registered in pingback settings of your account
defaultTimeout
120
Polling timeout in seconds for all captcha types except reCAPTCHA. Defines how long the module tries to get the answer from the res.php API endpoint
recaptchaTimeout
600
Polling timeout for reCAPTCHA in seconds. Defines how long the module tries to get the answer from the res.php API endpoint
pollingInterval
10
Interval in seconds between requests to the res.php API endpoint. Setting values less than 5 seconds is not recommended
extendedResponse
None
Set to True to get the response with additional fields or in more practical format (enables JSON response from res.php API endpoint). Suitable for ClickCaptcha, Canvas
[!IMPORTANT]
Once callback is defined for the TwoCaptcha instance, all methods return only the captcha ID and DO NOT poll the API to get the result. The result will be sent to the callback URL.
To bypass a normal captcha (distorted text on an image) use the following method. This method can also be used to recognize any text in an image.
result = solver.normal('path/to/captcha.jpg', param1=..., ...)
# OR
result = solver.normal('https://site-with-captcha.com/path/to/captcha.jpg', param1=..., ...)
Use the following method to bypass an audio captcha (mp3 formats only).
You must provide the language as lang = 'en'. Supported languages are "en", "ru", "de", "el", "pt", "fr".
result = solver.audio('path/to/captcha.mp3', lang = 'lang', param1=..., ...)
# OR
result = solver.audio('https://site-with-captcha.com/path/to/captcha.mp3', lang = 'lang', param1=..., ...)
Use this method to solve the Lemin captcha. Returns JSON with an answer containing the following values: answer, challenge_id.
result = solver.lemin(captcha_id='CROPPED_1abcd2f_a1234b567c890d12ef3a456bc78d901d',
div_id='lemin-cropped-captcha',
url='https://www.site.com/page/',
param1=..., ...)
Yandex Smart
Use this method to solve Yandex Smart Captcha. Returns JSON with the token.
result = solver.yandex_smart(sitekey='0x1AAAAh45AAAAkg0s2VIOD34y5hy4h4h',
url='http://mysite.com/',
proxy={'type': 'HTTPS', 'uri': 'login:password@IP_address:PORT'},
userAgent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36')
Use this method to solve Cloudflare Turnstile. Returns JSON with the token.
result = solver.turnstile(sitekey='0x1AAAAAAAAkg0s2VIOD34y5',
url='http://mysite.com/',
data='foo',
pagedata='bar',
action='challenge',
useragent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36')
Use this method to solve Amazon WAF Captcha also known as AWS WAF Captcha is a part of Intelligent threat mitigation for Amazon AWS. Returns JSON with the token.
result = solver.amazon_waf(sitekey='0x1AAAAAAAAkg0s2VIOD34y5',
iv='CgAHbCe2GgAAAAAj',
context='9BUgmlm48F92WUoqv97a49ZuEJJ50TCk9MVr3C7WMtQ0X6flVbufM4n8mjFLmbLVAPgaQ1Jydeaja94iAS49ljb+sUNLoukWedAQZKrlY4RdbOOzvcFqmD/ZepQFS9N5w15Exr4VwnVq+HIxTsDJwRviElWCdzKDebN/mk8/eX2n7qJi5G3Riq0tdQw9+C4diFZU5E97RSeahejOAAJTDqduqW6uLw9NsjJBkDRBlRjxjn5CaMMo5pYOxYbGrM8Un1JH5DMOLeXbq1xWbC17YSEoM1cRFfTgOoc+VpCe36Ai9Kc='
url='https://non-existent-example.execute-api.us-east-1.amazonaws.com/latest'
param1=..., ...)
The grid method was originally called the Old reCAPTCHA V2 method. The method can be used to bypass any type of captcha where you can apply a grid on an image and click specific grid boxes. Returns numbers of boxes.
result = solver.grid('path/to/captcha.jpg', param1=..., ...)
[!IMPORTANT]
To successfully use the received token, the captcha widget must not be loaded on the page. To do this, you need to abort request to /friendlycaptcha/...module.min.js on the page. When the captcha widget is already loaded on the page, there is a high probability that the received token will not work.
result = solver.friendly_captcha(sitekey='FCMGEMUD2KTDSQ5H',
url='https://friendlycaptcha.com/demo',
param1=..., ...)
[!IMPORTANT]
To solve the DataDome captcha, you must use a proxy. It is recommended to use residential proxies.
result = solver.datadome(captcha_url="https://geo.captcha-delivery.com/captcha/?initialCid=...",
pageurl="https://mysite.com/page/with/datadome",
userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
proxy={
'type': 'HTTP',
'uri': 'login:password@IP_address:PORT'
},
param1=..., ...)
Use this method to solve CyberSiARA. Returns a token.
result = solver.cybersiara(master_url_id='tpjOCKjjpdzv3d8Ub2E9COEWKt1vl1Mv',
pageurl='https://demo.mycybersiara.com/',
userAgent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
param1=..., ...)
Use this method to solve a captcha where one or more images need to be dragged onto specific positions on a background image.
body and each item in images can be a file path, a URL, or a Base64-encoded string.
Returns the standard result dictionary {'captchaId': 'TASK_ID', 'code': 'COORDINATES'}. The coordinates string in
result['code'] contains one entry per image from images, in the same order, separated by |. An image that
wasn't moved may be returned by the API as null.
result = solver.drag_and_drop(
body='path/to/background.jpg',
images=['path/to/image1.jpg', 'path/to/image2.jpg'],
)
coordinates = result['code']
The cookies from domains must be passed to the target website. If the solution contains headers.User-Agent,
use the same User-Agent for requests to the website.
Other methods
send / get_result
These methods can be used for manual captcha submission and answer polling. The send() method supports sending any captcha
type, to specify the captcha type you must send value method manually, for example method='recaptcha' for solving reCaptcha.
You can find the value of the method parameter in the API documentation.
Example for solving Normal captcha manually:
import time
. . . . .
id = solver.send(file='path/to/captcha.jpg')
time.sleep(20)
code = solver.get_result(id)
In case of an error, the captcha solver throws an exception. It's important to properly handle these cases. We recommend using try except to handle exceptions.
The list of all errors can be found in the API documentation.
try:
result = solver.text('If tomorrow is Saturday, what day is today?')
except ValidationException as e:
# invalid parameters passed
print(e)
except NetworkException as e:
# network error occurred
print(e)
except ApiException as e:
# api respond with error
print(e)
except TimeoutException as e:
# captcha is not solved so far
print(e)
Proxies
You can pass your proxy as an additional argument for the following methods: recaptcha, funcaptcha, geetest, geetest v4,
keycaptcha, capy puzzle, lemin, atbcaptcha, turnstile, amazon waf, mtcaptcha, friendly captcha, cutcaptcha, Tencent, DataDome, cybersiara,
Yandex Smart, VKCaptcha, CaptchaFox, Prosopo, Altcha, Binance, Yidun, Hunt, Alibaba, TSPD, Basilisk, Imperva (Incapsula).
The proxy will be forwarded to the API to solve the captcha.
To use the async version, just replace TwoCaptcha with AsyncTwoCaptcha:
import asyncio
from twocaptcha import AsyncTwoCaptcha
async def solve_captcha():
solver = AsyncTwoCaptcha('YOUR_API_KEY')
try:
recaptcha_result = await solver.recaptcha(...)
return recaptcha_result
except Exception as e:
print(e)
return None
if __name__ == '__main__':
result = asyncio.run(solve_captcha())
The AsyncTwoCaptcha class supports all the same methods and parameters as the synchronous TwoCaptcha class but operates asynchronously. Configuration is identical.
Solving Multiple Captchas in Parallel
One of the main advantages of using async support is the ability to solve multiple captchas concurrently:
async def solve_multiple_captchas():
solver = AsyncTwoCaptcha('YOUR_API_KEY')
# Start all tasks simultaneously
task1 = asyncio.create_task(solver.text('What color is the sky on a clear day?'))
task2 = asyncio.create_task(solver.text('What is 2+2?'))
task3 = asyncio.create_task(solver.text('Name of the planet we live on?'))
# Wait for all tasks to complete
results = await asyncio.gather(task1, task2, task3, return_exceptions=True)
return results
# This completes much faster than solving captchas sequentially
results = asyncio.run(solve_multiple_captchas())
Examples of solving all supported captcha types asynchronously are located in the examples/async directory directory.
Legacy Async Method
For backward compatibility, you can also use the traditional executor-based approach with the synchronous client:
import asyncio
import concurrent.futures
from twocaptcha import TwoCaptcha
API_KEY = "YOUR_API_KEY"
image = "data:image/png;base64,iVBORw0KGgoA..."
async def captchaSolver(image):
loop = asyncio.get_running_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
result = await loop.run_in_executor(pool, lambda: TwoCaptcha(API_KEY).normal(image))
return result
captcha_result = asyncio.run(captchaSolver(image))
Examples
Examples of solving all supported captcha types are located in the examples directory.
There are many ways to contribute, of which development is only one! Find your next job. Open positions: AI experts, scrapers, developers, technical support, and much more! 😍
License
The code in this repository is licensed under the MIT License. See the LICENSE file for more details.
Graphics and Trademarks
The graphics and trademarks included in this repository are not covered by the MIT License. Please contact support for permissions regarding the use of these materials.
Alternatives
Use instead of 2captcha-python: tools that do the same job, closest first.