Skip to main content

๐Ÿš€ Powerful and simple API key rotator for bypassing rate limits and handling errors

Project description

โœจ Features

๐Ÿ”„ Automatic Key Rotation

  • Seamlessly cycles through API keys
  • Bypasses rate limits effortlessly
  • Removes invalid keys automatically

๐Ÿ” Smart Retry Logic

  • Exponential backoff strategy
  • Intelligent error classification
  • Configurable retry attempts

๐Ÿ›ก๏ธ Anti-Bot Evasion

  • User-Agent rotation
  • Random delays between requests
  • Proxy support

โšก Dual Mode Support

  • Synchronous (requests)
  • Asynchronous (aiohttp)
  • Drop-in replacement

๐Ÿง  Intelligent Headers

  • Auto-detects auth patterns
  • Learns successful configs
  • Persistent configuration

๐Ÿš€ Quick Start

๐Ÿ“ฆ Installation

pip install apikeyrotator

โšก Basic Usage

from apikeyrotator import APIKeyRotator

# Initialize with API keys
rotator = APIKeyRotator(api_keys=["key1", "key2", "key3"])

# Make requests - it's that simple!
response = rotator.get("https://api.example.com/data")
print(response.json())

# The rotator automatically:
# โœ… Rotates keys on rate limits
# โœ… Retries on failures
# โœ… Manages headers intelligently

๐ŸŒŸ Using Environment Variables

Create a .env file:

API_KEYS=key1,key2,key3

Then use without explicit keys:

from apikeyrotator import APIKeyRotator

# Automatically loads from .env
rotator = APIKeyRotator()

response = rotator.get("https://api.example.com/data")

๐Ÿ”ฅ Advanced Configuration

from apikeyrotator import APIKeyRotator

rotator = APIKeyRotator(
    api_keys=["key1", "key2", "key3"],
    
    # Retry & Timeout
    max_retries=5,              # Retry up to 5 times per key
    base_delay=1.0,             # Start with 1s delay
    timeout=15.0,               # 15s request timeout
    
    # Anti-Bot Features
    user_agents=[               # Rotate User-Agents
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64)...",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)..."
    ],
    random_delay_range=(1.0, 3.0),  # Random 1-3s delays
    proxy_list=[                # Rotate proxies
        "http://proxy1.com:8080",
        "http://proxy2.com:8080"
    ]
)

# Now make requests with all these features active!
response = rotator.get("https://api.example.com/data")

๐ŸŒ Asynchronous Usage

import asyncio
from apikeyrotator import AsyncAPIKeyRotator

async def main():
    async with AsyncAPIKeyRotator(api_keys=["key1", "key2"]) as rotator:
        # Make async requests
        response = await rotator.get("https://api.example.com/data")
        data = await response.json()
        print(data)

asyncio.run(main())

๐ŸŽฏ Why APIKeyRotator?

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  โœ“ Effortless API key management                              โ”‚
โ”‚  โœ“ Automatic rate limit handling                              โ”‚
โ”‚  โœ“ Smart retry logic with exponential backoff                 โ”‚
โ”‚  โœ“ Anti-bot evasion (User-Agents, delays, proxies)            โ”‚
โ”‚  โœ“ Both sync and async support                                โ”‚
โ”‚  โœ“ Intelligent header detection and persistence               โ”‚
โ”‚  โœ“ Clean, modern Python with full type hints                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Š Comparison

Feature Manual Management APIKeyRotator
Key rotation โŒ Manual โœ… Automatic
Retry logic โŒ Custom code โœ… Built-in
Rate limit handling โŒ Manual โœ… Automatic
Error classification โŒ Status codes โœ… Intelligent
Anti-bot features โŒ Not included โœ… Complete
Session management โŒ Manual โœ… Optimized
Code complexity โŒ High โœ… Minimal

๐ŸŽจ Use Cases

๐Ÿ•ท๏ธ Web Scraping
from apikeyrotator import APIKeyRotator
from bs4 import BeautifulSoup

