API Tutorials

How to Solve Image CAPTCHA with Base64 API

Image CAPTCHAs show distorted text or numbers that users must type to prove they are human. CaptchaAI supports solving these via two methods: file upload and base64 encoding. The base64 method is ideal when you already have the image in memory (from a screenshot, page scraping, or API response) and want to avoid writing temporary files.

Send the base64-encoded image to in.php with method=base64, and CaptchaAI returns the recognized text.


What you need

Requirement Details
CaptchaAI API key captchaai.com
CAPTCHA image JPG, PNG, or GIF, 100 bytes to 100 KB
Python 3.7+ or Node.js 18+ For the examples below

How base64 submission works

Instead of uploading a file, you:

  1. Capture the CAPTCHA image from the page
  2. Encode it as a base64 string
  3. Send it to CaptchaAI with method=base64 and body=<base64_string>
  4. Poll for the OCR result

This avoids disk I/O and works well in serverless environments or headless browser workflows where the image is already in memory.


Python implementation

Submit and solve

import requests
import base64
import time

API_KEY = "YOUR_API_KEY"


def solve_image_captcha_base64(image_path):
    """Solve an image CAPTCHA using base64 encoding."""

    # Read and encode the image
    with open(image_path, "rb") as f:
        image_base64 = base64.b64encode(f.read()).decode("utf-8")

    # Submit task with base64 body
    submit = requests.post("https://ocr.captchaai.com/in.php", data={
        "key": API_KEY,
        "method": "base64",
        "body": image_base64,
        "json": 1
    }).json()

    if submit.get("status") != 1:
        raise RuntimeError(f"Submit error: {submit.get('request')}")

    task_id = submit["request"]
    print(f"Task ID: {task_id}")

    # Poll for result
    time.sleep(5)
    for _ in range(20):
        result = requests.get("https://ocr.captchaai.com/res.php", params={
            "key": API_KEY, "action": "get", "id": task_id, "json": 1
        }).json()

        if result.get("status") == 1:
            return result["request"]
        if result.get("request") != "CAPCHA_NOT_READY":
            raise RuntimeError(f"Solve error: {result['request']}")
        time.sleep(5)

    raise TimeoutError("Solve timed out")


text = solve_image_captcha_base64("captcha.png")
print(f"Solved text: {text}")

From a URL (download + encode)

def solve_captcha_from_url(image_url):
    """Download a CAPTCHA image and solve it via base64."""
    image_data = requests.get(image_url).content
    image_base64 = base64.b64encode(image_data).decode("utf-8")

    submit = requests.post("https://ocr.captchaai.com/in.php", data={
        "key": API_KEY,
        "method": "base64",
        "body": image_base64,
        "json": 1
    }).json()

    if submit.get("status") != 1:
        raise RuntimeError(f"Submit error: {submit.get('request')}")

    task_id = submit["request"]

    time.sleep(5)
    for _ in range(20):
        result = requests.get("https://ocr.captchaai.com/res.php", params={
            "key": API_KEY, "action": "get", "id": task_id, "json": 1
        }).json()

        if result.get("status") == 1:
            return result["request"]
        if result.get("request") != "CAPCHA_NOT_READY":
            raise RuntimeError(f"Solve error: {result['request']}")
        time.sleep(5)

    raise TimeoutError("Solve timed out")

Node.js implementation

const fs = require("fs");

const API_KEY = "YOUR_API_KEY";

function delay(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function solveImageCaptchaBase64(imagePath) {
  // Read and encode the image
  const imageBuffer = fs.readFileSync(imagePath);
  const imageBase64 = imageBuffer.toString("base64");

  // Submit task
  const submitRes = await fetch("https://ocr.captchaai.com/in.php", {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: new URLSearchParams({
      key: API_KEY,
      method: "base64",
      body: imageBase64,
      json: "1",
    }),
  });
  const submitData = await submitRes.json();

  if (submitData.status !== 1) {
    throw new Error(`Submit error: ${submitData.request}`);
  }

  const taskId = submitData.request;
  console.log(`Task ID: ${taskId}`);

  // Poll for result
  await delay(5000);

  for (let i = 0; i < 20; i++) {
    const pollRes = await fetch(
      `https://ocr.captchaai.com/res.php?${new URLSearchParams({
        key: API_KEY,
        action: "get",
        id: taskId,
        json: "1",
      })}`
    );
    const pollData = await pollRes.json();

    if (pollData.status === 1) {
      return pollData.request;
    }

    if (pollData.request !== "CAPCHA_NOT_READY") {
      throw new Error(`Solve error: ${pollData.request}`);
    }

    await delay(5000);
  }

  throw new Error("Solve timed out");
}

(async () => {
  const text = await solveImageCaptchaBase64("captcha.png");
  console.log(`Solved text: ${text}`);
})();

Expected output:

Task ID: 73849562810
Solved text: ABC123

Optional parameters

Parameter Description Example
numeric 1 = digits only, 2 = letters only, 0 = any numeric=1
min_len Minimum text length min_len=4
max_len Maximum text length max_len=8
phrase 1 = contains spaces phrase=0
regsense 1 = case-sensitive regsense=1
language 0 = default, 1 = Cyrillic, 2 = Latin language=2

Example with parameters:

submit = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": API_KEY,
    "method": "base64",
    "body": image_base64,
    "numeric": 1,      # digits only
    "min_len": 4,
    "max_len": 6,
    "json": 1
}).json()

Troubleshooting

Error Cause Fix
ERROR_ZERO_CAPTCHA_FILESIZE Image payload too small (<100 bytes) Ensure valid image data is being encoded
ERROR_TOO_BIG_CAPTCHA_FILESIZE Image exceeds 100 KB Compress or resize the image
ERROR_WRONG_FILE_EXTENSION Unsupported format Use JPG, PNG, or GIF
ERROR_IMAGE_TYPE_NOT_SUPPORTED Corrupt or unrecognized image Re-encode as standard PNG/JPG
Wrong text returned Low image quality Increase image resolution, reduce compression
Wrong text returned Missing hint parameters Add numeric, min_len, max_len as appropriate

FAQ

When should I use base64 vs file upload?

Use base64 when the image is already in memory (screenshots, API responses, headless browser captures). Use file upload when you have a CAPTCHA image saved on disk.

What image formats are supported?

JPG, JPEG, PNG, and GIF. Images must be between 100 bytes and 100 KB.

How fast are image CAPTCHA solves?

Typically 2–10 seconds, much faster than token-based CAPTCHAs like reCAPTCHA.

Can I solve math CAPTCHAs with this method?

Yes. CaptchaAI recognizes math expressions (e.g., "3 + 7 =") and returns the answer as text.

How do I improve accuracy?

Use the numeric, min_len, max_len, and language parameters to constrain the expected answer format. Higher-quality images also improve accuracy.


Start solving image CAPTCHAs

Get your API key at captchaai.com. Use method=base64 with your encoded image data.


Full Working Code

Complete runnable examples for this article in Python, Node.js, PHP, Go, Java, C#, Ruby, Rust, Kotlin & Bash.

View on GitHub →

Discussions (0)

No comments yet.