Back to Vercel

@vercel/firewall

packages/firewall/docs/README.md

16.1.25.1 KB
Original Source

@vercel/firewall

Table of contents

References

Functions

References

unstable_checkRateLimit

Renames and re-exports checkRateLimit

Functions

checkRateLimit

checkRateLimit(rateLimitId, options?): Promise<{ error?: "not-found" | "blocked" ; rateLimited: boolean }>

Experimental: Check rate-limits defined through the Vercel Firewall.

This function provides programmatic access to rate limits defined in the Vercel Firewall from Vercel Functions. The given ID is matched against rate limit rules defined with the same ID. The return value indicates whether the request is rate limited or not.

Example

js
import { unstable_checkRateLimit as checkRateLimit } from '@vercel/firewall';

export async function POST() {
  const { rateLimited } = await checkRateLimit('my-rate-limit-id');
  if (rateLimited) {
    return new Response('', {
      status: 429,
    });
  }
  // Implement logic guarded by rate limit
}

Parameters

NameTypeDescription
rateLimitIdstringThe ID of the rate limit to check. The same ID must be defined in the Vercel Firewall as a @vercel/firewall rule condition.
options?Object
options.firewallHostForDevelopment?stringThe host name on which the rate limit rules are defined
options.headers?Headers | Record<string, string> | Record<string, string | string[]>The headers for the current request. Optional.
options.rateLimitKey?stringThe key to use for rate-limiting. If not defined, defaults to the user's IP address.
options.request?RequestThe current request object. Optional.

Returns

Promise<{ error?: "not-found" | "blocked" ; rateLimited: boolean }>

A promise that resolves to an object with a rateLimited property that is true if the request is rate-limited, and false otherwise. The error property is defined if the request was blocked by the firewall or the rate limit ID was not found.

Defined in

rate-limit.ts:29