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

betterkeys

v1.0.0

Published

High availability key manager for multi-key deployments in Node.js environments.

Readme

better-key

High availability key manager for multi-key deployments in Node.js environments.

better-key is a lightweight TypeScript library that manages multiple API keys with intelligent rotation strategies to improve reliability and fault tolerance in production systems.

It is designed for applications that operate with multiple API keys and need automatic key balancing, health tracking, and failure handling.


✨ Features

  • 🔄 Multiple key rotation strategies
  • 📊 Usage tracking per key
  • 🚦 Automatic unhealthy key detection
  • 🧠 Least-used load balancing
  • 🎲 Random selection strategy
  • 🛡 Designed for high-availability systems
  • ⚡ Zero external dependencies
  • 🧩 Fully typed with TypeScript

📦 Installation

npm install better-key

or

yarn add better-key

🚀 Quick Start

1️⃣ Add your API keys to .env

GEMINI_KEYS=key1,key2,key3

2️⃣ Initialize the manager

import { KeyManager } from "better-key";

const manager = new KeyManager({
  provider: "gemini",
  keys: process.env.GEMINI_KEYS!.split(","),
  strategy: "least-used", // optional
});

3️⃣ Use a key

const apiKey = manager.getKey();

4️⃣ Report errors (optional but recommended)

manager.reportError(apiKey);

If a key exceeds the internal error threshold, it will automatically be marked as unhealthy and excluded from rotation.


🧠 Available Strategies

| Strategy | Description | | ------------- | --------------------------------- | | round-robin | Cycles through keys sequentially | | least-used | Selects the key with lowest usage | | random | Selects a random healthy key |

Example:

const manager = new KeyManager({
  provider: "openai",
  keys: ["key1", "key2"],
  strategy: "round-robin",
});

🏗 How It Works

Each key maintains internal metadata:

  • usageCount
  • errorCount
  • lastUsed
  • healthy status

When:

  • getKey() is called → key is selected based on strategy
  • reportError(key) is called → error counter increases
  • Error threshold exceeded → key marked unhealthy

Unhealthy keys are automatically excluded from future selections.


🛠 Example with Gemini

Example usage with the Gemini API from Google DeepMind:

async function callModel(prompt: string) {
  const key = manager.getKey();

  try {
    // Make request using selected key
  } catch (error: any) {
    if (error.status === 429) {
      manager.reportError(key);
    }
  }
}

🌍 Supported Environments

Works in:

  • Next.js
  • Express.js
  • Hono
  • Node.js server applications
  • Serverless functions
  • Edge runtimes (if no Node-only APIs are used)

⚠️ Important Notice

better-key is designed for legitimate multi-key deployments and high availability setups.

It does not bypass provider rate limits or quotas. Always ensure your usage complies with your API provider's Terms of Service.


🔮 Roadmap

  • [ ] Redis adapter for distributed systems
  • [ ] Cooldown mechanism for unhealthy keys
  • [ ] Event system (onKeyDisabled, onKeyRecovered)
  • [ ] Metrics export
  • [ ] Custom error thresholds
  • [ ] Concurrency-safe locking

🧪 Testing

npm test

📄 License

MIT


🤝 Contributing

Contributions are welcome. Please open an issue or submit a pull request.


👨‍💻 Author

Built for developers building reliable AI infrastructure.