CAPSOLVER
Blog
How to solve hCaptcha with Node.JS

How to solve hCaptcha with Node.JS

Logo of CapSolver

Rajinder Singh

Deep Learning Researcher

27-Sep-2023

Solving hCaptcha, hCaptcha with Node.JS, hCaptcha 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 hCaptcha 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 solveHCaptcha(pageURL, sitekey) {
  const taskPayload = {
    type: "HCaptchaTaskProxyless",
    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 solveHCaptcha(PAGE_URL, SITE_KEY );
      console.log(`Received token cookie: ${response.solution.gReCaptcharesponse}`);
        
    }
catch (error) {
    console.error(`Error: ${error}`);
  }

}
main();

👨‍💻 Node.JS Code for solve HCaptcha 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 solveHCaptcha(pageURL, sitekey) {
  const taskPayload = {
    type: "HCaptchaTask",
    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 solveHCaptcha(PAGE_URL, SITE_KEY );
      console.log(`Received token cookie: ${response.solution.gReCaptcharesponse}`);
        
    }
catch (error) {
    console.error(`Error: ${error}`);
  }

}
main();

⚠️ Change these variables

  • PROXY: Change to your proxy, only if you use HCaptchaTask.
  • 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

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

Best hCaptcha Solvers: Top Tools to Solve CAPTCHA in 2024
Best hCaptcha Solvers: Top Tools to Solve CAPTCHA in 2024

Stumbled upon a troublesome hCaptcha while web scraping? Learn how CapSolver quickly and accurately solves hCaptcha process and increases efficiency!

hCaptcha
Logo of CapSolver

Sora Fujimoto

13-Aug-2024

Solving hCaptcha in C++: A Complete Guide
Solving hCaptcha in C++: A Complete Guide

Learn how to solve hCaptcha in C++ using the CapSolver API. This complete guide covers setting up your project, creating tasks, and retrieving task results with practical examples.

hCaptcha
Logo of CapSolver

Sora Fujimoto

31-Jul-2024

How to Solve hCaptcha: A Comprehensive Guide
How to Solve hCaptcha: A Comprehensive Guide

hCaptcha is a robust CAPTCHA service that effectively differentiates between humans and bots, but it can pose challenges for legitimate automated processes

hCaptcha
Logo of CapSolver

Rajinder Singh

29-Jul-2024

How to solve captcha with playwright while web scraping
How to solve captcha with playwright while web scraping

Learn to seamlessly How to solve captcha with playwright while web scraping, a detailed guide on setting up and automating captcha solutions effectively

hCaptcha
Logo of CapSolver

Rajinder Singh

16-Jul-2024

Automating hCaptcha Solutions: Best Practices and Tools
Automating hCaptcha Solutions: Best Practices and Tools

Learn to seamlessly Automating hCaptcha Solutions: Best Practices and Tools, a detailed guide on setting up and automating captcha solutions effectively

hCaptcha
Logo of CapSolver

Ethan Collins

16-Jul-2024

How to Identify if hCaptcha is Enterprise | By using CapSolver Extension
How to Identify if hCaptcha is Enterprise | By using CapSolver Extension

Learn how to identify if hcaptcha is enterprise using the CapSolver extension. This guide provides step-by-step instructions for detecting hcaptcha parameters, ensuring you can manage and troubleshoot website security measures effectively.

hCaptcha
Logo of CapSolver

Ethan Collins

03-Jun-2024