π Quick Start β’ π Documentation β’ π‘ Examples β’ π Security β’ π Changelog
|
π Automatic Key Rotation
|
π Smart Retry Logic
|
π‘οΈ Anti-Bot Evasion
|
|
β‘ Dual Mode Support
|
π§ Intelligent Headers
|
pip install apikeyrotatorfrom 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 intelligentlyCreate a .env file:
API_KEYS=key1,key2,key3Then use without explicit keys:
from apikeyrotator import APIKeyRotator
# Automatically loads from .env
rotator = APIKeyRotator()
response = rotator.get("https://api.example.com/data")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")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())ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β 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 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| 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 |
π·οΈ 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)| 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 β
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 β
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
) |
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 β
| 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 |
# 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 -vContributions are what make the open-source community amazing! We welcome:
- π Bug reports
- π‘ Feature suggestions
- π Documentation improvements
- π§ Code contributions
π Read Contributing Guidelines β
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
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
# Synchronous: ~100 requests/second
# Asynchronous: ~1000 requests/second (10x faster)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
Distributed under the MIT License. See LICENSE for more information.
If you find this project helpful, please consider:
- β Starring the repository
- π Reporting bugs
- π‘ Suggesting new features
- π’ Sharing with others
- GitHub Issues: Report bugs
- GitHub Discussions: Ask questions
- Email: develop@eclips-team.ru
- PyPI: pypi.org/project/apikeyrotator
- GitHub: github.com/PrimeevolutionZ/apikeyrotator
- Documentation: Full Documentation
- Changelog: What's New