Zero dependencies, fixed-window, queued rate limiter for Axios: set how many requests per interval should perform immediately, other will be delayed automatically.
npm install axios-rate-limitimport 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()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: 1500means 1.5 seconds). Strings supportms,s,m,h(examples:'500ms','2s','1m').
queue(optional): custom queue implementation. Must supportpush(item)andshift(), and eitherlengthorgetLength(). Sync and async queues are supported.shouldCountRequest(optional): predicate(config, response) => boolean. If it returnsfalse, 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.
Returned axios instance methods:
getQueue(): returns current queue instance.getMaxRPS(): returns first window RPS view (maxRequests / (durationInMs / 1000)), or0when limiter is not configured.setRateLimitOptions(options): updates limiter options at runtime.setMaxRPS(rps): shorthand runtime update equivalent tosetRateLimitOptions({ maxRequests: rps, perMilliseconds: 1000 }).
Also available from module:
rateLimit.getLimiter(options): creates a limiter instance that can be shared across axios clients.
Single-window constructor shape (maxRequests with one of perMilliseconds, duration, or maxRPS) is still supported for compatibility. For new code, prefer limits[] format.
- Single rate limit — API enforces one limit; use one window via
limits. - Multiple rate limits — API enforces several limits (e.g. per second and per minute); use multiple windows.
- Custom queue — Pass your own queue (e.g. to log when requests are added or removed).
- Retrying failed requests — Use with axios-retry to rate-limit and retry failed requests (see issue #24).
- Integration with axios-cache-adapter — Don't count cached responses toward the limit (see issue #43).
- Change RPS on the fly — Update
setMaxRPS/setRateLimitOptionsat runtime and speed up queued cancellation handling (see issue #48). - Mocking in Jest — How to mock axios-rate-limit in Jest so tests do not hit the network (see issue #51).
- Shared limiter — Reuse one limiter instance across multiple axios clients that share the same API quota.
Consider using Axios built-in rate-limiting functionality.