# Configure for web scraping
rotator = APIKeyRotator(
    api_keys=["key1", "key2", "key3"],
    user_agents=[
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64)...",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)..."
    ],
    random_delay_range=(1.0, 3.0),
    max_retries=5
)

# Scrape multiple pages
for page in range(1, 11):
    response = rotator.get(f"https://example.com/products?page={page}")
    soup = BeautifulSoup(response.content, 'html.parser')
    # Process data...
๐Ÿ“Š Data Collection
from apikeyrotator import APIKeyRotator

rotator = APIKeyRotator(api_keys=["key1", "key2", "key3"])

# Collect data from multiple endpoints
endpoints = ["/users", "/posts", "/comments", "/analytics"]

data = {}
for endpoint in endpoints:
    response = rotator.get(f"https://api.example.com{endpoint}")
    data[endpoint] = response.json()
    print(f"โœ“ Collected {endpoint}")
๐Ÿ”„ High-Volume Requests
import asyncio
from apikeyrotator import AsyncAPIKeyRotator

async def fetch_all_items(item_ids):
    async with AsyncAPIKeyRotator(api_keys=["key1", "key2", "key3"]) as rotator:
        tasks = [
            rotator.get(f"https://api.example.com/items/{id}")
            for id in item_ids
        ]
        responses = await asyncio.gather(*tasks)
        return [await r.json() for r in responses]

# Fetch 1000 items concurrently
items = asyncio.run(fetch_all_items(range(1000)))
๐Ÿ› ๏ธ Production API Client
from apikeyrotator import APIKeyRotator, AllKeysExhaustedError
from typing import Dict, List

class APIClient:
    def __init__(self, api_keys: List[str]):
        self.rotator = APIKeyRotator(
            api_keys=api_keys,
            max_retries=5,
            base_delay=2.0
        )
    
    def get_user(self, user_id: int) -> Dict:
        try:
            response = self.rotator.get(f"https://api.example.com/users/{user_id}")
            return response.json()
        except AllKeysExhaustedError:
            # Fallback to cache or alternative source
            return self._get_cached_user(user_id)

client = APIClient(api_keys=["key1", "key2", "key3"])
user = client.get_user(123)

๐Ÿ”ง Configuration Options

Parameter Type Default Description
api_keys List[str] or str None API keys to rotate
env_var str "API_KEYS" Environment variable name
max_retries int 3 Max retry attempts per key
base_delay float 1.0 Base delay for exponential backoff
timeout float 10.0 Request timeout in seconds
user_agents List[str] None User-Agent strings to rotate
random_delay_range Tuple[float, float] None Random delay range (min, max)
proxy_list List[str] None Proxy URLs to rotate
error_classifier ErrorClassifier None Custom error classifier

๐Ÿ“š View Complete API Reference โ†’

๐Ÿ›ก๏ธ Error Handling

from apikeyrotator import (
    APIKeyRotator,
    NoAPIKeysError,
    AllKeysExhaustedError
)

try:
    rotator = APIKeyRotator(api_keys=["key1", "key2"])
    response = rotator.get("https://api.example.com/data")
    
except NoAPIKeysError:
    print("โŒ No API keys provided or found")
    
except AllKeysExhaustedError:
    print("โŒ All keys failed after maximum retries")
    # Implement fallback strategy
    
except Exception as e:
    print(f"โŒ Unexpected error: {e}")

Error Classification System:

  • RATE_LIMIT (429): Switches to next key immediately
  • TEMPORARY (5xx): Retries with exponential backoff
  • PERMANENT (401, 403): Removes invalid key from pool
  • NETWORK: Connection errors, retries or switches key

๐Ÿ“– Learn More About Error Handling โ†’

๐ŸŽฏ Advanced Features

๐ŸŽญ Custom Retry Logic

def custom_retry(response):
    if response.status_code == 429:
        return True
    try:
        return 'error' in response.json()
    except:
        return False

rotator = APIKeyRotator(
    api_keys=["key1"],
    should_retry_callback=custom_retry
)

๐Ÿ”‘ Dynamic Headers

