# @sapphire/ratelimits

> Bucket implementation for Ratelimits.

Latest version **2.4.11** (published 2024-11-09) · MIT license · 4.4K weekly downloads

## Install

```sh
npm install @sapphire/ratelimits
pnpm add @sapphire/ratelimits
yarn add @sapphire/ratelimits
bun add @sapphire/ratelimits
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.4.11 |
| Published | 2024-11-09 |
| First published | 2020-10-11 |
| Weekly downloads | 4.4K |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=v14.0.0 |
| Dependencies | 0 |
| Unpacked size | 65.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 190 |
| Author | @sapphire |
| Maintainers | favna, vladfrangu, kyranet |
| Keywords | @sapphire/ratelimits, bot, typescript, ts, yarn, discord, sapphire, standalone |

## Links

- npm: https://www.npmjs.com/package/@sapphire/ratelimits
- Repository: https://github.com/sapphiredev/utilities
- Homepage: https://github.com/sapphiredev/utilities/tree/main/packages/ratelimits
- Issues: https://github.com/sapphiredev/utilities/issues
- npm.io page: https://npm.io/package/@sapphire/ratelimits

## Alternatives

- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads
- [@1selfworld/adchain-sdk-react-native](https://npm.io/package/@1selfworld/adchain-sdk-react-native.md) — 80 weekly downloads
- [@bem/sdk.deps](https://npm.io/package/@bem/sdk.deps.md) — 77 weekly downloads

## Recent versions

- 2.4.11 (latest) — 2024-11-09
- 2.4.12-next.c1299bac (next) — 2026-03-08
- 3.0.0-pr-935.7da5c8bb (pr-935) — 2025-11-25
- 2.4.10-pr-725.55b1ff7b.0 (pr-725) — 2024-03-17
- 2.4.9-pr-684.9d7e2fce.0 (pr-684) — 2023-12-14
- 3.0.0-pr-601.2f2c308a.0 (pr-601) — 2023-05-28
- 3.0.0-pr-589.aa473f9.0 (pr-589) — 2023-05-04
- 3.0.0-pr-587.e9607661.0 (pr-587) — 2023-05-04
- 3.0.0-pr-584.b444d5b.0 (pr-584) — 2023-05-02
- 2.4.7-pr-575.91d23d87.0 (pr-575) — 2023-04-16
- 2.4.7-pr-567.8638d898.0 (pr-567) — 2023-04-06
- 2.4.6-pr-434.77ef153.0 (pr-434) — 2023-01-06
- 2.2.0-pr-270.b2c4518.0 (pr-270) — 2022-01-26
- 3.0.0-pr-160.676f855.0 (pr-160) — 2021-08-23
- 2.1.0-pr-159.fae7a606.0 (pr-159) — 2021-08-23
- … 1013 more at https://npm.io/package/@sapphire/ratelimits/versions

## README

<div align="center">

![Sapphire Logo](https://raw.githubusercontent.com/sapphiredev/assets/main/banners/SapphireCommunity.png)

# @sapphire/ratelimits

**Bucket implementation for Ratelimits.**

[![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
[![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/ratelimits?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/ratelimits)
[![npm](https://img.shields.io/npm/v/@sapphire/ratelimits?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/ratelimits)

</div>

**Table of Contents**

-   [Description](#description)
-   [Features](#features)
-   [Installation](#installation)
-   [Usage](#usage)
    -   [Token RateLimitManager](#token-ratelimitmanager)
    -   [Leaky RateLimitManager](#leaky-ratelimitmanager)
-   [Buy us some doughnuts](#buy-us-some-doughnuts)
-   [Contributors](#contributors)

## Description

There is often a need to apply ratelimits to protect a network from excessive traffic levels on connections routed through it, or limit bot command usages in your Discord bot, or similar things. This package offers two different techniques in the same implementation: the [Token Bucket](https://en.wikipedia.org/wiki/Token_bucket), and the [Leaky Bucket](https://en.wikipedia.org/wiki/Leaky_bucket).

## Features

-   Written in TypeScript
-   Bundled with esbuild so it can be used in NodeJS and browsers
-   Offers CommonJS, ESM and UMD bundles
-   Fully tested

## Installation

You can use the following command to install this package, or replace `npm install` with your package manager of choice.

```sh
npm install @sapphire/ratelimits
```

## Usage

**Note:** While this section uses `require`, the imports match 1:1 with ESM imports. For example `const { RateLimitManager } = require('@sapphire/ratelimits')` equals `import { RateLimitManager } from '@sapphire/ratelimits'`.

### Token RateLimitManager

```typescript
// Import the Bucket class
const { RateLimitManager } = require('@sapphire/ratelimits');

// Define a bucket with 1 usage every 5 seconds
const rateLimitManager = new RateLimitManager(5000);

// Acquire the rate limit. The ID can be something like `'global'`, a Discord channel/server/user ID, or anything else.
const ratelimit = rateLimitManager.acquire('some-unique-id-here');

// Check if there is a rate limit right now
if (ratelimit.limited) {
	// Do something when limited, such as throwing an error
}

// We're not rate limited so we drip the bucket. After consuming once, the second run through we'll be rate limited.
ratelimit.consume();

// And now we can finish the flow by returning some form of "success" state.
```

### Leaky RateLimitManager

```typescript
// Import the Bucket class
const { RateLimitManager } = require('@sapphire/ratelimits');

// Define a bucket with 2 usages every 5 seconds
const rateLimitManager = new RateLimitManager(5000, 2);

// Acquire the rate limit. The ID can be something like `'global'`, a Discord channel/server/user ID, or anything else.
const ratelimit = rateLimitManager.acquire('some-unique-id-here');

// Check if there is a rate limit right now
if (ratelimit.limited) {
	// Do something when limited, such as throwing an error
}

// We're not rate limited so we drip the bucket. After consuming twice, the third run through we'll be rate limited.
ratelimit.consume();

// And now we can finish the flow by returning some form of "success" state.
```

---

## Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

|   Donate With   |                       Address                       |
| :-------------: | :-------------------------------------------------: |
| Open Collective | [Click Here](https://sapphirejs.dev/opencollective) |
|      Ko-fi      |      [Click Here](https://sapphirejs.dev/kofi)      |
|     Patreon     |    [Click Here](https://sapphirejs.dev/patreon)     |
|     PayPal      |     [Click Here](https://sapphirejs.dev/paypal)     |

## Contributors

Please make sure to read the [Contributing Guide][contributing] before making a pull request.

Thank you to all the people who already contributed to Sapphire!

<a href="https://github.com/sapphiredev/utilities/graphs/contributors">
  <img src="https://contrib.rocks/image?repo=sapphiredev/utilities" />
</a>

[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md

---
_Source: https://npm.io/package/@sapphire/ratelimits · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
