Circumvent API rate limits by having several API tokens and let the dealer manage and give them to you
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status Greenkeeper badge

Circumvent API rate limits by having several API tokens and let the dealer manage and give them to you.

Several public APIs, such as GitHub and Twitter, have rate limits applied per account. To multiply these rate limits, you must have a farm of tokens associated to multiple accounts, either donated or created by you. This is where token-dealer comes in, making it easy to manage these tokens and their usage.


$ npm install token-dealer


tokenDealer(tokens, fn, [options])

Calls fn(token, exhaust) with the most appropriate token from tokens and a exhaust function that you may call to signal that the token is exhausted.

Basically the only thing you must do is call exhaust(reset, [retry]) whenever you know that the token may not be used again until reset (timestamp in ms). Additionally, you may retry if the operation you were trying to do failed because the token was exhausted, causing fn to be called again with another token.

Here's an example from a request to the GitHub API using got:

const tokenDealer = require('tokenDealer');
const got = require('got');

const tokens = [

tokenDealer(tokens, (token, exhaust) => {
    const handleRateLimit = (response, err) => {
        if (response.headers['x-ratelimit-remaining'] === '0') {
            const isRateLimitError = err && err.statusCode === 403 && /rate limit/i.test(response.body.message);

            exhaust(Number(response.headers['x-ratelimit-reset']) * 1000, isRateLimitError);

    return got('', {
        json: true,
        headers: { Authorization: `token ${token}` },
    .then((response) => {
        return response;
    }, (err) => {
        err.response && handleRateLimit(err.response, err);
        throw err;
.then((response) => {
    // ...
}, (err) => {
    // If all tokens are exhausted, err.code will be 'EALLTOKENSEXHAUSTED'

Available options:

  • group: The group associated to the tokens; this effectively groups tokens to prevent conflicts (e.g. github, defaults to default).
  • wait: True to wait for a token to be free in case all are exhausted (defaults to false); alternatively you may pass a function that will be called with [token, duration] so that you can decide to wait dynamically.
  • lru: A custom LRU cache instance to be used internally.
  • onExhausted: Called with (token, reset) whenever a token become exhausted (defaults to null).

If tokens is nullish or an empty array, the given token will be an empty string.

tokenDealer.getTokensUsage(tokens, [options])

Get the tokens usage for tokens. The available options are group and lru which are the same as tokenDealer().

const tokenDealer = require('tokenDealer');

const tokens = [

const usage = tokenDealer.getTokensUsage(tokens);

// `usage` looks like this:
// {
//     'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': {
//         exhausted: true,       // true if exhausted, false otherwise
//         reset: 1455996883369,  // the timestamp in which the token will become available again
//         inflight: 0,           // the number of deliveries (`fn` calls) that still haven't completed
//     },
//     'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb': {
//         // ...
//     }
// }


$ npm test
$ npm test-cov to get coverage report


Released under the MIT License.