The Complete Guide to Solving Captcha with 2Captcha.
Tags: javascript, webdev, programming, tutorial
Published: 2022-02-18 07:26:13

Captcha is a type of puzzle that requires humans to solve it in order to prove they’re not robots. They’re designed with the intention of protecting your account from getting hacked by bots.

It usually appears when you are registering for an account on some websites, posting comments, filling out surveys, participating in polls, uploading your video to YouTube, among other things.

They’re used to protect websites from spam and automated programs, but many people find captchas frustrating and time-consuming.

It’s hard enough just to prove who we are without being forced to decipher an illegible word or number.

Thankfully, there is a new solution for this with 2Captcha.

2Captcha is a service that allows you to bypass captcha so that you can log in or sign up for any website or app with ease. It has been proven as one of the most effective ways of using captcha security on the web today.

Most websites tend to use Google’s reCAPTCHA, but this service works better since it doesn’t discriminate against people who have disabilities or impairments.

In this article, we’ll teach you how to use 2Captcha and what are the benefits of using 2Captcha. We will also discuss how you can use 2Captcha API with an existing project.

## What is 2Captcha?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqiau1gret92kwwphzh9.jpg)

 
Basically, 2Captcha provides an AI-based service that will solve captchas automatically and quickly – saving you time and frustration!

It solves captchas automatically and provides you with an answer without having to solve it yourself. All you need is the captcha image and 2captcha will take care of the rest.

How 2Captcha works is it solves this issue by providing a human-solved captcha service that bypasses the use of Captcha completely. It offers a simple and effective solution for any website owner looking for better security at a cost-effective rate.
Keep reading to learn more about 2Captcha, how it works, and why you should use it on your site today!

The best part of 2captcha is it has a consumer rating of 4.57 stars from 767 reviews indicating that most customers are generally satisfied with their purchases.

## Benefits of using 2Captcha over traditional Captchas

Following are the benefits of using 2Captcha:

1. Increased security.
2. Improved user experience – Reduced customer frustration.
3. It provides a human-solved captcha services.
4. Usually takes 11 seconds to solve any captcha.
5. API available for most popular programming languages.
6. Supports all types of captcha such as normal, text, click, rotate, etc.
7. Provide 100% recognition service.

## How to use the 2Captcha API with an existing project?

Basically, it’s a simple process where you have to get the API key and simply use it in your projects. Here I have developed a very simple login form that has a captcha to be solved by using the JavaScript programming language.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60k19pl1jxkrqqxa9tz8.jpg)
While filling out this login form you will see a captcha that’s a hCaptcha. Before we write our code to solve captcha using JavaScript we have to bring API key from their official site i.e. [2Captcha.](https://2captcha.com/)

## How to get API Key?

To activate your API key it has a very simple 4 steps to follow.

**Step 1:** Go to [2Captcha](https://2captcha.com/). Here you have to sign up.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilab1bcehzizwzg4pr5b.jpg)
**Step 2:** Click on the API Key section.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwzprac9pe7adrbrqe1r.jpg)
**Step 3:** Scroll down a little bit where you will notice this(see on the image). Click on your **account settings page**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/an3x4z4q7ut9hwsoq4ww.jpg)
**Step 4:** Copy your API key.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ibcrpjad8dtyq449klk.jpg)
Now we have our own API key it’s time to solve the captcha using JavaScript. Firstly, we have to install two libraries called axios and 2captcha. The axios library is mainly used to handle all the API calls and the 2captcha library is used to solve the captcha itself.

Create your file and Install these 2 packages.

```
npm install axios

npm install 2captcha
```
After installing these 2 libraries it’s time to import them with the help of require method. let their name will be axios and captcha.

```
const axios = require("axios");

const captcha = require("2captcha");
```
Now it’s time to initialize your API key with the captcha. We have already taken out the API key.

```
const result = new Captcha.Solver("API_KEY");
```
Basically, we have initialized the result(which you can see above) through which we will solve the captcha. To do this we have to create an async function named as logIn. Usually, it requires two parameters first one is your site key and the second one is your website URL.

```
 const logIn= async () => {
  console.log("solving...");
  const { data } = await result.hcaptcha(
    "a6631pa5-7e1p-68pc-5c1f-c9y4ef12it45",
    "https://login.com/login"
  );
```
So now we have to use a **try-catch statement**. We will put all the things in the try statement so that if there’s an error we can make the axios call.

```
 try {
    let result = await axios.post("https://discord.com/api/v9/auth/register", {
        captcha_key: data,
        consent: true,
        username: "prakashmishra",
        email_adress: "prakash@gmail.com",
        select_your_car: "audia4",
        enter_your_address: "address",
        enter_your_phone_number: "***********",
    });
    console.log(result.data);
} catch (err) {
    console.log(err);
}
};
```
Lastly, run node file_name i.e. node app.js on the terminal.

Usually, it will take some time to receive a token. To pass the token you have to use the below code.

```
function login(token) {
    setInterval(() => {
        document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = `"${token}"`
    }, 50);
    setTimeout(() => {
        location.reload();
    }, 2500);
}
```
Paste this code on your console and you will login to your website without solving the captcha. Isn’t amazing. In my opinion, this is one of the best things. You just have to write a few lines of code that will save you a lot of time.

## Conclusion

CAPTCHAs are often used in computing as a way of limiting abuse from bots such as spam, denial-of-service attacks, or other malicious input. These tests can be seen on many websites and when logging into an account that requires verification.

With the help of 2Captcha, it’s easy to use that quickly and easily turns your manual tasks of character recognition into automated processes.

I hope this blog helps you and understand how this 2Captcha works.

If you find it helpful then please share it with your friend and colleagues and show some support. Let me know if you have any doubts please comment regarding MIT App Inventor.