Skip to main content

Rotate API keys to bypass rate limits.

Project description

Simple API Key Rotator

Rotate API keys using a cooldown rule (default 24 hours). Keys are stored in a text file and usage is tracked in a JSON file per service.

Installation

pip install simple-api-key-rotator

Quickstart

from simple_api_key_rotator import get_key, set_key

current = get_key("my-service")
next_key = set_key("my-service")

Directory layout

Create a directory per service with a keys.txt file and (optionally) a usage.json file:

./my-service/
  keys.txt
  usage.json

keys.txt example:

# Comments and blank lines are ignored
key-1
key-2
key-3

Root directory selection

You can set the base directory in two ways:

  1. Set API_KEY_ROTATOR_ROOT in the environment.
  2. Pass root=... to get_key or set_key.
from pathlib import Path
from simple_api_key_rotator import get_key

key = get_key("my-service", root=Path("/srv/api-keys"))

If both are provided, the explicit root= argument wins.

Rotation rules

A key is eligible when:

  • It has never been used, or
  • It was last used more than 24 hours ago (by default).

When rotating, the algorithm starts from the key after the most recently used key and wraps around in order.

You can override the cooldown window:

from simple_api_key_rotator import set_key

next_key = set_key("my-service", cooldown_hours=6)

Error cases

get_key and set_key raise RuntimeError when no key is eligible or keys.txt is empty.

Example: rotate on API exceptions

Rotate the key when an API returns a rate-limit response, then retry or exit.

import requests

from simple_api_key_rotator import get_key, set_key


def call_api():
    api_key = get_key("my-service")
    response = requests.get(
        "https://api.example.com/endpoint",
        params={"q": "example", "key": api_key},
    )

    if response.status_code == 429:
        set_key("my-service")
        raise RuntimeError("Rate limited. Rotated key; retry later.")

    response.raise_for_status()
    return response.json()

Documentation

  • docs/README.md

Development

make test

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

simple_api_key_rotator-0.2.0.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.

simple_api_key_rotator-0.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file simple_api_key_rotator-0.2.0.tar.gz.

File metadata

  • Download URL: simple_api_key_rotator-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simple_api_key_rotator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 819ad1d2d1ebd66caba79f2eb38f0b86b4e74995bd9b7e3f4bbe0ec6a9a0ea34
MD5 7efa1c40f2497d66cfa87a9860eaecc5
BLAKE2b-256 18ce30bff530f9153ad323ed8708193977d99969be5d95e30aa9e142f8c69ca3

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_api_key_rotator-0.2.0.tar.gz:

Publisher: publish.yml on evgeniyarbatov/simple-api-key-rotator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simple_api_key_rotator-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_api_key_rotator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4424304c279131c04e712f7c349f68f7d9a168b63a327df047821b45841a13f4
MD5 f60fbd9677d7bcaf78773f07ca8c6352
BLAKE2b-256 57cc95b8c39ba9c5193d4e4fb16d1e3fcd46d4c0312b33b546e01b6137ee831e

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_api_key_rotator-0.2.0-py3-none-any.whl:

Publisher: publish.yml on evgeniyarbatov/simple-api-key-rotator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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