Python
Solve captchas from Python.
The helper below uses the requests library.
It submits a task with POST /createTask, polls POST /getTaskResult on a fixed
interval until the status is ready, and raises a RuntimeError if the API reports an
error or the timeout is exceeded. The example solves a Cloudflare Turnstile challenge.
Keep your key out of source control
Read the API key from an environment variable rather than hardcoding it. Set it once
with export NOCAPTCHAAI_KEY="your-key" (get one at
nocaptchaai.com/manage).
import os
import time
import requests
BASE_URL = "https://api.nocaptchaai.com"
API_KEY = os.environ["NOCAPTCHAAI_KEY"]
def solve(task: dict, *, poll_interval: float = 3.0, timeout: float = 120.0) -> dict:
"""Create a task, poll until it is ready, and return the solution.
Raises RuntimeError on an API error or if the timeout is reached.
"""
created = requests.post(
f"{BASE_URL}/createTask",
json={"clientKey": API_KEY, "task": task},
timeout=30,
).json()
if created.get("errorId"):
raise RuntimeError(f"createTask failed: {created}")
task_id = created["taskId"]
deadline = time.monotonic() + timeout
while True:
result = requests.post(
f"{BASE_URL}/getTaskResult",
json={"clientKey": API_KEY, "taskId": task_id},
timeout=30,
).json()
status = result.get("status")
if status == "ready":
return result["solution"]
if status == "failed" or result.get("errorId"):
raise RuntimeError(f"task failed: {result}")
if time.monotonic() >= deadline:
raise RuntimeError(f"timed out after {timeout}s waiting for task {task_id}")
time.sleep(poll_interval)
if __name__ == "__main__":
solution = solve(
{
"type": "AntiTurnstileTask",
"websiteURL": "https://example.com",
"websiteKey": "0x4AAAAAAA...",
}
)
print(solution)Call solve() with any task object — swap the type, websiteURL, and websiteKey
for the captcha you need to solve. The returned solution dict contains the token (and
any other fields) you submit back to the target site.