JavaScript captcha solver: an API client with fetch
A JavaScript captcha solver needs nothing beyond the language and the web platform: fetch sends the task, JSON carries it, btoa encodes the image. With ilovecaptcha that means two HTTPS calls, createTask then getTaskResult. The example on this page is an ES module that Deno runs as is, and the sections below explain each of its steps.
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 JavaScript?
With fetch, the request function built into browsers and into Deno. The helper post sends a POST request whose body is JSON.stringify of the task with the key added under clientKey, then reads the reply with response.json. The file is an ES module, so it uses top-level await and loads no library at all.
| Item | Value |
|---|---|
| Written for | Deno, as an ES module |
| Packages to install | None: web-standard functions only |
| File | solve.mjs |
| Command | deno run --allow-net --allow-read solve.mjs |
| HTTP client | fetch |
How do you send the captcha image and wait for the answer in JavaScript?
The image is fetched as a Blob, located next to the module through import.meta.url, then turned into base64 by the helper toBase64: it reads the bytes into a Uint8Array, builds a binary string and passes it to btoa. solve then calls createTask and polls getTaskResult.
POLL_MSis 1500: asetTimeoutwrapped in a promise is awaited between two polls.TIMEOUT_MSis 120000: past that,solvethrows anError.- The deadline is computed once, with
Date.now. - A reply already
readyatcreateTaskis returned at once.
Where does the API key go, and how are errors reported in JavaScript?
On a server or in a command-line script, never in a page served to visitors: anything a browser downloads can be read, key included. Under Deno, read it with Deno.env.get from a variable such as ILOVECAPTCHA_KEY, which requires the --allow-env permission. Errors arrive as JSON: when errorId is set, post throws an Error built from errorCode and errorDescription. A failed connection makes fetch reject, to catch around solve.
What does a complete JavaScript captcha solver client look like?
The module below is the whole client, in 43 lines. It defines post, solve, getBalance and toBase64, then loads captcha.png from its own folder, submits it as an aws-waf task and logs the solution. Replace YOUR_API_KEY and YOUR_API_URL with your own values and adapt the task to your captcha before you run it.
solve.mjs · Complete JavaScript client: one createTask call, then getTaskResult until the task is ready (preview format).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 function toBase64(blob) {
const bytes = new Uint8Array(await blob.arrayBuffer());
let binary = "";
for (const byte of bytes) binary += String.fromCharCode(byte);
return btoa(binary);
}
const imageBlob = await (await fetch(new URL("./captcha.png", import.meta.url))).blob();
console.log(await solve({ type: "aws-waf", image: await toBase64(imageBlob) }));
console.log(await getBalance());
JavaScript captcha solver: frequently asked questions
Do I need an npm package or an SDK to solve captchas in JavaScript?
No. The client uses only functions that the platform already provides: fetch for the two calls, JSON.stringify and response.json for the body and the reply, btoa for the image. Nothing is imported, so there is no package.json and no dependency to update. The module on this page is the complete client: copy it, export solve, and import it where you need it.
Can I call a captcha solver API from a web page?
The same code runs in a page, since fetch and btoa are browser functions, but the key would then be visible to every visitor, in the source and in the network panel. Keep the call on a server or in a script you run yourself, and let the page talk to your own backend. The page only needs the answer: a token, points, a distance or text.
Is this the same code as the Node.js example?
Almost. The two calls and the polling loop are identical, because Node.js also provides fetch. The differences are at the edges: this module uses import.meta.url, a Blob and btoa to read and encode the image, where the Node.js version reads the file with the fs module and encodes it with a Buffer, in CommonJS. Pick this one for Deno, the other one for Node.js.
Sources
The facts and figures quoted on this page come from these sources.
- MDN Web Docs, Fetch API and Window: btoa() method (request, JSON body, base64 encoding).
- Deno, Security and permissions (the allow-net, allow-read and allow-env flags).