Blog
How to solve reCaptcha v2 with Node.JS

How to solve reCaptcha v2 with Node.JS

Logo of Capsolver

CapSolver Blogger

How to use capsolver

27-Sep-2023

Solving reCaptcha v2, reCaptcha v2 with Node.JS, reCaptcha v2 Node.JS solver

⚙️ Prerequisites

  • Proxy (Optional)
  • Node.JS installed
  • Capsolver API key

🤖 Step 1: Install Necessary Packages

Execute the following commands to install the required packages:

npm install axios

👨‍💻 Node.JS Code for solve reCaptcha v2 without proxy

Here's a Node.JS sample script to accomplish the task:

const axios = require('axios');

const PAGE_URL = ""; // Replace with your Website
const SITE_KEY = ""; // Replace with your Website
const CLIENT_KEY = "";  // Replace with your CAPSOLVER API Key

async function createTask(payload) {
  try {
    const res = await axios.post('https://api.capsolver.com/createTask', {
      clientKey: CLIENT_KEY,
      task: payload
    });
    return res.data;
  } catch (error) {
    console.error(error);
  }
}
async function getTaskResult(taskId) {
    try {
        success = false;
        while(success == false){

            await sleep(1000);
        console.log("Getting task result for task ID: " + taskId);
      const res = await axios.post('https://api.capsolver.com/getTaskResult', {
        clientKey: CLIENT_KEY,
        taskId: taskId
      });
      if( res.data.status == "ready") {
        success = true;
        console.log(res.data)
        return res.data;
      }
    }
  
    } catch (error) {
      console.error(error);
      return null;
    }
  }
  

async function solveReCaptcha(pageURL, sitekey) {
  const taskPayload = {
    type: "ReCaptchaV2TaskProxyless",
    websiteURL: pageURL,
    websiteKey: sitekey,
  };
  const taskData = await createTask(taskPayload);
  return await getTaskResult(taskData.taskId);
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
  try {
   
      const response = await solveReCaptcha(PAGE_URL, SITE_KEY );
      console.log(`Received token: ${response.solution.gReCaptcharesponse}`);
        
    }
catch (error) {
    console.error(`Error: ${error}`);
  }

}
main();

👨‍💻 Node.JS Code for solve reCaptcha v2 with proxy

Here's a Node.JS sample script to accomplish the task:

const axios = require('axios');

const PAGE_URL = ""; // Replace with your Website
const SITE_KEY = ""; // Replace with your Website
const CLIENT_KEY = "";  // Replace with your CAPSOLVER API Key
const PROXY =  "https://username:password@host:port";

async function createTask(payload) {
  try {
    const res = await axios.post('https://api.capsolver.com/createTask', {
      clientKey: CLIENT_KEY,
      task: payload
    });
    return res.data;
  } catch (error) {
    console.error(error);
  }
}
async function getTaskResult(taskId) {
    try {
        success = false;
        while(success == false){

            await sleep(1000);
        console.log("Getting task result for task ID: " + taskId);
      const res = await axios.post('https://api.capsolver.com/getTaskResult', {
        clientKey: CLIENT_KEY,
        taskId: taskId
      });
      if( res.data.status == "ready") {
        success = true;
        console.log(res.data)
        return res.data;
      }
    }
  
    } catch (error) {
      console.error(error);
      return null;
    }
  }
  

async function solveReCaptcha(pageURL, sitekey) {
  const taskPayload = {
    type: "ReCaptchaV2Task",
    websiteURL: pageURL,
    websiteKey: sitekey,
    proxy: PROXY
  };
  const taskData = await createTask(taskPayload);
  return await getTaskResult(taskData.taskId);
}
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
  try {
   
      const response = await solveReCaptcha(PAGE_URL, SITE_KEY );
      console.log(`Received token: ${response.solution.gRecaptchaResponse}`);
        
    }
catch (error) {
    console.error(`Error: ${error}`);
  }

}
main();

⚠️ Change these variables

  • PROXY: Change to your proxy, only if you use ReCaptchaV2Task.
  • CLIENT_KEY: Obtain your API key from the Capsolver Dashboard.
  • PAGE_URL: Replace with the URL of the website for which you wish to solve hCaptcha
  • SITE_KEY: Replace with the site key of the page with hcaptcha

👀 More information

More

reCAPTCHA v3 Solver
reCAPTCHA v3 Solver, Auto Recognition and Solve reCAPTCHA v3

The prevailing view is that in cybersecurity, it is vital to protect websites from infiltration by automated bots and malicious activity. However...

reCAPTCHA

02-Feb-2024

reCAPTCHA v2 Solver
reCAPTCHA v2 Solver, reCAPTCHA v2 Captcha Solving Service

Just as we all know, solving CAPTCHA has turned out to be an essential part of us in the web. However, solving CAPTCHA issues manually is time-consuming and frustrating...

reCAPTCHA

02-Feb-2024

Best reCAPTCHA v3 Captcha Solver
Best reCAPTCHA v3 Captcha Solver, auto reCAPTCHA v3 Solving Service Using API or Extension

In the realm of online security, reCAPTCHA v3 serves as a vital tool to protect websites against automated bots and malicious activities...

reCAPTCHA

25-Jan-2024

Best reCAPTCHA v2 Captcha Solver
Best reCAPTCHA v2 Captcha Solver, auto reCAPTCHA v2 Solving Service Using API or Extension

reCAPTCHA v2 poses a formidable challenge that necessitates an advanced solution. In this article, we unveil the epitome of captcha-solving technology: Capsolver...

reCAPTCHA

25-Jan-2024

Best reCAPTCHA v2/v3, Funcaptcha, Geetest Auto Solver
Captcha Solver Free, Best reCAPTCHA v2/v3, Funcaptcha, Geetest Auto Solver

Captcha solvers have become essential tools in navigating the internet, especially when faced with complex security measures like reCAPTCHA v2/v3, Funcaptcha, and Geetest...

reCAPTCHA

25-Jan-2024

the-Fastest-reCAPTCHA-FunCaptcha-Recognition
Captcha Solving Service, the Fastest reCAPTCHA & FunCaptcha Recognition

In today's digital landscape, online security has become a top priority. Websites employ various measures to protect themselves from malicious...

reCAPTCHA

19-Jan-2024