JavaScript MIT

axios-rate-limit

Zero-dependency, fixed window, queued rate limit for axios

A

aishek

Dernière activité 21 juin 2026
aishek/axios-rate-limit

248

étoiles

37

forks

5

issues ouvertes

axiosjsrate-limitrate-limitingrate-limitsratelimitratelimiterratelimitingtypescript

Ce README est souvent en anglais.

axios-rate-limit

npm version npm downloads build status code coverage install size

Zero dependencies, fixed-window, queued rate limiter for Axios: set how many requests per interval should perform immediately, other will be delayed automatically.

Installing

npm install axios-rate-limit

Usage

import axios from 'axios';
import rateLimit from 'axios-rate-limit';

const http = rateLimit(axios.create(), {
  limits: [
    { maxRequests: 5, duration: '2s' },
    { maxRequests: 2, duration: '500ms' }
  ]
})
http.get('https://example.com/api/v1/users.json?page=1')
http.getQueue()

Options

limits is the recommended format. It accepts an array of independent fixed windows, and a request is executed only when all windows allow it.

const http = rateLimit(axios.create(), {
  limits: [
    { maxRequests: 100, duration: '1m' },
    { maxRequests: 10, duration: '1s' }
  ]
})

Each limits[] entry:

  • maxRequests (number, required, > 0): max requests per window.
  • duration (string or number, required, > 0): window size. If a number is provided, it is interpreted as milliseconds (duration: 1500 means 1.5 seconds). Strings support ms, s, m, h (examples: '500ms', '2s', '1m').

Advanced options

  • queue (optional): custom queue implementation. Must support push(item) and shift(), and either length or getLength(). Sync and async queues are supported.
  • shouldCountRequest (optional): predicate (config, response) => boolean. If it returns false, the limiter refunds one occupied slot (useful for cached responses).
  • rateLimiter (optional): lets you pass an existing limiter instance, so multiple axios clients can share one quota.

Runtime API

Returned axios instance methods:

  • getQueue(): returns current queue instance.
  • getMaxRPS(): returns first window RPS view (maxRequests / (durationInMs / 1000)), or 0 when limiter is not configured.
  • setRateLimitOptions(options): updates limiter options at runtime.
  • setMaxRPS(rps): shorthand runtime update equivalent to setRateLimitOptions({ maxRequests: rps, perMilliseconds: 1000 }).

Also available from module:

  • rateLimit.getLimiter(options): creates a limiter instance that can be shared across axios clients.

Legacy single-window options

Single-window constructor shape (maxRequests with one of perMilliseconds, duration, or maxRPS) is still supported for compatibility. For new code, prefer limits[] format.

Typical use cases

Alternatives

Consider using Axios built-in rate-limiting functionality.

Projets similaires

Rate limiting library for serverless runtimes

TypeScriptrate-limitingredisserverless
Uupstash
2 k étoiles52

A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled

TypeScript
Jjhurliman
1,6 k étoiles133

Simple async rate limiter for Node.js — promise-friendly limits for APIs and workers.

JavaScriptapiasyncconcurrency
Mmicrolinkhq
335 étoiles22