Skip to main content

Python library for API key rotation, rate limit control and load balancing

Project description

APIRotater

APIRotater is a Python library that helps you manage your API keys and control rate limits. By managing multiple API keys, it automatically performs key rotation in case of rate limit exceedance.

Key Features

  • Automatic API Key Rotation: Uses multiple API keys in rotation.
  • Rate Limit Control: You can set a maximum number of uses for each API key within a specific time window.
  • Automatic .env File Loading: Automatically loads API keys from .env files located in the current working directory or parent directory.
  • Usage Statistics: Tracks usage counts of API keys.
  • RateLimitExceeded Exception: Throws an error when all keys have exceeded their rate limit.

Installation

pip install apirotater

Usage

Define your API keys in a .env file as follows:

API_KEY_1=your_api_key_1
API_KEY_2=your_api_key_2
API_KEY_3=your_api_key_3

Basic Usage

import apirotater

# Get an API key
api_key = apirotater.key()

# Make API request
# ...

# Report API key usage
apirotater.hit(api_key)

Usage With Rate Limit

import apirotater

# Get a key with maximum 2 uses in 60 seconds
api_key = apirotater.key(time_window=60, max_uses=2)

# Make API request
# ...

# Report API key usage
apirotater.hit(api_key)

Handling Rate Limit Exceedance

import apirotater
import time

try:
    # Get a key with rate limit
    api_key = apirotater.key(time_window=60, max_uses=2)
    
    # Make API request
    # ...
    
    # Report API key usage
    apirotater.hit(api_key)
    
except apirotater.RateLimitExceeded as e:
    print(f"Rate limit exceeded: {e}")
    # Wait for a while and try again
    time.sleep(60)

API

  • key(time_window=60, max_uses=100): Gets an API key (with rate limit)
  • hit(api_key): Reports API key usage
  • usage(): Returns usage statistics for all keys
  • get_all_keys(): Lists all loaded API keys

License

MIT

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

apirotater-0.4.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

apirotater-0.4.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file apirotater-0.4.1.tar.gz.

File metadata

  • Download URL: apirotater-0.4.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for apirotater-0.4.1.tar.gz
Algorithm Hash digest
SHA256 210b0c978459ae8513c5a94772805923bfe36eee92897cc1d7462d8371d2614b
MD5 034b8514dfe0894c7504eba94c9fc33b
BLAKE2b-256 7f0349dc8ac7589f14629d2868aa9a7042e9b3266e925215610436bed34e00ca

See more details on using hashes here.

File details

Details for the file apirotater-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: apirotater-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for apirotater-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a87bb720eb629d0686d0df22f9d9969d93a6b0a9d9d491f4d6214ac25744b61
MD5 ac0494f467f7bdea0185f58de5ee9311
BLAKE2b-256 0114bb01fdeb5c4605cbe6d32ef868a01feaa6f105ff24af5458b274b4f918fe

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