Scarnox · Blog

Node.js में captcha solver: fetch के साथ working उदाहरण (2026)

Node.js में captcha solver एक HTTP request है। Node 18 और उसके बाद से fetch पहले से शामिल है, इसलिए API call करने के लिए किसी package की ज़रूरत नहीं। Sitekey और page URL भेजें, जवाब का इंतज़ार करें, और token को form में डालें। यह guide PopularCaptcha के लिए पूरा flow दिखाती है, जिसे Scarnox आज proxyless आधार पर solve करता है।

मुफ़्त ट्रायल क्रेडिट से शुरू करें, कार्ड की ज़रूरत नहीं और नीचे का code पहले किसी test page पर चलाएँ।

ज़रूरतें

Solver module

नीचे का module एक function है। वह Bearer header के साथ API पर POST करता है, 185 सेकंड बाद रद्द करता है, और errorId के 0 न होने पर throw करता है:

// solver.mjs
const API_URL = "https://api.scarnox.com/api/tasks/create";

export async function solveCaptcha(sitekey, pageUrl, { invisible = false } = {}) {
  const body = {
    captcha_type: "PopularCaptchaTaskProxyless",
    site_key: sitekey,
    site_url: pageUrl,
  };
  if (invisible) body.is_invisible = true;

  const res = await fetch(API_URL, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.SCARNOX_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(body),
    signal: AbortSignal.timeout(185_000), // token तैयार होने पर call लौट आती है
  });

  const data = await res.json().catch(() => null);
  if (!data) throw new Error(`HTTP ${res.status}: response was not JSON`);

  if (data.errorId !== 0) {
    throw new Error(`${data.errorCode}: ${data.errorDescription}`);
  }
  return data.solution.token;
}

कोई polling नहीं है। Request तब तक खुली रहती है जब तक token तैयार न हो, और सफलता पर token solution.token में होता है।

Retries

विफल solve errorId के 0 से अलग मान लौटाता है, और उसका चार्ज नहीं लगता। अस्थायी failures के लिए एक छोटा retry loop काफ़ी है। प्रयासों की संख्या कम रखें, ताकि गलत sitekey या page URL समय न खाए:

export async function solveWithRetry(sitekey, pageUrl, attempts = 3) {
  let lastError;
  for (let i = 1; i <= attempts; i++) {
    try {
      return await solveCaptcha(sitekey, pageUrl);
    } catch (err) {
      lastError = err;
      console.warn(`Attempt ${i} failed: ${err.message}`);
      await new Promise((r) => setTimeout(r, 1000 * i));
    }
  }
  throw lastError;
}

पूरी error policy के लिए captcha solver error handling देखें।

Token submit करना

Token उस form field में जाता है जिसकी site उम्मीद करती है। ज़्यादातर PopularCaptcha pages h-captcha-response पढ़ती हैं, और कई g-recaptcha-response भी पढ़ती हैं। सुरक्षा के लिए दोनों भेजें:

// submit.mjs
import { solveWithRetry } from "./solver.mjs";

const pageUrl = "https://example.com/signup";
const token = await solveWithRetry("YOUR-SITEKEY-UUID", pageUrl);

const form = new URLSearchParams({
  email: "you@example.com",
  "h-captcha-response": token,
  "g-recaptcha-response": token,
});

const res = await fetch(pageUrl, {
  method: "POST",
  body: form,
  redirect: "manual",
});
console.log("Status:", res.status);

इसे इस तरह चलाएँ:

SCARNOX_API_KEY=scarnox_YOURAPIKEY node submit.mjs

Cookies और sessions

सादा fetch requests के बीच cookies नहीं रखता। कई sign-up और search pages captcha को session cookie से जोड़ती हैं, इसलिए बिना मेल खाती cookie के भेजा गया token reject हो सकता है। इसे संभालने के दो तरीके हैं:

पहला तरीका तब चुनें जब form सरल हो और site बहुत JavaScript न चलाती हो। किसी भी अधिक जटिल चीज़ के लिए दूसरा चुनें।

एक व्यावहारिक जाँच: POST के बाद status code और अंतिम URL देखें। सफलता वाले page पर redirect आमतौर पर बताता है कि token स्वीकार हुआ। widget को फिर दिखाने वाला page आमतौर पर बताता है कि token reject हुआ, या GET की cookie साथ नहीं आई। पहले कुछ runs के लिए response body लॉग करें, ताकि पता चले कि आप किस स्थिति में हैं।

समय

Token लगभग दो मिनट में expire होता है। Submit के लगभग उसी पल solve करें। आम गलती है लंबे job की शुरुआत में solve करना और बाद में submit करना। Solve करें, फिर तुरंत submit करें। captcha token expiry इसे विस्तार से बताती है।

Invisible captchas

अगर page के widget में data-size="invisible" है, तो { invisible: true } पास करें। तब function request के साथ is_invisible: true भेजता है:

const token = await solveCaptcha("YOUR-SITEKEY-UUID", pageUrl, { invisible: true });

अगर page widget के साथ एक callback भी जोड़ता है, तो form को उस function को token के साथ बुलाने की ज़रूरत पड़ सकती है। उसे खोजने के लिए page की script पढ़ें। invisible captcha guide पैटर्न कवर करती है।

Solver के साथ scraping

कभी-कभी captcha उस page पर दिखता है जिसे आप scrape करते हैं, किसी form पर नहीं जिसे आप submit करते हैं। वही पैटर्न काम करता है: HTML में widget पहचानें, उसे solve करें, फिर उस URL पर submit करें जिस पर widget post करता है। Request rate कम रखें और पहले site के terms जाँचें। ज़िम्मेदारी से scrape करें: robots.txt का सम्मान करें, वह personal data न लें जिसकी ज़रूरत नहीं, और server को हथौड़े की तरह न पीटें।

कीमत

Scarnox हर सफल solve पर बिल करता है। विफल solves का चार्ज नहीं लगता। मौजूदा दरों के लिए pricing page देखें। शुरू करने के लिए मुफ़्त ट्रायल क्रेडिट से शुरू करें, कार्ड की ज़रूरत नहीं।

अगले कदम