https://github.com/agm-dev/keyro
Key rotation utility
https://github.com/agm-dev/keyro
Last synced: 7 months ago
JSON representation
Key rotation utility
- Host: GitHub
- URL: https://github.com/agm-dev/keyro
- Owner: agm-dev
- License: mit
- Created: 2019-11-09T11:53:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T00:44:18.000Z (about 3 years ago)
- Last Synced: 2025-07-19T21:21:42.195Z (8 months ago)
- Language: JavaScript
- Size: 1.21 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# keyro - key rotation utility
[](https://travis-ci.org/agm-dev/keyro)
[](https://www.codacy.com/manual/agm-dev/keyro?utm_source=github.com&utm_medium=referral&utm_content=agm-dev/keyro&utm_campaign=Badge_Grade)
[](https://www.codacy.com/app/codacy/node-codacy-coverage)



## What is this
Keyro is just a simple utility to store an array of values and return the 'next one' on every 'get' access.
I have done it to mitigate the rate limit overload on some services, so a different key is used on every request, but as it is something very generic I guess it can be useful on other situations.
## Requirements
This is a npm package, and you will need a node version which supports ES6. Anyway, the code is really simple, and could be reduced to drop node dependencies so it can be used also on browsers:
```javascript
class Keyro {
constructor({ pool = [] } = {}) {
if (!Array.isArray(pool)) {
throw new Error("pool has to be an array");
}
this.pointer = 0;
this.pool = pool;
}
add(key) {
this.pool = [...this.pool, key];
}
get() {
const value = this.pool[this.pointer];
const hasReachTheEnd = this.pointer >= this.pool.length - 1;
const nextPointerValue = hasReachTheEnd ? 0 : this.pointer + 1;
this.pointer = nextPointerValue;
return value;
}
}
```
## Installation
```bash
npm i --save keyro
```
## Usage
```javascript
const Keyro = require("keyro");
const keysFromConfig = [
"SDF9DF897SFGD98A7SDF",
"VB8N8SC7G68DF8B76S4D",
"OOLI4L2L1U5I3HJU5K15"
];
const keyro = new Keyro({ pool: keysFromConfig });
function getApiKey() {
return keyro.get();
}
console.log(getApiKey()); // "SDF9DF897SFGD98A7SDF"
console.log(getApiKey()); // "VB8N8SC7G68DF8B76S4D"
console.log(getApiKey()); // "OOLI4L2L1U5I3HJU5K15"
console.log(getApiKey()); // "SDF9DF897SFGD98A7SDF"
console.log(getApiKey()); // "VB8N8SC7G68DF8B76S4D"
console.log(getApiKey()); // "OOLI4L2L1U5I3HJU5K15"
```