DEV Community

Cover image for Capsolver: The Easiest Way to Solve Captchas
Kinanee Samson
Kinanee Samson

Posted on

Capsolver: The Easiest Way to Solve Captchas

Capsolver is an automated solution for captchas that is very fast and reliable. Capsolver provides the cheapest and fastest automatic captcha solution service available on the internet. But why are we even talking about Capsolver and what do we mean when we use the term captcha? In today's article, we will take a look at what Capsolver is and how we can utilize their API to automate the process of solving captchas thus we will consider the following.

  • What is a captcha?
  • Capsolver
  • How Capsolver Works
  • How much does it cost?
  • Why even bother?

What is a captcha?

A captcha is a kind of test used to distinguish humans from web crawlers, there are many instances where bots are used to crawl up websites, to get information off the web. These robots interact with a page as a human does and they can also fill out and submit forms too, however, the information provided to us by these bots is of no use since they are fake and autogenerated. It makes no sense to populate our database with bogus data. This is where captchas come in.

A captcha test is a way to distinguish humans from computers, this works by providing simple tests that humans can understand but makes no sense to a robot. This helps to prevent bots from spamming us with loads of useless information. However captchas are not all that rosy, they are notorious for contributing to bad user experience due to the time and effort it takes to complete them, and most users would rather abandon a captcha and even leave the website entirely than complete it. If you have a website with a lot of content, you need to pay closer attention because, with a captcha service like Capsolver, you can protect your data while delivering a good experience to your users.

What is Capsolver?

Capsolver allows you to greatly simplify the process of captcha completion for your users by completely automating the process of solving the captcha. Capsolver is based on a Machine Learning algorithm that enables it to scale up easily. Capsolver provides developers with an easy-to-use API that can be integrated into your application with minimal effort. Capsolver is highly reliable and can solve several different captchas ranging from text captcha to reCaptcha, hCaptcha, and even funCaptcha.

Why is Capsolver using this approach? Most existing captcha automation solutions out there just funnel your captcha to another human to physically solve them, although this model has the advantage of the captcha being solved by a human it has the terrible disadvantage of being very difficult to apply at scale due to the limited amount of humans ready to solve the captcha and also the limited amount time of each user can spend on solving captchas.

With Capsolver all of these concerns have been taken care of and as such you are guaranteed reliable results that are fast to obtain. You don’t have to worry about sending a large number of captcha requests because the system is designed to scale up as your requests increase.

How Capsolver works

Here’s a quick breakdown of how easy it is to automate a captcha solution with Capsolver in four easy steps.

  • First, you need to head over to Capsolver to create an account.
  • If you successfully created your account then you can head over to your dashboard to obtain your API keys.
  • Then you need to query the Capsolver API to set up a task.
  • Finally, you need to track the task you just created to see its status.

Setting up a task

Capsolver’s API is very capable and here’s a list of the kind of captchas that you can automate with Capsolver;

  • Image recognition/Voice recognition with FunCaptcha, HCaptcha, ReCaptchaV2, and AwsWAF
  • Token-based captcha which covers captchas like maths problems in a captcha and other types of complex captchas.

To create an account with Capsolver you need to head to the Capsolver website, if you successfully created an account you will be redirected to your dashboard where you can see and subsequently copy your API key. Open up your favorite editor and let’s create a simple task to help us automate the process of solving a captcha for converting image to text.

const ur = 'https://api.capsolver.com/createTask';
const task = {
  "client key": "API_KEY",
  "task":{
        "type": "ImageToTextTask",
        "module": "queue",
        "body": "/5l/kZJRQS4AAgABA......" # base64 encoded image
   }
};

fetch(url, {
  method: 'POST',
  headers: { 'Content-Type': 'application/JSON'},
  body: task
}).then((res) => {
  if (res. ok) return res.json()
}).then((data) => {
  console.log(data);
})

// {
//  "error": 0,
//  "errorCode": ",
//  "error description": ",
//  "status": "ready",
//  "solution": {
//    "text": "44795sds"
//  },
//  "taskId": "2376919c-1863-11ec-a012-94e6f7355a0b",
}
Enter fullscreen mode Exit fullscreen mode

Handling response

Let's go over the response from Capsolver's API, this type of request will contain the solution to the captcha as part of the response from the API. If there's an error with our request the errorId, errorCode, and errorDescription will provide information with regards to the error, read more about handling errors. The status tells us about the state of the task and if everything goes according to plan its value will be ready. The solution property on the response is where the solution to the captcha will be. This is an object with a text property that stores the image converted to a text, you can use this text to fill out the captcha form and submit it automatically without the user having to do anything.

reCaptcha

Let's see how we can automate a reCaptcha test. reCaptcha is a captcha service provided by Google. reCaptcha uses an advanced risk analysis engine to protect your data. reCaptcha is very easy to setup and use on your website. I'm going to assume you already know how to do this, otherwise visit the reCaptcha documentation. Let's setup a task that will automate the process of solving a reCaptcha v3 test. This is going to be a node project so ensure that you have NodeJS installed on your computer.

