const axios = require('axios');
const sleep = require('util').promisify(setTimeout);
const API_KEY = 'YOUR_API_KEY_2CAPTCHA'; // Replace with your real API key
// Function for reCAPTCHA v2 solution
async function solveReCaptchaV2(siteKey, pageUrl) {
try {
// Sending a request for the captcha solution
const sendCaptchaResponse = await axios.post(`http://2captcha.com/in.php`, null, {
params: {
key: API_KEY,
method: 'userrecaptcha',
googlekey: siteKey,
pageurl: pageUrl,
json: 1
}
});
if (sendCaptchaResponse.data.status !== 1) {
throw new Error(`Error when sending captcha: ${sendCaptchaResponse.data.request}`);
}
const requestId = sendCaptchaResponse.data.request;
console.log(`Captcha sent, request ID: ${RequestId}`);
// Waiting for the captcha solution
while (true) {
await sleep(5000); // Waiting 5 seconds before the next request
const getResultResponse = await axios.get(`http://2captcha.com/res.php`, {
params: {
key: API_KEY,
action: 'get',
id: requestId,
json: 1
}
});
if (getResultResponse.data.status === 1) {
console.log('Captcha solved successfully.');
return getResultResponse.data.request;
} else if (getResultResponse.data.request === 'CAPCHA_NOT_READY') {
console.log('The captcha has not been solved yet, waiting...');
} else {
throw new Error(`Error while solving captcha: ${getResultResponse.data.request}`);
}
}
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
}
// Function for reCAPTCHA v3 solution
async function solveReCaptchaV3(siteKey, pageUrl, action = 'verify', minScore = 0.3) {
try {
// Sending a request for the captcha solution
const sendCaptchaResponse = await axios.post(`http://2captcha.com/in.php`, null, {
params: {
key: API_KEY,
method: 'userrecaptcha',
googlekey: siteKey,
pageurl: pageUrl,
version: 'v3',
action: action,
min_score: minScore,
json: 1
}
});
if (sendCaptchaResponse.data.status !== 1) {
throw new Error(`Error when sending captcha: ${sendCaptchaResponse.data.request}`);
}
const requestId = sendCaptchaResponse.data.request;
console.log(`Captcha sent, request ID: ${RequestId}`);
// Waiting for the captcha solution
while (true) {
await sleep(5000); // Waiting 5 seconds before the next request
const getResultResponse = await axios.get(`http://2captcha.com/res.php`, {
params: {
key: API_KEY,
action: 'get',
id: requestId,
json: 1
}
});
if (getResultResponse.data.status === 1) {
console.log('Captcha solved successfully.');
return getResultResponse.data.request;
} else if (getResultResponse.data.request === 'CAPCHA_NOT_READY') {
console.log('The captcha has not been solved yet, waiting...');
} else {
throw new Error(`Error while solving captcha: ${getResultResponse.data.request}`);
}
}
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
}
// Usage example for reCAPTCHA v2
(async () => {
const siteKeyV2 = 'YOUR_SITE_KEY_V2'; // Replace with the real site key
const pageUrlV2 = 'https://example.com '; // Replace with the real URL of the page
const tokenV2 = await solveReCaptchaV2(siteKeyV2, pageUrlV2);
console.log(`Received token for reCAPTCHA v2: ${tokenV2}`);
})();
// Usage example for reCAPTCHA v3
(async () => {
const siteKeyV3 = 'YOUR_SITE_KEY_V3'; // Replace with the real site key
const pageUrlV3 = 'https://example.com '; // Replace with the real URL of the page
const action = 'homepage'; // Replace with the corresponding action
const MinScore = 0.5; // Set the minimum allowed score
const tokenV3 = await solveReCaptchaV3(siteKeyV3, pageUrlV3, action, minScore);
console.log(`Received token for reCAPTCHA v3: ${tokenV3}`);
})();