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:
- Set
API_KEY_ROTATOR_ROOTin the environment. - Pass
root=...toget_keyorset_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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
819ad1d2d1ebd66caba79f2eb38f0b86b4e74995bd9b7e3f4bbe0ec6a9a0ea34
|
|
| MD5 |
7efa1c40f2497d66cfa87a9860eaecc5
|
|
| BLAKE2b-256 |
18ce30bff530f9153ad323ed8708193977d99969be5d95e30aa9e142f8c69ca3
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_api_key_rotator-0.2.0.tar.gz -
Subject digest:
819ad1d2d1ebd66caba79f2eb38f0b86b4e74995bd9b7e3f4bbe0ec6a9a0ea34 - Sigstore transparency entry: 1205866440
- Sigstore integration time:
-
Permalink:
evgeniyarbatov/simple-api-key-rotator@d28062ee531bfa053c0841cf54244498715d220a -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/evgeniyarbatov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d28062ee531bfa053c0841cf54244498715d220a -
Trigger Event:
push
-
Statement type:
File details
Details for the file simple_api_key_rotator-0.2.0-py3-none-any.whl.
File metadata
- Download URL: simple_api_key_rotator-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4424304c279131c04e712f7c349f68f7d9a168b63a327df047821b45841a13f4
|
|
| MD5 |
f60fbd9677d7bcaf78773f07ca8c6352
|
|
| BLAKE2b-256 |
57cc95b8c39ba9c5193d4e4fb16d1e3fcd46d4c0312b33b546e01b6137ee831e
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_api_key_rotator-0.2.0-py3-none-any.whl -
Subject digest:
4424304c279131c04e712f7c349f68f7d9a168b63a327df047821b45841a13f4 - Sigstore transparency entry: 1205866465
- Sigstore integration time:
-
Permalink:
evgeniyarbatov/simple-api-key-rotator@d28062ee531bfa053c0841cf54244498715d220a -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/evgeniyarbatov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d28062ee531bfa053c0841cf54244498715d220a -
Trigger Event:
push
-
Statement type: