0.5.3 • Published 6 months ago
@unkey/ratelimit v0.5.3
Installation
npm install @unkey/ratelimit
Quickstart
- Configure your ratelimiter:
import { Ratelimit } from "@unkey/ratelimit";
const unkey = new Ratelimit({
rootKey: process.env.UNKEY_ROOT_KEY,
namespace: "my-app",
limit: 10,
duration: "30s",
async: true,
});
- Use it:
async function handler(request) {
const identifier = request.getUserId(); // or IP or anything else you want
const ratelimit = await unkey.limit(identifier);
if (!ratelimit.success) {
return new Response("try again later", { status: 429 });
}
// handle the request here
}
Making it Bullet Proof
To ensure reliability, you can configure timeout and error handling:
import { Ratelimit } from "@unkey/ratelimit";
const fallback = (identifier: string) => ({
success: true,
limit: 0,
reset: 0,
remaining: 0,
});
const unkey = new Ratelimit({
// ... standard configuration
timeout: {
ms: 3000, // only wait 3s at most before returning the fallback
fallback,
},
onError: (err, identifier) => {
console.error(`${identifier} - ${err.message}`);
return fallback(identifier);
},
});
API Overview
Create a new instance for ratelimiting by providing the necessary configuration.
new Ratelimit(config: RatelimitConfig)
Check whether a specific identifier is currently allowed to do something or if they have currently exceeded their limit.
.limit(identifier: string, opts: LimitOptions): Promise<RatelimitResponse>
Documentation
0.5.3
6 months ago
0.5.2
6 months ago
0.5.1
6 months ago
0.5.0
7 months ago
0.4.7
7 months ago
0.4.6
8 months ago
0.4.5
10 months ago
0.4.4
11 months ago
0.4.3
11 months ago
0.4.2
11 months ago
0.4.1
11 months ago
0.4.0
12 months ago
0.3.0
1 year ago
0.2.0
1 year ago
0.3.2
12 months ago
0.3.1
1 year ago
0.1.10
1 year ago
0.1.11
1 year ago
0.1.12
1 year ago
0.1.8
1 year ago
0.1.9
1 year ago
0.1.7
1 year ago
0.1.6
1 year ago
0.1.5
1 year ago
0.1.4
1 year ago
0.1.3
1 year ago
0.1.2
1 year ago
0.1.0
1 year ago
0.1.1
1 year ago
0.0.3
1 year ago
0.0.2
1 year ago
0.0.1
1 year ago