Client SDK

Limiter Client

import { Limiter } from "limit-api";
 
const limiter = new Limiter(
  {
    // Your api key from the limit api dashboard (https://limitapi.com/dashboard)
    apiKey: "your-api-key",
 
    // This tag will prefix the rate limit keys so multiple projects can use the same api key without conflicts.
    // For example, if the tag is "my-project" and the rate limit key is "my-rate-limit-key", the rate limit key will be "my-project:my-rate-limit-key".
    // The limit api dashboard will show the number of requests per hour for each tag.
    // There is a limit of 5000 unique tags per account. If you exceed this limit analytics for some tags will be randomly dropped. Everything else will work as normal.
    tag: "project-1",
  },
 
  // See [Limiter Config] page for the different limiter configurations
  config: {
    type: "token-bucket",
    maxTokens: 100,
    refillRate: {
      tokens: 10,
      seconds: 5,
    },
  }
);

Limiter Client Usage

const { status, data } = await limiter.limit(
  key: string,
 
  // The value to use for the usage limit.
  // If the value is not provided the default value is 1.
  value?: number
);

Rate Limiting Example

const RATE_LIMIT_KEY = "my-rate-limit-key";
 
const { status, data } = await limiter.limit(RATE_LIMIT_KEY);

Usage Limiting Example

const USAGE_LIMIT_KEY = "my-usage-limit-key";
const USAGE_LIMIT_VALUE = 7;
 
const { status, data } = await limiter.limit(
  USAGE_LIMIT_KEY,
  USAGE_LIMIT_VALUE
);

Response handling

const { status, data } = await limiter.limit(RATE_LIMIT_KEY);
 
if (status === "success") {
  // The request was allowed
} else if (status === "blocked") {
  // The request was usage or rate limited
} else {
  // An error occurred such as a network error, invalid API key, etc.
}