Spin up a new NodeJS project with the following command

$ npm init --y
Enter fullscreen mode Exit fullscreen mode

Then you need to install axios and express.

$ npm i express axios
Enter fullscreen mode Exit fullscreen mode

Now we've installed the dependencies let's create a basic express server.

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

const app = express ();

app.get('/', (req, res) => {
  res.end("Hello World!")
});

app.listen(3000, () => {
  console.log("app is running out");
})
Enter fullscreen mode Exit fullscreen mode

Now we've setup a basic express server we'll serve an HTML template that contains a Google reCaptcha v3 test. Here's the HTML we are sending down to the client.

<!DOCTYPE html>
<html lang="en">
    <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>CSS GRID</title>

      <script src="https://www.google.com/recaptcha/api.js"></script>
      <script
        src="https://code.jquery.com/jquery-3.7.0.min.js"
        integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g="
        crossorigin="anonymous"
      ></script>
    </head>
    <body>
      <h1>Hello World</h1>

      <button
        class="g-recaptcha"
        data-sitekey="reCaptcha_site_key"
        data-callback="onSubmit"
        data-action="register"
      >
        Submit
      </button>

      <button id="skip" type="button">
        skip
      </button>
    </body>
  </html>
Enter fullscreen mode Exit fullscreen mode

Let's send this file back to the client, from our server. Then we create a handler to setup the task on Capsolver for us.


async function createTask() {
  const response = await axios({
    url: 'https://api.capsolver.com/createTask',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      "clientKey": "CAPSOLVER_API_KEY",
      "task": {
        "type": "ReCaptchaV3TaskProxyLess",
        "websiteURL": "http://localhost:3000",
        "websiteKey": "RECAPTCHA_KEY",
        "pageAction": "register",
        "minScore": 0.7
      }
    }
  });
  const data = response.data;
  return data;
}

app.get('/automate', async (req, res) => {
  const solved = await automate();
  res.json({ solution: solved })
})

app.get('/', (req, res) => {
  res.sendFile(__dirname+'/index.html')
});
Enter fullscreen mode Exit fullscreen mode

Let's edit the html file to automatically send the task once the document is loaded.

<script>
window.addEventListener('DOMContentLoaded', async (event) => {
  fetch('automate', {
    mode: 'no-cors',
  })
  .then((res) => res.json())
  .then((data) => {
     const taskId = data
       .solution.taskId;
  })
   .catch((error) => {
     console.log(error);
   });
 })
</script>
Enter fullscreen mode Exit fullscreen mode

Now when the web page is completely loaded it's going to create a task for us on Capsolver. Let's edit our server to verify the status of the task.

async function trackTask(taskId) {
  const response = await axios({
    url: 'https://api.capsolver.com/getTaskResult',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      "clientKey": "CAPSOLVER-API-KEY",
      taskId,
    }
  });
  const data = response.data;
  return data;
}

app.get("/verify/:taskId", async (req, res) => {
    const {taskId} = req.params;
    const result = await  trackTask(taskId)
    res.json(result);
})
Enter fullscreen mode Exit fullscreen mode

We are going to add the snippet to track the request from the front;

<script>

function verify = async (taskId) => {
    const res = await fetch("verify/"+taskId);
    const data = await res.json();
    return data; 
}

document.addEventListener('ready', async (event) => {
  fetch('automate', {
    mode: 'no-cors',
  })
  .then((res) => res.json())
  .then((data) => {
     const taskId = data
       .solution.taskId;
     setTimeout(async () => {
       const result = await verify(taskId);
       token = result
        .solution
        .gRecaptchaResponse
       console.log(token) 
       // verify token
     }, 500)
  })
  .catch((error) => {
    console.log(error);
  });
})

</script>
Enter fullscreen mode Exit fullscreen mode

With the token obtained from the task, we can query reCaptcha to verify the token and allow the user to continue again uninterrupted.

Pricing

Now we’ve seen how to set up a basic image recognition Captcha and a re-Captcha automated task, let’s take a look at Capsolver’s pricing model. Capsolver has two different pricing models because they’ve taken to account what everyone would like. They have a pay-as-you-go model where you pay a $1/1000 request on the platform, I know some of you would not agree with this idea because you’d rather pay a fixed amount every month. Capsolver has a subscription-based plan that allows you to pay per month for any of the captcha solutions, prices range from $22/month for Gee Test to $225/month for Datadome captcha.

Why Use Capsolver?

I know you are wondering why should I even use Capsolver. Capsolver is fast and it would significantly improve the experience for your users. Head over to Capsolver's official website to create an account today to get started.

Capsolver's API is well documented and covers how you can solve different types of captchas and I suggest that you create an account today to get started, you should also spend some time on their documentation to understand how to solve different types of captchas with Capsolver. I hope you found this useful, thank you for your time and I'll see you in the next one.

Top comments (0)