How to solve a captcha in Node.js using 2Captcha?

Jun 21st, 2023

How to solve a captcha in Node.js using 2Captcha?

advertisement

Captcha, or Completely Automated Public Turing tests are everywhere in today’s world from filling up a form to logging into a website. The basic goal of a captcha is to help the system check if the user is a human or a bot.

In today’s world, the captcha has advanced significantly. At times, solving a captcha can be effortless, while other times it is a headache for the users to determine whether the letter is uppercase or lowercase. As a result, nowadays we can find some captcha-solving software like 2Captcha.

However, solving a captcha programmatically can be challenging, as a result in this article we’ll discuss how to solve a captcha using Node.js and 2Captcha API.

What is 2Captcha?

2Captcha is an online captcha recognition service that solves captchas in real-time. 2Captcha can recognize and solve various types of captcha. They have an API for developers and is available for most programming languages.

Advantages of using 2Captcha:

  • Solves almost all types of captcha
  • API available for most popular programming languages
  • Provides high-level accuracy
  • Low captcha-solving time

By now, you should have gained familiarity with what 2Captcha is.

This article will be divided into two parts:

  • Image captcha solving
  • and hCaptcha solving

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  • Node.js installed on your machine
  • A 2Captcha API key (sign up at https://2captcha.com/ if you don’t have one)

How to solve an image captcha in Node.js?

To get started, create a new Node.js project and install the necessary dependencies. Open your terminal and execute the following commands:

npm init -y
npm install 2captcha

Implementing the captcha solver:

  • Inside image.js add the following code:

    const Captcha = require('2captcha');
    const fs = require('fs');
    
    const solver = new Captcha.Solver('<API_KEY>');
    
    solver
        .imageCaptcha(fs.readFileSync('IMAGE_URL', 'base64'))
        .then((res) => { console.log(res); })
        .catch((err) => {  console.log(err); });
  • Get the captcha image and update the IMAGE_URL in the code. For this tutorial, I will be using the following captcha image:

    https://res.cloudinary.com/dtscloud/image/upload/v1687343651/Post%20Images/captcha_fzvoih.png

  • Once you get the image, run the code:

    node image.js
  • After you run the code, it will take a few seconds to process and return the output in the terminal. The output will look similar to this:

    { data: '25w53', id: 'XXXXXX44469' }

    As you can clearly see 2Captcha has successfully solved the captcha. You can try this with different image captchas.

How to solve hCaptcha using Node.js?

  • To get started, create a new project (if you wish you can continue in the old project), and install the necessary dependencies:

    npm init -y
    npm install 2captcha
  • Create a new file solve.js and inside it add the following code:

    const Captcha = require('2captcha');
    
    const solver = new Captcha.Solver('<API_KEY>');
    
    solver
      .hcaptcha("WEBSITE_HCAPTCHA_SITE_KEY", "WEB_URL")
      .then((res) => { console.log(res); })
      .catch((err) => { console.log(err); });

    Replace the site key with the website’s hCaptcha site key and also replace the WEB_URL

    Here in this tutorial, I will be using a demo form created by me.

  • After replacing the values, run the code:

    node solve.js
  • Once you run the code, it will take a few seconds to solve and return with an output:

    {
      data: 'P1_eyJ0eXAXOuOKV1QiLCJhbGciOiJIUzI1NiJ9.hKdwYXNza2V5xQow0gOC2_gJbeHxe488mD_tgotHnkc-_CRoArTZCH2wnN5n6fUSKDJl8OUcJNPmbPq64Kv-zEnUeHAmAdspAI9sYZPcOz69hgbt_pjzc1w4Qdb4AEk9W43t96bnUMYGgtcehSUmZsEuMSdGGyupbsfjmBUNpv6vFHUNy7APLcHAu755YISE4DyJ4qnkPnpp_Wl79zdvYELCc4h71P80TkvmNKYghlLYj43htDgk9pSnzaPius47RVO70m49vsseAn1uHHOsXlhsQQKmZZZDTeNF0NEtBLlBcRp0usqtJ0uSbrTkNUsvZcnIO_KortIg6XWgCfbLuPtTLhvHqB0ICKM9YusEswhbpLjEFXNEw-W22xNnvrgrrdRmkLM7WICOGfHxi9SpzG1ut8R1w1oGMbzMmvX4DEo61PcgXx2zXECb9R4Dji1gM3MD9k2yXmqZxceaV5R3DDDKhDL4tO3rlxWMXMeKOwyUbE4YS3t2Bt4a_J85EewkWpAo8KottZFD-pXIYJU1-mthcLkZE4whhjMIX9TYHT7zKDKkRBb8Xs4sb4TpvXt8Ax_Gg_h8-RKyISgUOhwfpECoE_NguLmE2kdkIVsGIKWhb2ajYgz5T6tMyhNOSuBnzfD-ipDZbHxGkGbZ2OhhxSeSpypKnRaXBr1i_F_4E5d8-eTCqiXbuSCOCmUvCmmhF6sL2gaDgwLhu8n6gt2wKnvZc9CK73BkYSHqaA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fWt6FjVG9jQdgcH-8w0MuGnaFK5-akX6XVgfCJbzZKiehec6l249vYIGzMPKaGmGnS7g24FTAl1xMGqj3R1OAu1i8Il2mZVmp1BjKQnde4r4JZNducF4p6jLuX_uDJ3ltnBkjEuRC2Z3rTvxWB2vo2tLZENp2otukoM0ZzPi49fsM231ytGiE4I1f3_fg7nQL1VeTQRrEfHNdM1lKZuouR9TwPnHP9yQOQkNZBuEVEVQtLC69ZCTU5WsAMx8ZDLUodENlLOLuHRc29LLmGio5vKc-7PLQ2IjYpmsFx66XPoFTDokSp2tKLRy9Nfj2sGYgv7fDcyflYHPr89HqSzJ-g1lEi52LojC3nFgAqSJu5nKGL8eXFNAdX7MryZV7XEPY17OEP-MEMVb0xyd83V5Q_d8gU8u1tkQ50u9t-v95yoTeO7IIusvus_8Mr7INy8n83dyqPyYmHGySAP-_AZHGg28ImT7No7eTUOBUv2KNs_stu0qApUgpLDOKt3oshfV7660-J7J9NjeBf4vJLmqKV_yEaZo6GW0SlP1mAwRNyw47irB5Vu1LoQ1XWUbRNasgGRXy8KNleHDOZJLldahzaGFyZF9pZM4VmeRUonBkAA.4flJozFcrOsbc91NQWufEUqVSVSwpBq3Nj75a15jDSo',
      id: 'XXXXXXX4001'
    }

    Tada you have successfully solved the captchas using 2Captcha and Node.js.

Conclusion

I hope you have learned something new from this article, thanks for reading!

That’s it! You now have a comprehensive guide on how to solve CAPTCHAs in Node.js using the 2Captcha API. Happy coding!


Disclosure: Includes paid promotion

Disclaimer: The use of the 2Captcha service and the implementation described in this article is subject to the terms and conditions set by 2Captcha. Make sure to review and comply with their terms of service and any applicable usage limits or restrictions. The code examples provided in this article are for demonstration purposes only and should be adapted and used responsibly in accordance with legal and ethical considerations. We do not endorse or assume any responsibility for the use of third-party services or the consequences that may arise from their usage.

Credits: Article Banner Image illustration credit Notioly