Back to BlogTutorial

Node.js CAPTCHA Automation: Complete Integration Guide

Build robust CAPTCHA solving into your Node.js apps with TypeScript, async/await patterns, and Puppeteer integration.

reGOTCHA TeamDecember 18, 20257 min read
Node.js CAPTCHA Automation: Complete Integration Guide

Setup

terminal
npm install axios puppeteer

TypeScript CAPTCHA Client

example.ts
import axios from 'axios';

interface TaskResponse {
  errorId: number;
  taskId?: string;
  errorDescription?: string;
}

interface ResultResponse {
  status: 'processing' | 'ready';
  solution?: { gRecaptchaResponse: string };
  errorId?: number;
  errorDescription?: string;
}

class CaptchaSolver {
  private apiKey: string;
  private baseUrl = 'https://api.regotcha.com';

  constructor(apiKey: string) {
    this.apiKey = apiKey;
  }

  async solve(
    websiteUrl: string,
    websiteKey: string,
    action: string = 'submit',
    timeout: number = 120000
  ): Promise<string> {
    // Create task
    const { data: taskData } = await axios.post<TaskResponse>(
      `${this.baseUrl}/createTask`,
      {
        clientKey: this.apiKey,
        task: {
          type: 'ReCaptchaV3EnterpriseTaskProxyless',
          websiteURL: websiteUrl,
          websiteKey: websiteKey,
          pageAction: action,
        },
      }
    );

    if (taskData.errorId) {
      throw new Error(taskData.errorDescription || 'Task creation failed');
    }

    const taskId = taskData.taskId!;
    const startTime = Date.now();

    // Poll for result
    while (Date.now() - startTime < timeout) {
      const { data: result } = await axios.post<ResultResponse>(
        `${this.baseUrl}/getTaskResult`,
        { clientKey: this.apiKey, taskId }
      );

      if (result.status === 'ready') {
        return result.solution!.gRecaptchaResponse;
      }

      if (result.errorId) {
        throw new Error(result.errorDescription || 'Solving failed');
      }

      await new Promise((r) => setTimeout(r, 2000));
    }

    throw new Error('Timeout waiting for CAPTCHA solution');
  }
}

export { CaptchaSolver };

Puppeteer Integration

example.ts
import puppeteer from 'puppeteer';
import { CaptchaSolver } from './captcha-solver';

async function automateLogin() {
  const browser = await puppeteer.launch({ headless: 'new' });
  const page = await browser.newPage();
  const solver = new CaptchaSolver(process.env.REGOTCHA_API_KEY!);

  await page.goto('https://example.com/login');

  // Get site key from page
  const siteKey = await page.evaluate(() => {
    const script = document.querySelector('script[src*="recaptcha"]');
    return script?.getAttribute('src')?.match(/render=([^&]+)/)?.[1];
  });

  if (!siteKey) throw new Error('Site key not found');

  // Solve CAPTCHA
  const token = await solver.solve(
    page.url(),
    siteKey,
    'login'
  );

  // Inject token
  await page.evaluate((t) => {
    const input = document.querySelector('[name="g-recaptcha-response"]');
    if (input) (input as HTMLInputElement).value = t;
  }, token);

  // Submit form
  await page.click('button[type="submit"]');
  await page.waitForNavigation();

  console.log('Login successful!');
  await browser.close();
}

automateLogin().catch(console.error);

Error Handling Patterns

example.ts
import { CaptchaSolver } from './captcha-solver';

async function solveWithRetry(
  solver: CaptchaSolver,
  url: string,
  key: string,
  action: string,
  maxRetries: number = 3
): Promise<string> {
  let lastError: Error | null = null;

  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      return await solver.solve(url, key, action);
    } catch (error) {
      lastError = error as Error;
      console.log(`Attempt ${attempt} failed: ${lastError.message}`);

      if (attempt < maxRetries) {
        await new Promise((r) => setTimeout(r, 2000 * attempt));
      }
    }
  }

  throw lastError;
}
Best Practice: Always handle the token injection timing carefully - some sites validate tokens immediately, while others only check on form submission.
Node.jsTypeScriptPuppeteerTutorial

Ready to solve CAPTCHAs at scale?

Get started with 50 free credits. No credit card required.