Node.js captcha solver: an API client with built-in fetch
A Node.js captcha solver fits in one file since Node.js 18, which provides fetch as a global: no HTTP library is needed to send a task and read its answer. With ilovecaptcha the client makes two HTTPS calls, createTask then getTaskResult, and encodes the image with a Buffer. This page explains the steps and gives the complete script.
Preview: the API described here is in public beta. Endpoint names, fields and codes can still change until the end of the beta, and no API key is distributed yet.
How do you call a captcha solver API from Node.js?
With the global fetch available in Node.js 18 or newer. The helper post sends a POST request with the header Content-Type:, a body made by JSON.stringify with the key under clientKey, and returns response.json. The script is CommonJS: it has one require, for node:, and takes nothing from npm.
| Item | Value |
|---|---|
| Runs on | Node.js 18 or newer |
| Packages to install | None: built-in modules only |
| File | solve.js |
| Command | node solve.js |
| HTTP client | Global fetch |
How do you send the captcha image and wait for the answer in Node.js?
fs.readFileSync returns the image as a Buffer, and its toString method called with base64 gives the string to place in the task. solve posts it to createTask, keeps the taskId and polls getTaskResult inside a while loop, all in one async function.
POLL_MSis 1500: the loop awaits a promise resolved bysetTimeout.TIMEOUT_MSis 120000: past that,solvethrows anErrorthat names the task.- The event loop stays available during the wait, so other work can run.
- A reply already
readyatcreateTaskis returned at once.
How do you handle the API key and errors in Node.js?
Read the key from the environment with process.env, for instance from a variable named ILOVECAPTCHA_KEY, instead of the constant API_KEY used in the example: the key then stays out of the repository. When the reply carries a non-zero errorId, post throws an Error with errorCode and errorDescription. The example runs inside an async function without a catch: add one, otherwise the process stops on the first rejection.
What does a complete Node.js captcha solver client look like?
The script below is the whole client, in 40 lines. It defines post, solve and getBalance, then reads captcha.png from the current folder, submits it as an aws-waf task and logs the solution. Replace YOUR_API_KEY and YOUR_API_URL with your own values, set the task type and fields of your captcha, and run it with node.
solve.js · Complete Node.js client: one createTask call, then getTaskResult until the task is ready (preview format).const fs = require("node:fs");
const API_KEY = "YOUR_API_KEY";
const BASE_URL = "YOUR_API_URL";
const POLL_MS = 1500;
const TIMEOUT_MS = 120000;
async function post(route, body) {
const response = await fetch(BASE_URL + route, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ clientKey: API_KEY, ...body }),
});
const result = await response.json();
if (result.errorId) throw new Error(`${result.errorCode}: ${result.errorDescription}`);
return result;
}
async function solve(task) {
let result = await post("/createTask", { task });
if (result.status === "ready") return result.solution;
const taskId = result.taskId;
const deadline = Date.now() + TIMEOUT_MS;
while (Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
result = await post("/getTaskResult", { taskId });
if (result.status === "ready") return result.solution;
}
throw new Error(`task ${taskId} not ready after ${TIMEOUT_MS / 1000} s`);
}
async function getBalance() {
return (await post("/getBalance", {})).balance;
}
(async () => {
const image = fs.readFileSync("captcha.png").toString("base64");
console.log(await solve({ type: "aws-waf", image }));
console.log(await getBalance());
})();
Node.js captcha solver: frequently asked questions
Do I need an npm package or an SDK to solve captchas in Node.js?
No. Since version 18, Node.js provides fetch without any import, and the rest is in the language: JSON.stringify for the body, response.json for the reply, a Buffer for the base64 image. The script on this page is the complete client. Put it in your project, export solve with module.exports and require it where you need it: no dependency is added to package.json.
Does the example run on Node.js versions older than 18?
Not as written: fetch is a global only since Node.js 18. On an older version, rewrite the body of post with the https module, or install a package that provides fetch; nothing else changes, since solve only expects post to return the decoded reply. The node: prefix used in require is also recent: on an old version, write fs instead.
How do I use the answer with Selenium, Puppeteer or Playwright in Node.js?
Keep post and solve as they are and add the browser. The automation tool takes a screenshot of the captcha element, the image goes to solve, and the returned points or distance are replayed with the mouse, divided by the scale of the screenshot. The pages on Selenium, Puppeteer and Playwright each give that complete script in Node.js, with the same two functions at the top.
Sources
The facts and figures quoted on this page come from these sources.
- OpenJS Foundation, Global objects, Node.js documentation (the fetch global).
- OpenJS Foundation, Buffer, Node.js documentation (base64 encoding of binary data).