๐ Powerful and simple API key rotator for bypassing rate limits and handling errors
Project description
๐ APIKeyRotator
๐ Quick Start โข ๐ Documentation โข ๐ก Examples โข ๐ Security โข ๐ Changelog
โจ Features
|
๐ Automatic Key Rotation
|
๐ Smart Retry Logic
|
๐ก๏ธ Anti-Bot Evasion
|
|
โก Dual Mode Support
|
๐ง Intelligent Headers
|
๐ 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 Logicdef 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 Headersdef 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
- 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
๐ 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
๐ Support
If you find this project helpful, please consider:
- โญ Starring the repository
- ๐ Reporting bugs
- ๐ก Suggesting new features
- ๐ข Sharing with others
๐ Contact & Support
- GitHub Issues: Report bugs
- GitHub Discussions: Ask questions
- Email: develop@eclips-team.ru
๐ Links
- PyPI: pypi.org/project/apikeyrotator
- GitHub: github.com/PrimeevolutionZ/apikeyrotator
- Documentation: Full Documentation
- Changelog: What's New
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apikeyrotator-0.4.3.tar.gz.
File metadata
- Download URL: apikeyrotator-0.4.3.tar.gz
- Upload date:
- Size: 59.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ba2a5e2692028d1df9f877c794a802f66cfa687dc581d0673c156eda3e7384a
|
|
| MD5 |
06b9d406bc15896b7cce78a5d0d50979
|
|
| BLAKE2b-256 |
30aa0d141037ac2ea075fa10d291a65ad5e90bd89f6caaa303ca059431433bff
|
File details
Details for the file apikeyrotator-0.4.3-py3-none-any.whl.
File metadata
- Download URL: apikeyrotator-0.4.3-py3-none-any.whl
- Upload date:
- Size: 53.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c8e72a1630772d12eb908e15e9de0be6c9350cc89eb291e4f7336f4ab6bde48
|
|
| MD5 |
e80553c52a8fc8a08dd5ef85a5251283
|
|
| BLAKE2b-256 |
917deb90006827b38a19151a39d895d0eedfc326a12fd5cd059542b34e6bc79e
|