๐ APIKeyRotator
๐ Quick Start โข ๐ Documentation โข ๐ก Examples โข ๐ Security โข ๐ Changelog
โจ Features
|
๐ Automatic Key Rotation
|
๐ Smart Retry Logic
|
๐ก๏ธ Anti-Bot Evasion
|
|
โก Dual Mode Support
|
๐ง Intelligent Headers
|
๐ฃ๏ธ Multi-Provider Routing
|
๐ 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
### ๐ฃ๏ธ Multi-Provider Fallback Routing
```python
from apikeyrotator import FallbackRouter, ProviderRoute, APIKeyRotator
router = FallbackRouter(routes=[
ProviderRoute(name="Primary", rotator=APIKeyRotator(api_keys=["key1"])),
ProviderRoute(
name="Fallback",
rotator=APIKeyRotator(api_keys=["key2"]),
request_transformer=lambda m, u, k: (m, "https://api.fallback.com/data", k)
)
])
# Automatically falls back to the second API if the first is exhausted!
response = router.get("https://api.primary.com/data")
### ๐ Using Environment Variables
Create a `.env` file:
```bash
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
๐ 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 via the internal issue tracker.
๐ 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
๐ Links
- PyPI: pypi.org/project/apikeyrotator
- GitHub: github.com/PrimeevolutionZ/apikeyrotator
- Documentation: Full Documentation
- Changelog: What's New
Metadata
Release files for apikeyrotator 0.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| apikeyrotator-0.6.1.tar.gz | 55.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| apikeyrotator-0.6.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 109.8 kB
Release files / apikeyrotator-0.6.1.tar.gz
| Download URL | apikeyrotator-0.6.1.tar.gz |
|---|---|
| Size | 55.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f56ad1341cf9a7cb65596cb036663f64d69553b16bb68d8a613309fcbe0ac471
|
|
BLAKE2b-256 checksum How to use checksums |
a7dd598df2000363062732674ce94c7fa432292762ba7cd036432e4a5b1002b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.0
|
Release files / apikeyrotator-0.6.1-py3-none-any.whl
| Download URL | apikeyrotator-0.6.1-py3-none-any.whl |
|---|---|
| Size | 53.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0bcc11bc219cdbd38f945b9d2b14619df504e9c892685cdd89cb593b7e39ec4d
|
|
BLAKE2b-256 checksum How to use checksums |
7b1da4987bf38fe042b6387707a6ca902d06c2dff040e5b283866918cf678117
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.0
|