FOLASAYO SAMUEL OLAYEMI
SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

Follow

SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

Follow

How To Solve Captcha with NodeJS Using 2Captcha API

FOLASAYO SAMUEL OLAYEMI's photo
FOLASAYO SAMUEL OLAYEMI
·Apr 19, 2022·

3 min read

Play this article

Table of contents

  • What is Captcha?
  • What is 2Captcha?
  • Why do I need 2Captcha library?
  • How To Solve Captcha in NodeJS Using 2Captcha
  • Brief Explanations to the code samples above

What is Captcha?

Captcha is a computer program or system intended to distinguish human from machine input, typically as a way of thwarting spam and automated extraction of data from websites. Captcha stands for the Completely Automated Public Turing test to tell Computers and Humans Apart. CAPTCHAs are tools you can use to differentiate between real users and automated users, such as bots. CAPTCHAs provide challenges that are difficult for computers to perform, but relatively easy for humans. For example, identifying stretched letters or numbers, or clicking in a specific area.

CAPTCHAs are used by any website that wishes to restrict usage by bots.

What is 2Captcha?

2Captcha.com is a human-powered image recognition and CAPTCHA solving service, 2Captcha also helps you to bypass anti-robot protection (CAPTCHA) solving your captchas.

Why do I need 2Captcha library?

2captcha is a service that solves many types of captchas, this library serves as a wrapper around their API to bring easy, promise-based functionality to NodeJS. This library specializes in concurrent solves, and bulk-api usage.

How To Solve Captcha in NodeJS Using 2Captcha

I'm going to explain how to get the captcha solved on any website with NodeJS using 2Captcha.

Firstly, start your NodeJS by creating a folder. You can name your folder anything you feel like. After that, run the command npm init -y on your terminal. It makes sense to npm init at the start of the project because we use npm to download dependencies. Once we npm init , we can begin downloading (and saving) our dependencies.

To solve the captcha using NodeJS, we can use this library called 2captcha. Install this package after creating your file: npm install 2captcha or yarn add 2captcha After installing 2captcha library from NPM, now follow the examples below to get your captcha solved with ease by using 2captcha's API.

I'm going to make my examples simpler to understand.

Recaptcha

Code Sample:

const Captcha = require("2captcha")

// A new 'solver' instance with our API key
const solver = new Captcha.Solver("<Your 2captcha api key>")

/* Example ReCaptcha Website */
solver.recaptcha("6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5", "https://patrickhlauke.github.io/recaptcha/")

.then((res) => {
    console.log(res)
})

Now, Import the Captcha image:

const Captcha = require("2captcha")
const fs = require("fs")

const solver = new Captcha.Solver("<Your 2captcha api key>")

// Read from a file as base64 text
solver.imageCaptcha(fs.readFileSync("./captcha.png", "base64"))
.then((res) => {
    // Logs the image text
    console.log(res)
})
.catch((err) => {
    console.error(err.message)
})

Lastly, you have to set your proxy:

const Captcha = require("2captcha")

const solver = new Captcha.Solver("<Your 2captcha api key>")


solver.recaptcha("6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5", "https://patrickhlauke.github.io/recaptcha/", {
    proxy: "login:password@21.214.43.26", // The (Username : Password @ Address) of our chosen proxy
    proxytype: "HTTP" // The 'Type' of proxy, http, https, socks, ect.
})

.then((res) => {
    console.log(res)
})
.catch((err) => {
    console.error(err.message)
})

Brief Explanations to the code samples above

You have to initialize the captcha with your API key, you can get your API key when you sign up. The API_KEY is available on your dashboard at the Account setting.

Note that, we create an instance of solver with an API key and use the imageCaptcha. We are passing the image being parsed by fs package.

Here are some 2Captcha's Features:

  • Promise based 2captcha solving

  • Browser & NodeJS Support

  • Uses node-fetch, a lightweight HTTP library

  • Fluent typing & TS support

  • Account Interaction

  • Invalid Captcha reporting

  • Proxy Support

    Check here to know more about 2Captcha API's

With 2Captcha you can get your captchas solved with ease.

Thanks for reading...

Happy Coding!

Did you find this article valuable?

Support FOLASAYO SAMUEL OLAYEMI by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this