Skip to main content

A library to help prevent rate limits or load balance by using API keys in rotation

Project description

APIRotater

A Python library for rotating API keys to help prevent rate limit issues.

Features

  • Manage multiple API keys
  • Sequential rotation
  • Track API key usage counts
  • Reserve keys for specific number of uses
  • Set time-based rate limits for each key
  • Automatically load API keys from .env file
  • Helper functions and decorators
  • Smart fallback mechanism for continuous operation
  • Direct module-level API access
  • Short syntax aliases for common operations

Installation

pip install apirotater

Usage Examples

Creating a .env File

Create a .env file in your root directory and add your API keys in the following format:

API_KEY_1=your_first_api_key_here
API_KEY_2=your_second_api_key_here
API_KEY_3=your_third_api_key_here

The library will automatically load all API keys defined with the API_KEY_* pattern in your .env file.

Usage

import time
import apirotater #import apirotater

#Get a available API key and set rate limit as 15 requests per 60 seconds
api_key = apirotater.key(time_window=60, max_uses=15)

try:

    #your code here

    apirotater.hit(api_key) #report usage to apirotater

    """
    apirotater will automatically rotate to a new API key 
    until there are no more available API keys 
    according to the rate limit set by apirotater.key().
    When a key is available again apirotater will pass the key to app automatically.
    """

except apirotater.RateLimitExceeded as e: #catch if there are no more available API keys
    print(e)
    time.sleep(10) #or do anything else you want

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.3.0.tar.gz (10.5 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.3.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for apirotater-0.3.0.tar.gz
Algorithm Hash digest
SHA256 79e7194285366a8d0e017c4f70054258a097be070a249e542b2729bdf5df8179
MD5 1a30e38ebb247d8b9581984e73160618
BLAKE2b-256 df35b5b03924eeac7f4fe695380c74fd76c27d388e9cfb5ea3cb7f33225706c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: apirotater-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bae69d590e341e4bd6249cb872c269bdb55759a6a5363df99681e4e82800f9a3
MD5 21eeefb9d4cad8f61ee311b3b82333ca
BLAKE2b-256 73fa74f267132db5c82c2b180aaddbcfccc9c3f200548435e30e28a7bc10fb5b

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