Logo of «2Captcha»To home page
Captcha bypass tutorials

Was this helpful?

How to bypass captcha using C++

Gregory Fisher
Gregory Fisher

Technical engineer

Introduction

C++ is traditionally used for building high-load applications, game bots, low-level automation, and parsers where maximum performance and minimal resource consumption are critical. When developing such software, developers often face the need to bypass captchas. Writing custom HTTP requests using libcurl, manually implementing polling loops, and parsing JSON responses significantly complicates the code and distracts from solving core tasks.

In this guide, we will look at how to use the official C++ library to integrate with the 2Captcha API. We will cover the connection process, client initialization, solving popular captcha types, and proper error handling.

Installation and setup

The library is distributed as source code and can be integrated into your project using CMake or vcpkg. The library requires installed libcurl and any modern compiler version supporting C++11 or higher.

If you are using CMake, you can add the library as a submodule or connect it via FetchContent:

cmake Copy
cmake_minimum_required(VERSION 3.10)
project(CaptchaSolver)

set(CMAKE_CXX_STANDARD 11)

find_package(CURL REQUIRED)

add_executable(CaptchaSolver main.cpp)
target_include_directories(CaptchaSolver PRIVATE path/to/2captcha-cpp/include)
target_link_libraries(CaptchaSolver PRIVATE ${CURL_LIBRARIES} path/to/2captcha-cpp/lib/libtwocaptcha.a)

After building and linking the library, include the main header file in your code:

cpp Copy
#include <iostream>
#include <two_captcha/two_captcha.h>

Client initialization

To get started, you need to create an instance of the TwoCaptcha class and pass your API key to it. For security reasons, it is recommended not to store the key directly in the source code, but to get it from environment variables or configuration files.

cpp Copy
#include <iostream>
#include <cstdlib>
#include <two_captcha/two_captcha.h>

int main() {
    const char* apiKey = std::getenv("CAPTCHA_API_KEY");
    if (!apiKey) {
        std::cerr << "API key not found in environment variables." << std::endl;
        return 1;
    }

    try {
        TwoCaptcha solver(apiKey);
        std::cout << "Client initialized successfully." << std::endl;
        
        // Further captcha solving code
        
    } catch (const std::exception& e) {
        std::cerr << "Initialization error: " << e.what() << std::endl;
    }

    return 0;
}

Solving different types of captchas

The library interface is designed in an object-oriented way: each captcha type has its own class. The solve method automatically sends the task to the server, waits for it to be solved, and returns the final result.

1. GeeTest v4

GeeTest is a popular behavioral captcha often found on Asian websites, online games, and fintech services. Version 4 requires a captchaId and the page URL.

cpp Copy
try {
    GeeTestV4 captcha("abcdef12-3456-7890-abcd-ef1234567890", "https://example.com/login");
    solver.solve(captcha);
    std::cout << "lot_number: " << captcha.getSolution()["lot_number"] << std::endl;
    std::cout << "captcha_output: " << captcha.getSolution()["captcha_output"] << std::endl;
    std::cout << "pass_token: " << captcha.getSolution()["pass_token"] << std::endl;
    std::cout << "gen_time: " << captcha.getSolution()["gen_time"] << std::endl;
} catch (const std::exception& e) {
    std::cerr << "Solving error: " << e.what() << std::endl;
}

2. FunCaptcha (Arkose Labs)

FunCaptcha is an interactive captcha with tasks like rotating objects or selecting pictures, often used in the gaming industry. It requires a public key (skey) and the page URL.

cpp Copy
try {
    FunCaptcha captcha("E8A75615-1CDA-5D62-D310-2378D456F0B3", "https://example.com/signup");
    captcha.setServiceUrl("https://example-api.arkoselabs.com");
    solver.solve(captcha);
    std::cout << "FunCaptcha token: " << captcha.code() << std::endl;
} catch (const std::exception& e) {
    std::cerr << "Solving error: " << e.what() << std::endl;
}

3. Capy Puzzle

Capy Puzzle is a visual captcha where the user needs to assemble a puzzle or move an element to the correct position. It requires a captchaKey and the page URL.

