Explainers

CAPTCHA Solving API Key Security: Storage and Rotation

Your CaptchaAI API key controls access to your balance. Exposing it means someone else can use your funds. Here's how to keep it safe.


Common Mistakes

Mistake Risk Impact
Hardcoded in source code Key exposed in Git Balance drained
Committed to public repo Key found by bots Immediate abuse
Shared in chat/email Forwarded or leaked Unauthorized use
Logged in plaintext Visible in log files Exposure to log readers
Same key for dev and prod Dev env compromise Prod balance affected

import os


def get_api_key():
    """Load API key from environment variable."""
    key = os.environ.get("CAPTCHAAI_API_KEY")
    if not key:
        raise RuntimeError(
            "CAPTCHAAI_API_KEY not set. "
            "Set it via: export CAPTCHAAI_API_KEY=your_key"
        )
    return key


# Usage
api_key = get_api_key()

Set the environment variable:

# Linux/macOS
export CAPTCHAAI_API_KEY="your_api_key_here"

# Windows PowerShell
$env:CAPTCHAAI_API_KEY = "your_api_key_here"

# Windows CMD
set CAPTCHAAI_API_KEY=your_api_key_here

.env File with python-dotenv

# .env file (add to .gitignore!)
# CAPTCHAAI_API_KEY=your_api_key_here

from dotenv import load_dotenv
import os

load_dotenv()  # Load from .env file

api_key = os.environ["CAPTCHAAI_API_KEY"]

Critical: Add .env to .gitignore:

# .gitignore
.env
.env.local
.env.production

Configuration File Pattern

import json
import os


def load_config(config_path="config.json"):
    """Load API key from config file outside repo."""
    # Use home directory for config
    if not os.path.isabs(config_path):
        config_path = os.path.join(os.path.expanduser("~"), ".captchaai", config_path)

    if not os.path.exists(config_path):
        raise FileNotFoundError(
            f"Config not found: {config_path}\n"
            f"Create it with: {{'api_key': 'your_key'}}"
        )

    with open(config_path, "r") as f:
        config = json.load(f)

    return config["api_key"]

Prevent Accidental Logging

import logging


class SecureApiKey:
    """API key wrapper that prevents accidental logging."""

    def __init__(self, key):
        self._key = key

    @property
    def value(self):
        return self._key

    def __str__(self):
        """Show only last 4 chars in logs."""
        return f"***{self._key[-4:]}"

    def __repr__(self):
        return f"SecureApiKey(***{self._key[-4:]})"


# Usage
api_key = SecureApiKey(os.environ["CAPTCHAAI_API_KEY"])

logging.info(f"Using API key: {api_key}")  # Logs: "Using API key: ***ab12"

# Use .value when actually making API calls
requests.post("https://ocr.captchaai.com/in.php", data={
    "key": api_key.value,  # Actual key value
    # ...
})

Git Pre-Commit Hook

Prevent committing API keys:

#!/usr/bin/env python3
"""Git pre-commit hook to detect API keys."""
# Save as .git/hooks/pre-commit and chmod +x

import subprocess
import sys
import re

# Patterns that might be API keys
KEY_PATTERNS = [
    r'CAPTCHAAI_API_KEY\s*=\s*["\'][a-f0-9]{32}',
    r'key["\']:\s*["\'][a-f0-9]{32}',
    r'"key":\s*"[a-f0-9]{32}"',
]

def check_staged_files():
    result = subprocess.run(
        ["git", "diff", "--cached", "--name-only"],
        capture_output=True, text=True,
    )
    files = result.stdout.strip().split("\n")

    for filepath in files:
        if not filepath or filepath.endswith((".pyc", ".png", ".jpg")):
            continue
        try:
            diff = subprocess.run(
                ["git", "diff", "--cached", filepath],
                capture_output=True, text=True,
            )
            for pattern in KEY_PATTERNS:
                if re.search(pattern, diff.stdout, re.IGNORECASE):
                    print(f"BLOCKED: Possible API key in {filepath}")
                    print("Use environment variables instead of hardcoding keys.")
                    return 1
        except Exception:
            continue
    return 0

sys.exit(check_staged_files())

Key Rotation Workflow

import os
import time


class KeyRotator:
    """Support multiple API keys for rotation."""

    def __init__(self):
        self.keys = self._load_keys()
        self._current_index = 0

    def _load_keys(self):
        """Load API keys from environment."""
        keys = []
        # Primary key
        primary = os.environ.get("CAPTCHAAI_API_KEY")
        if primary:
            keys.append(primary)

        # Additional keys: CAPTCHAAI_API_KEY_2, _3, etc.
        for i in range(2, 10):
            key = os.environ.get(f"CAPTCHAAI_API_KEY_{i}")
            if key:
                keys.append(key)

        if not keys:
            raise RuntimeError("No API keys configured")
        return keys

    def get_key(self):
        """Get current API key."""
        return self.keys[self._current_index]

    def rotate(self):
        """Switch to next key."""
        self._current_index = (self._current_index + 1) % len(self.keys)

    def on_error(self, error):
        """Rotate key on balance or auth errors."""
        if "ZERO_BALANCE" in str(error) or "WRONG_USER_KEY" in str(error):
            self.rotate()

Security Checklist

Check Status
API key not in source code Required
.env file in .gitignore Required
Key loaded from environment Required
Logging masks key value Recommended
Different keys for dev/prod Recommended
Pre-commit hook installed Recommended
Key rotation plan documented Optional

