DEV Community

Cover image for Decoding image CAPTCHA using 2captcha
shrey vijayvargiya
shrey vijayvargiya

Posted on

Decoding image CAPTCHA using 2captcha

Working with 2captcha to decode image captcha in Node JS

Under the Hood
I am sure you have gone through the process of selecting the specific images from the collection of images called CAPTCHAS. If not below is the image to give you an example.

Image description

This image selection process is called CAPTCHAS, it’s a test which users have to pass so that websites can differentiate between users and bots. Bots nowadays can access the website and fill the form, make transactions and do many more kinds of abusive interactions with the websites.

In order to prevent this malicious unwanted interaction, CAPTCHAS come into existence, you can understand them as a test which helps the websites to distinguish between bots and humans.

What is image CAPTCHAS?
Image captchas are the test that uses low-resolution real-life images containing real-world daily seen objects. There are 2 reasons why image CAPTCHAS are widely used -

Low-resolution images are difficult for bots to read
Objects within low resolution are again not easy to detect by the bots

What is 2captcha?
2captcha is the API that helps developers to decode the captchas in less than 12 seconds irrespective of the programming language you are working with. They are compatible with Browser and Servers and can be executed with any programming language of developers' choice.

Few more points why 2captchas are the best services in the same β€”

  • reCaptcha recognition service
  • Compatible with other programming languages
  • Fast and accurate
  • Solves other kinds of captchas such as text, hCaptchas and so on.

Getting started
To get started working with 2captchas you need to create an account on their website. Once the signup process is done you will be redirected to the dashboard where you will get the API keys and you can easily COPY it from the dashboard as shown in the image.

Image description

2captcha dashboard screenshot
Under account settings, you can find the API key, so COPY it and save it as we will be needing it later on.

Decoding image CAPTCHA
We will be using Node js and 2captcha npm module to decode the image captchas. Go ahead and create Node js basic application if not you can directly download the code repository from here.

Once the repository is cloned, install the 2captcha npm module in the application.

Decode image method
2captcha provide easy to use promise-based method to solve image captchas.

  • Import captcha from 2captcha
  • Create captcha solver instance using 2captcha by providing the API key that we have saved earlier from the dashboard
  • Pass the image using the fs module of node js as the parameter to the captchas solver method.
  • Handle the promised return by captcha solver
const Captcha = require("2captcha");
const fs = require("fs");

const solver = new Captcha.Solver(API_KEY)
solver.imageCaptcha(fs.readFileSync("./captcha.png", "base64"))
.then((res) => {
.catch((err) => {
Enter fullscreen mode Exit fullscreen mode

2captch will solve the image captcha in less than 12 seconds and in return, it will give you the data and id, the data key containing the text written on the image or data processed in the image and the id is required to access the data.

2captcha is a widely used npm module to solve captchas and protect websites from bots vulnerabilities. They have more than 80K npm downloads that ensure its reliability, trust and accuracy. I will cover more stories about solving text-based, hCaptchas using the 2captcha module and how to implement them with other programming languages, so stay tuned for the same.

Code repository

Our website iHateReading || Youtube || Twitter

Discussion (0)