Skip to main content

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

Metadata

Release files for simple-api-key-rotator 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-api-key-rotator 0.2.0
File Size Uploaded
simple_api_key_rotator-0.2.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-api-key-rotator 0.2.0
File Interpreter ABI Platform
simple_api_key_rotator-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / simple_api_key_rotator-0.2.0.tar.gz

Download URL simple_api_key_rotator-0.2.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
819ad1d2d1ebd66caba79f2eb38f0b86b4e74995bd9b7e3f4bbe0ec6a9a0ea34
BLAKE2b-256 checksum
How to use checksums
18ce30bff530f9153ad323ed8708193977d99969be5d95e30aa9e142f8c69ca3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 1, 2026.

Transparency log

Release files / simple_api_key_rotator-0.2.0-py3-none-any.whl

Download URL simple_api_key_rotator-0.2.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4424304c279131c04e712f7c349f68f7d9a168b63a327df047821b45841a13f4
BLAKE2b-256 checksum
How to use checksums
57cc95b8c39ba9c5193d4e4fb16d1e3fcd46d4c0312b33b546e01b6137ee831e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 1, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page