FAQ

What if my key is exposed?

Immediately generate a new API key from CaptchaAI dashboard. The old key remains active until you regenerate, so act fast to prevent unauthorized usage.

Should I use different keys for different projects?

Yes, if possible. Separate keys let you track usage per project and limit blast radius if one key is exposed.

Can I restrict my key to specific IPs?

Yes. CaptchaAI supports IP whitelisting. See the IP Whitelisting guide for setup instructions.



Secure your integration — manage keys at CaptchaAI.

Discussions (0)

No comments yet.

Related Posts

Tutorials CaptchaAI Webhook Security: Validating Callback Signatures
Secure your Captcha AI callback/pingback endpoints — validate request origins, implement HMAC signatures, and protect against replay attacks.

Secure your Captcha AI callback/pingback endpoints — validate request origins, implement HMAC signatures, and...

Automation Python All CAPTCHA Types
Feb 15, 2026
API Tutorials CaptchaAI IP Whitelisting and API Key Security
Secure your Captcha AI API key with IP whitelisting, environment variables, and access control best practices.

Secure your Captcha AI API key with IP whitelisting, environment variables, and access control best practices.

Automation Python All CAPTCHA Types
Jan 30, 2026
DevOps & Scaling Blue-Green Deployment for CAPTCHA Solving Infrastructure
Implement blue-green deployments for CAPTCHA solving infrastructure — zero-downtime upgrades, traffic switching, and rollback strategies with Captcha AI.

Implement blue-green deployments for CAPTCHA solving infrastructure — zero-downtime upgrades, traffic switchin...

Automation Python All CAPTCHA Types
Apr 07, 2026
Reference CAPTCHA Solving Performance by Region: Latency Analysis
Analyze how geographic region affects Captcha AI solve times — network latency, proxy location, and optimization strategies for global deployments.

Analyze how geographic region affects Captcha AI solve times — network latency, proxy location, and optimizati...

Automation Python All CAPTCHA Types
Apr 05, 2026
Tutorials Streaming Batch Results: Processing CAPTCHA Solutions as They Arrive
Process CAPTCHA solutions the moment they arrive instead of waiting for tasks to complete — use async generators, event emitters, and callback patterns for stre...

Process CAPTCHA solutions the moment they arrive instead of waiting for all tasks to complete — use async gene...

Automation Python All CAPTCHA Types
Apr 07, 2026
DevOps & Scaling Ansible Playbooks for CaptchaAI Worker Deployment
Deploy and manage Captcha AI workers with Ansible — playbooks for provisioning, configuration, rolling updates, and health checks across your server fleet.

Deploy and manage Captcha AI workers with Ansible — playbooks for provisioning, configuration, rolling updates...

Automation Python All CAPTCHA Types
Apr 07, 2026
Tutorials Bulkhead Pattern: Isolating CAPTCHA Solving Failures
Apply the bulkhead pattern to isolate CAPTCHA solving failures — partition resources into independent pools so a slow or failing solver type doesn't starve othe...

Apply the bulkhead pattern to isolate CAPTCHA solving failures — partition resources into independent pools so...

Automation Python All CAPTCHA Types
Apr 07, 2026
API Tutorials Graceful Degradation When CAPTCHA Solving Fails
Keep your automation running when CAPTCHA solving fails — fallback strategies, queue-based retries, and degraded-mode patterns.

Keep your automation running when CAPTCHA solving fails — fallback strategies, queue-based retries, and degrad...

Automation Python All CAPTCHA Types
Apr 06, 2026
Tutorials Discord Webhook Alerts for CAPTCHA Pipeline Status
Send CAPTCHA pipeline alerts to Discord — webhook integration for balance warnings, error spikes, queue status, and daily summary reports with Captcha AI.

Send CAPTCHA pipeline alerts to Discord — webhook integration for balance warnings, error spikes, queue status...

Automation Python All CAPTCHA Types
Tutorials Profiling CAPTCHA Solving Bottlenecks in Python Applications
Profile Python CAPTCHA solving scripts to identify bottlenecks — timing breakdowns, c Profile, line_profiler, and async profiling for Captcha AI integrations.

Profile Python CAPTCHA solving scripts to identify bottlenecks — timing breakdowns, c Profile, line_profiler,...

Automation Python All CAPTCHA Types
Apr 04, 2026
Explainers reCAPTCHA v2 Invisible: Trigger Detection and Solving
Detect and solve re CAPTCHA v 2 Invisible challenges with Captcha AI — identify triggers, extract parameters, and handle auto-invoked CAPTCHAs.

Detect and solve re CAPTCHA v 2 Invisible challenges with Captcha AI — identify triggers, extract parameters,...

Automation Python reCAPTCHA v2
Apr 07, 2026
Explainers How BLS CAPTCHA Works: Grid Logic and Image Selection
Deep dive into BLS CAPTCHA grid logic — how images are arranged, how instructions map to selections, and how Captcha AI processes BLS challenges.

Deep dive into BLS CAPTCHA grid logic — how images are arranged, how instructions map to selections, and how C...

Automation BLS CAPTCHA
Apr 09, 2026
Explainers How BLS CAPTCHA Works
Understand how BLS CAPTCHA works on visa appointment systems.

Understand how BLS CAPTCHA works on visa appointment systems. Learn about its image selection mechanism, how i...

Automation BLS CAPTCHA
Apr 06, 2026