def header_callback(key, headers):
    return {
        "Authorization": f"Bearer {key}",
        "X-Client-Version": "2.0"
    }, {}

rotator = APIKeyRotator(
    api_keys=["key1"],
    header_callback=header_callback
)

๐Ÿš€ Explore Advanced Features โ†’

๐Ÿ“š Documentation

Resource Description
๐Ÿ“– Documentation Index Complete documentation hub
๐Ÿš€ Getting Started Quick start guide
๐Ÿ“‹ API Reference Complete API documentation
๐Ÿ’ก Examples Real-world code examples
๐Ÿ”ง Advanced Usage Power features & customization
๐Ÿšจ Error Handling Comprehensive error management
โ“ FAQ Frequently asked questions
๐Ÿ”’ Security Security best practices

๐Ÿงช Testing

# Install test dependencies
pip install pytest pytest-asyncio requests-mock aioresponses

# Run all tests
pytest

# Run with coverage
pytest --cov=apikeyrotator --cov-report=html

# Run specific test file
pytest tests/test_rotator.py -v

๐Ÿค Contributing

Contributions are what make the open-source community amazing! We welcome:

  • ๐Ÿ› Bug reports
  • ๐Ÿ’ก Feature suggestions
  • ๐Ÿ“ Documentation improvements
  • ๐Ÿ”ง Code contributions

๐Ÿ“– Read Contributing Guidelines โ†’

Quick Contribution Steps

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ˆ Performance

APIKeyRotator is optimized for production use:

  • โšก Connection Pooling: Reuses TCP connections
  • ๐Ÿง  Smart Caching: Caches successful header configurations
  • ๐Ÿ”„ Async Support: Handle thousands of concurrent requests
  • ๐Ÿ“Š Memory Efficient: Minimal memory footprint

Benchmarks

# Synchronous: ~100 requests/second
# Asynchronous: ~1000 requests/second (10x faster)

๐Ÿ”’ Security

Security is a top priority. Please review our Security Policy for:

  • ๐Ÿ” Best practices for API key management
  • ๐Ÿ›ก๏ธ Reporting vulnerabilities
  • ๐Ÿ“œ Security features
  • โœ… Security audit checklist

Found a security issue? Please report it privately to develop@eclips-team.ru

๐Ÿ“œ License

Distributed under the MIT License. See LICENSE for more information.

๐ŸŒŸ Star History

Star History Chart

๐Ÿ’– Support

If you find this project helpful, please consider:

  • โญ Starring the repository
  • ๐Ÿ› Reporting bugs
  • ๐Ÿ’ก Suggesting new features
  • ๐Ÿ“ข Sharing with others

๐Ÿ“ž Contact & Support

๐Ÿ”— Links


Made with ๐Ÿ”„ and โค๏ธ by Eclips Team

Made with Python Powered by requests Async with aiohttp

โฌ†๏ธ Back to top

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apikeyrotator-0.4.2.tar.gz (41.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apikeyrotator-0.4.2-py3-none-any.whl (51.1 kB view details)

Uploaded Python 3

File details

Details for the file apikeyrotator-0.4.2.tar.gz.

File metadata

  • Download URL: apikeyrotator-0.4.2.tar.gz
  • Upload date:
  • Size: 41.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for apikeyrotator-0.4.2.tar.gz
Algorithm Hash digest
SHA256 4ff91958d5d1df3c51c04dec84dc142080201b90ed42ef1079cfb669b975709b
MD5 4bb984cb6aeaf47106004a19c3e97bd7
BLAKE2b-256 4837ced7abbb6ab45c86a37a657cc79ab468b07c596a9aae290f7bb6dff79aa7

See more details on using hashes here.

File details

Details for the file apikeyrotator-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: apikeyrotator-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for apikeyrotator-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c196fe43753a1ef69185fae72cc2da23bb8548594c91c6cfb7dad3fc58bff5b
MD5 7d16ad42227a37ba0b1fbe2398199878
BLAKE2b-256 60ccb4a9e2ab15785b12087217c940494356cb7bbf9d59c6cdd1457c0c94c353

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page