npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ai-key-rotator

v1.0.0

Published

Rotate API keys across multiple AI providers with automatic rate limit handling, cooldowns, and failover

Downloads

119

Readme

ai-key-rotator

Rotate API keys across multiple AI providers with automatic rate limit handling, cooldowns, and failover.

Install

npm install ai-key-rotator

Quick Start

import { AIKeyRotator } from "ai-key-rotator";

const rotator = new AIKeyRotator({
  providers: {
    openai: {
      credentials: [
        { key: "sk-key1", label: "personal" },
        { key: "sk-key2", label: "work" },
      ],
      cooldown: { type: "fixed", durationMs: 60_000 }, // 1 min cooldown
    },
    google: {
      credentials: [
        { key: "AIza-1", meta: { model: "gemini-flash" } },
        { key: "AIza-2", meta: { model: "gemini-pro" } },
      ],
      // default: resets at midnight UTC
    },
  },
});

// Get an available key
const cred = rotator.getKey("openai");
if (cred) {
  console.log(cred.key); // "sk-key1"
}

// On rate limit error, mark it and auto-rotate
rotator.markExhausted("openai", "sk-key1");

// Next call gets the next available key
const next = rotator.getKey("openai"); // "sk-key2"

API

new AIKeyRotator(config)

| Option | Type | Description | |--------|------|-------------| | providers | Record<string, ProviderConfig> | Map of provider name → credentials + cooldown | | persistence | PersistenceAdapter | Optional save/load adapter for state persistence | | resetIntervalMs | number | Background reset check interval (default: 600000). Set 0 to disable. |

ProviderConfig

{
  credentials: Array<{ key: string; label?: string; meta?: Record<string, unknown> }>;
  cooldown?: { type: "fixed"; durationMs: number } | { type: "midnight-utc" };
}

Methods

| Method | Returns | Description | |--------|---------|-------------| | getKey(provider) | Credential \| null | Get next available credential | | markExhausted(provider, key?) | boolean | Mark a key as exhausted, auto-rotates to next | | rotateNext(provider) | boolean | Manually rotate to next available key | | hasAvailable(provider) | boolean | Check if any keys are available | | getStatus(provider?) | RotatorStatus \| ProviderStatus | Get status overview | | getProviders() | string[] | List all provider names | | addCredential(provider, cred) | void | Add a credential at runtime | | removeCredential(provider, key) | void | Remove a credential | | addProvider(name, config) | void | Add a new provider at runtime | | resetAll(provider?) | void | Manually reset all exhausted credentials | | save() | Promise<void> | Persist state | | load() | Promise<boolean> | Restore state from persistence | | destroy() | void | Stop background timers |

Events

rotator.on("exhausted", (provider, credential) => { /* key was exhausted */ });
rotator.on("rotated", (provider, credential) => { /* switched to new key */ });
rotator.on("allExhausted", (provider) => { /* no keys left */ });
rotator.on("reset", (provider, count) => { /* keys were reset */ });

Persistence

import fs from "fs";

const rotator = new AIKeyRotator({
  providers: { /* ... */ },
  persistence: {
    save: (state) => fs.writeFileSync("state.json", state),
    load: () => {
      try { return fs.readFileSync("state.json", "utf-8"); }
      catch { return null; }
    },
  },
});

// Restore previous state on startup
await rotator.load();

License

MIT