What is limit-api?
Limit API is built for global low latency rate and usage limiting. We constantly update the locations of the limiter nodes to move them closer to where the requests are coming from reducing latency and improving availability.
Consistency
All limiters with less than 1000 requests per second are consistent. Limiters configured to handle more than 1000 requests per second are eventually consistent.
Scale
Limit API can scale up to 5 millions requests per second per limit key. Higher throughput limits are available if required but we recommend implementing caching and batching to reduce costs.
Rate Limiting Quick Start
import { Limiter } from "limit-api";
const limiter = new Limiter(
{
apiKey: "your-api-key",
},
// (TokenBucketLimiterConfig | FixedWindowLimiterConfig)
// See [Limiter Config] page for the different limiter configurations
{
type: "token-bucket",
maxTokens: 100,
refillRate: {
tokens: 10,
seconds: 5,
},
}
);
const RATE_LIMIT_KEY = "my-rate-limit-key"; // eg. user id, ip address, etc.
const { status, data } = await limiter.limit(RATE_LIMIT_KEY);
if (status === "success") {
// The request was allowed
} else if (status === "blocked") {
// The request was rate limited
} else {
// An error occurred such as a network error, invalid API key, etc.
}
Usage Limiting Quick Start
Usage limiting uses the same API as rate limiting but usage limiting specifies the amount of usage to record.
import { Limiter } from "limit-api";
const limiter = new Limiter(
{
apiKey: "your-api-key",
},
// (TokenBucketLimiterConfig | FixedWindowLimiterConfig)
// See [Limiter Config] page for the different limiter configurations
{
type: "token-bucket",
maxTokens: 100,
refillRate: {
tokens: 10,
seconds: 5,
},
}
);
const USAGE_LIMIT_KEY = "my-usage-limit-key"; // eg. user id, ip address, etc.
const USAGE_TO_RECORD = 100; // eg. 100 credits, 1000 bytes, etc.
const { status, data } = await limiter.limit(
USAGE_LIMIT_KEY,
USAGE_TO_RECORD // The only difference from rate limiting
);
if (status === "success") {
// The request was allowed
} else if (status === "blocked") {
// The request was usage limited
} else {
// An error occurred such as a network error, invalid API key, etc.
}