API Tutorials

Solve Image CAPTCHA with Node.js and CaptchaAI

Image CAPTCHAs display distorted text that users must type. They appear on government sites, legacy forms, and registration pages. CaptchaAI reads the image and returns the text. This guide shows how to do it from Node.js.


Prerequisites

Item Value
CaptchaAI API key From captchaai.com
Node.js 14+
Libraries axios, fs
Image format JPG, PNG, or GIF (100 bytes – 100 KB)

Method A: Base64 submission

const axios = require('axios');
const fs = require('fs');

const API_KEY = 'YOUR_API_KEY';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

// Read and encode the image
const imageB64 = fs.readFileSync('captcha.png').toString('base64');

// Submit to CaptchaAI
const { data: submitData } = await axios.post('https://ocr.captchaai.com/in.php', null, {
  params: {
    key: API_KEY,
    method: 'base64',
    body: imageB64,
    json: 1,
  },
});

if (submitData.status !== 1) throw new Error(submitData.request);
const taskId = submitData.request;
console.log(`Task submitted: ${taskId}`);

Method B: File upload

const FormData = require('form-data');

const form = new FormData();
form.append('key', API_KEY);
form.append('method', 'post');
form.append('json', '1');
form.append('file', fs.createReadStream('captcha.png'));

const { data: submitData } = await axios.post('https://ocr.captchaai.com/in.php', form, {
  headers: form.getHeaders(),
});

const taskId = submitData.request;

Poll for the text result

await sleep(5000);

let captchaText;
for (let i = 0; i < 30; i++) {
  const { data: pollData } = await axios.get('https://ocr.captchaai.com/res.php', {
    params: { key: API_KEY, action: 'get', id: taskId, json: 1 },
  });

  if (pollData.status === 1) {
    captchaText = pollData.request;
    console.log(`CAPTCHA text: ${captchaText}`);
    break;
  }
  if (pollData.request !== 'CAPCHA_NOT_READY') {
    throw new Error(pollData.request);
  }
  await sleep(5000);
}

Accuracy parameters

// Digits only, 4-6 characters
const { data } = await axios.post('https://ocr.captchaai.com/in.php', null, {
  params: {
    key: API_KEY,
    method: 'base64',
    body: imageB64,
    numeric: 1,      // digits only
    min_len: 4,       // minimum length
    max_len: 6,       // maximum length
    json: 1,
  },
});
Parameter Value Purpose
numeric 1 = digits, 2 = letters Limits characters
min_len / max_len Integer Length constraints
calc 1 Computes math expression
regsense 1 Case-sensitive

Complete working example

const axios = require('axios');
const puppeteer = require('puppeteer');
const fs = require('fs');

const API_KEY = 'YOUR_API_KEY';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

async function solveImageCaptcha() {
  // 1. Load page and screenshot CAPTCHA
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com/register');

  const captchaEl = await page.$('#captcha-image');
  await captchaEl.screenshot({ path: 'captcha.png' });

  // 2. Encode and submit
  const imageB64 = fs.readFileSync('captcha.png').toString('base64');
  const { data: submit } = await axios.post('https://ocr.captchaai.com/in.php', null, {
    params: { key: API_KEY, method: 'base64', body: imageB64, json: 1 },
  });
  const taskId = submit.request;

  // 3. Poll for text
  await sleep(5000);
  let text;
  for (let i = 0; i < 30; i++) {
    const { data: poll } = await axios.get('https://ocr.captchaai.com/res.php', {
      params: { key: API_KEY, action: 'get', id: taskId, json: 1 },
    });
    if (poll.status === 1) { text = poll.request; break; }
    if (poll.request !== 'CAPCHA_NOT_READY') throw new Error(poll.request);
    await sleep(5000);
  }

  // 4. Type and submit
  await page.type('#captcha-input', text);
  await page.click('form [type="submit"]');
  console.log(`Solved: ${text}`);
  await browser.close();
}

solveImageCaptcha().catch(console.error);

Expected output:

Solved: ABC123

Common errors

Error Cause Fix
ERROR_WRONG_FILE_EXTENSION Unsupported format Use JPG, PNG, or GIF
ERROR_TOO_BIG_CAPTCHA_FILESIZE Image > 100 KB Compress first
ERROR_ZERO_CAPTCHA_FILESIZE Image < 100 bytes Verify the image
CAPCHA_NOT_READY Still solving Poll every 5 seconds

FAQ

Can I solve math CAPTCHAs?

Yes. Add calc: 1 to the parameters and CaptchaAI will return the computed result.

How do I report wrong solutions?

Call https://ocr.captchaai.com/res.php?key=KEY&action=reportbad&id=TASK_ID to report incorrect results.

Is base64 or file upload faster?

Performance is the same. Base64 is more convenient when you already have the image in memory.



Start solving image CAPTCHAs with CaptchaAI →

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.