JavaScript / Node.js
Solve captchas from Node.js.
The helper below uses the native fetch API (built into Node.js 18+, no dependencies).
It submits a task with POST /createTask, polls POST /getTaskResult until the status
is ready, and throws 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).
const BASE_URL = "https://api.nocaptchaai.com";
const API_KEY = process.env.NOCAPTCHAAI_KEY;
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function post(path, body) {
const res = await fetch(`${BASE_URL}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
return res.json();
}
/**
* Create a task, poll until it is ready, and return the solution.
* Throws on an API error or if the timeout is reached.
*/
async function solve(task, { pollInterval = 3000, timeout = 120000 } = {}) {
const created = await post("/createTask", { clientKey: API_KEY, task });
if (created.errorId) {
throw new Error(`createTask failed: ${JSON.stringify(created)}`);
}
const taskId = created.taskId;
const deadline = Date.now() + timeout;
while (true) {
const result = await post("/getTaskResult", { clientKey: API_KEY, taskId });
if (result.status === "ready") return result.solution;
if (result.status === "failed" || result.errorId) {
throw new Error(`task failed: ${JSON.stringify(result)}`);
}
if (Date.now() >= deadline) {
throw new Error(`timed out waiting for task ${taskId}`);
}
await sleep(pollInterval);
}
}
const solution = await solve({
type: "AntiTurnstileTask",
websiteURL: "https://example.com",
websiteKey: "0x4AAAAAAA...",
});
console.log(solution);Call solve() with any task object — swap the type, websiteURL, and websiteKey
for the captcha you need to solve. The returned solution object contains the token
(and any other fields) you submit back to the target site.