CAPSOLVER
Blog
How to solve reCaptcha v2 with Node.JS

How to solve reCaptcha v2 with Node.JS

Logo of CapSolver

Rajinder Singh

Deep Learning Researcher

27-Sep-2023

TL;DR

This guide explains how to solve reCAPTCHA v2 using Node.js with CapSolver’s API. It covers both proxyless and proxy-based implementations, includes step-by-step setup instructions, and provides ready-to-use Node.js code examples. By following this tutorial, developers can integrate reCAPTCHA v2 handling into their automation or data-collection workflows efficiently and reliably.

Introduction

reCAPTCHA v2 remains one of the most commonly deployed human-verification mechanisms across the web. For developers working with automated workflows, data extraction, or large-scale browser automation, handling reCAPTCHA v2 efficiently is a recurring technical challenge.

In this article, we walk through a practical Node.js implementation for solving reCAPTCHA v2 using the CapSolver API. You will learn how to configure your environment, install required dependencies, and implement solutions both with and without a proxy. The included examples are production-oriented and designed to be easily adapted to real-world use cases.

⚙️ Prerequisites

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

🤖 Step 1: Install Necessary Packages

Execute the following commands to install the required packages:

python Copy
npm install axios

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

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

js Copy
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:

js Copy
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 captcha
  • SITE_KEY: Replace with the site key of the page with captcha

👀 More information

Conclusion

Solving reCAPTCHA v2 in Node.js does not need to be complex. By leveraging the CapSolver API, developers can implement a clean, scalable solution that works reliably across different website configurations. Whether you choose a proxyless setup for simplicity or a proxy-based approach for greater control, the examples in this guide provide a solid foundation for production use.

FAQs

1. What is the difference between proxyless and proxy-based reCAPTCHA v2 tasks?

Proxyless tasks rely on CapSolver’s internal infrastructure and are simpler to implement, while proxy-based tasks allow you to control IP reputation and geographic location for higher success rates on stricter websites.

2. When should I use ReCaptchaV2Task instead of ReCaptchaV2TaskProxyless?

Use ReCaptchaV2Task when the target website enforces IP consistency or applies regional restrictions that require a specific proxy configuration.

3. How long does it usually take to receive a reCAPTCHA v2 token?

Most tasks are completed within a few seconds. Actual timing depends on site complexity, challenge difficulty, and whether a proxy is used.

4. Can this solution be integrated into larger Node.js automation systems?

Yes. The provided functions are modular and can be embedded into crawlers, automation pipelines, or browser-based workflows.

Compliance Disclaimer: The information provided on this blog is for informational purposes only. CapSolver is committed to compliance with all applicable laws and regulations. The use of the CapSolver network for illegal, fraudulent, or abusive activities is strictly prohibited and will be investigated. Our captcha-solving solutions enhance user experience while ensuring 100% compliance in helping solve captcha difficulties during public data crawling. We encourage responsible use of our services. For more information, please visit our Terms of Service and Privacy Policy.

More