API Tutorials

Phrase, MinLen, and MaxLen Parameters for Image CAPTCHA

Constraining the expected answer length and format improves solve accuracy. These parameters eliminate impossible answers before they're returned.


Parameter Reference

Parameter Type Values Description
phrase int 0, 1 0 = single word, 1 = phrase (with spaces)
minLen int 1-20 Minimum answer length
maxLen int 1-20 Maximum answer length

How Length Constraints Help

Without constraints, the solver might return:

  • "ABCD" when the CAPTCHA has 6 characters
  • "ABCDEFGH" when there are only 4
  • Extra noise characters

With minLen=6, maxLen=6, the solver knows exactly how many characters to find.


Fixed-Length CAPTCHAs

Many CAPTCHAs use a consistent character count:

import requests
import base64
import time
import os

API_KEY = os.environ["CAPTCHAAI_API_KEY"]


def solve_fixed_length(image_b64, length):
    """Solve a CAPTCHA with a known fixed character count."""
    resp = requests.post("https://ocr.captchaai.com/in.php", data={
        "key": API_KEY,
        "method": "base64",
        "body": image_b64,
        "minLen": length,
        "maxLen": length,
        "json": 1,
    }, timeout=30)
    return resp.json()


# Common fixed-length patterns:
# 4-character: solve_fixed_length(b64, 4)
# 5-character: solve_fixed_length(b64, 5)
# 6-character: solve_fixed_length(b64, 6)

Variable-Length CAPTCHAs

Set a range when the length varies:

def solve_variable_length(image_b64, min_len, max_len):
    """Solve when character count varies within a range."""
    resp = requests.post("https://ocr.captchaai.com/in.php", data={
        "key": API_KEY,
        "method": "base64",
        "body": image_b64,
        "minLen": min_len,
        "maxLen": max_len,
        "json": 1,
    }, timeout=30)
    return resp.json()


# Examples:
# Banking (4-6 digits): solve_variable_length(b64, 4, 6)
# Forum (5-8 mixed):    solve_variable_length(b64, 5, 8)
# Simple (3-5 chars):   solve_variable_length(b64, 3, 5)

The Phrase Parameter

The phrase parameter handles CAPTCHAs with spaces between words:

def solve_phrase_captcha(image_b64):
    """Solve a CAPTCHA that contains a phrase (words with spaces)."""
    resp = requests.post("https://ocr.captchaai.com/in.php", data={
        "key": API_KEY,
        "method": "base64",
        "body": image_b64,
        "phrase": 1,       # Answer may contain spaces
        "json": 1,
    }, timeout=30)
    return resp.json()


# phrase=0 (default): "hello world" → might return "helloworld"
# phrase=1: "hello world" → returns "hello world" (preserves spaces)

Detecting Length from the Page

# detect_length.py
import re


def detect_captcha_length(page_html):
    """Auto-detect expected CAPTCHA length from page HTML."""
    hints = {}

    # Check input maxlength attribute
    match = re.search(
        r'<input[^>]*captcha[^>]*maxlength="(\d+)"',
        page_html, re.IGNORECASE,
    )
    if match:
        hints["maxLen"] = int(match.group(1))

    # Check minlength
    match = re.search(
        r'<input[^>]*captcha[^>]*minlength="(\d+)"',
        page_html, re.IGNORECASE,
    )
    if match:
        hints["minLen"] = int(match.group(1))

    # Check pattern attribute
    match = re.search(
        r'<input[^>]*captcha[^>]*pattern="([^"]+)"',
        page_html, re.IGNORECASE,
    )
    if match:
        pattern = match.group(1)
        # Pattern like [a-zA-Z0-9]{6} → length is 6
        len_match = re.search(r'\{(\d+)\}', pattern)
        if len_match:
            length = int(len_match.group(1))
            hints["minLen"] = length
            hints["maxLen"] = length
        # Pattern like [a-zA-Z0-9]{4,8} → range 4-8
        range_match = re.search(r'\{(\d+),(\d+)\}', pattern)
        if range_match:
            hints["minLen"] = int(range_match.group(1))
            hints["maxLen"] = int(range_match.group(2))

    # Check for phrase hints
    lower = page_html.lower()
    if "words" in lower or "phrase" in lower or "spaces" in lower:
        hints["phrase"] = 1

    return hints


# Usage
html = '<input id="captcha" maxlength="6" minlength="6">'
hints = detect_captcha_length(html)
# {"maxLen": 6, "minLen": 6}

Combining Parameters

# Common parameter combinations:

# 4-digit PIN CAPTCHA
pin_params = {
    "numeric": 1,
    "minLen": 4,
    "maxLen": 4,
    "phrase": 0,
}

# 6-char mixed CAPTCHA (case-sensitive)
mixed_params = {
    "regsense": 1,
    "minLen": 6,
    "maxLen": 6,
    "phrase": 0,
}

# Two-word phrase CAPTCHA
phrase_params = {
    "phrase": 1,
    "minLen": 6,
    "maxLen": 15,
    "language": 2,
}

# 5-8 digit number
number_params = {
    "numeric": 1,
    "minLen": 5,
    "maxLen": 8,
    "phrase": 0,
}

Validation After Solving

# validate.py


def validate_answer(answer, expected_min=None, expected_max=None, is_phrase=False):
    """Validate the solve result matches expectations."""
    if not answer:
        return False

    length = len(answer)

    if expected_min and length < expected_min:
        return False
    if expected_max and length > expected_max:
        return False

    if not is_phrase and " " in answer:
        return False  # Unexpected space

    return True

Troubleshooting

Issue Cause Fix
Answer too short Noise swallowed a character Set minLen to force minimum length
Answer too long Noise read as extra character Set maxLen to cap length
Spaces in answer Phrase CAPTCHA Set phrase=1
No spaces when expected Default phrase=0 Enable phrase=1 for multi-word CAPTCHAs

FAQ

What if I don't know the exact length?

Set a range. If the CAPTCHA usually has 4-6 characters, use minLen=4, maxLen=6. This is better than no constraint.

Can I use minLen and maxLen with token-based CAPTCHAs?

No. These parameters only apply to image/OCR CAPTCHAs (method=base64 or method=post). Token-based CAPTCHAs (reCAPTCHA, Turnstile) ignore them.

Does phrase=1 affect accuracy?

It can. Phrase mode tells the solver to look for word boundaries. Only use it when the CAPTCHA actually contains spaces.



Constrain and conquer — start with CaptchaAI.

Full Working Code

Complete runnable examples for this article in Python, Node.js, PHP, Go, Java, C#, Ruby, Rust, Kotlin & Bash.

View on GitHub →

Discussions (0)

No comments yet.