cpp Copy
try {
    Capy captcha("PUZLE_abcdef1234567890", "https://example.com/register");
    solver.solve(captcha);
    std::cout << "Answer: " << captcha.getSolution()["answer"] << std::endl;
    std::cout << "Challenge Key: " << captcha.getSolution()["challenge_key"] << std::endl;
} catch (const std::exception& e) {
    std::cerr << "Solving error: " << e.what() << std::endl;
}

Full list of supported captcha types

Currently, the 2Captcha service supports the following main captcha types, for which the library implements corresponding classes or which can be solved via universal methods: Normal, reCAPTCHA v2, reCAPTCHA v3, Cloudflare Turnstile, GeeTest v3 and v4, FunCaptcha, KeyCaptcha, Capy Puzzle, Lemin, Yidun, Amazon WAF, MTCaptcha, CutCaptcha, Tencent, DataDome, CyberSiARA, Coordinates, and Rotate.

Additional settings

Working via proxy

If the target website strictly checks IP addresses and binds the captcha to a specific proxy, you can pass the proxy parameters directly to the captcha object before calling the solve method.

cpp Copy
try {
    GeeTestV4 captcha("abcdef12-3456-7890-abcd-ef1234567890", "https://example.com/login");
    captcha.setProxy("HTTP", "login:password@198.51.100.10:8080");
    solver.solve(captcha);
    std::cout << "Token received successfully." << std::endl;
} catch (const std::exception& e) {
    std::cerr << "Solving error: " << e.what() << std::endl;
}

Timeout configuration

By default, the library waits for a captcha solution for 120 seconds, and the interval between status requests is 10 seconds. These parameters can be overridden via the solver object's methods.

cpp Copy
solver.setDefaultTimeout(300);  // Maximum waiting time in seconds
solver.setPollingInterval(15);  // Interval between status requests in seconds

Error handling

When errors occur, the library throws standard C++ exceptions (std::exception or its descendants), the message of which contains the code and description of the error from the API. This makes it easy to catch and log problems using try-catch blocks.

cpp Copy
try {
    FunCaptcha captcha("E8A75615-1CDA-5D62-D310-2378D456F0B3", "https://example.com/signup");
    solver.solve(captcha);
    std::cout << "Captcha successfully solved: " << captcha.code() << std::endl;
} catch (const std::exception& e) {
    std::string errorMsg = e.what();
    std::cerr << "Captcha solving error: " << errorMsg << std::endl;
    
    if (errorMsg == "ERROR_ZERO_BALANCE") {
        std::cerr << "Insufficient funds on balance." << std::endl;
        // Zero balance handling logic
    }
}

Common error codes

When exceptions occur, the library translates API error codes into understandable string messages. The main ones are presented in the table:

Error Code Description Recommendation
ERROR_WRONG_USER_KEY Invalid API key format or value Check the key correctness in the configuration
ERROR_ZERO_BALANCE Insufficient funds on balance Top up your account in the 2Captcha dashboard
ERROR_NO_SLOT_AVAILABLE No free workers available Retry after 10–15 seconds
ERROR_BAD_PARAMETERS Required parameters are missing Check parameter compliance with the documentation
ERROR_CAPTCHA_UNSOLVABLE Captcha was not solved Funds are refunded, try refreshing the captcha

Solution accuracy reports

To maintain high recognition quality and refund funds for incorrect solutions, use the report submission methods. They are called on the solver object and take the ID of the solved task.

cpp Copy
// If the website accepted the token and the form was successfully submitted
solver.reportCorrect(captcha.getId());

// If the website rejected the token with a validation error
solver.reportIncorrect(captcha.getId());

Conclusion

The C++ library for 2Captcha takes over all the routine work of interacting with the API: it hides polling loops, validates input parameters, and returns ready-made tokens or recognized text. Thanks to strict typing and an object-oriented approach, it easily and safely integrates into both console utilities and complex high-load systems.

The service supports dozens of different captcha types, from simple text ones to complex behavioral systems. This means that no matter what kind of protection you encounter, you will always have a reliable and high-performance tool to bypass it in C++.

To get started, just connect the library to your project, initialize the client with your API key, and call the required method. If necessary, you can easily add proxy support or configure timeouts for the specifics of your application.