Reduce abuse
Public API endpoint
AI Tools · Const
Rate limiting protects an API by restricting each client's requests within a time window. The documented example uses Upstash Redis and Upstash Ratelimit to allow five requests every 30 seconds and reject excess traffic.
The documented approach combines Upstash Redis with Upstash Ratelimit. Configure a limiter such as five requests per 30 seconds, call ratelimit.limit with a client identifier, and reject unsuccessful checks with a 429 response before handling the request.
const ratelimit = new Ratelimit({ redis, limiter: Ratelimit.fixedWindow(5, '30s') });
const { success, remaining } = await ratelimit.limit(clientKey);| Name | Type | Description |
|---|---|---|
ip | string | Client identifier used as the rate-limit key, represented by the request IP in the example. |
The page's rate-limit call returns success and remaining; the endpoint returns a 429 response when unsuccessful and otherwise continues to produce its normal response.
limit with the client key and read both whether it passed and how many requests remain.import { Redis } from '@upstash/redis';
import { Ratelimit } from '@upstash/ratelimit';
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.fixedWindow(5, '30s'),
});
export async function POST(req: Request) {
const ip = req.headers.get('x-forwarded-for') ?? 'ip';
const { success } = await ratelimit.limit(ip);
if (!success) return new Response('Ratelimited!', { status: 429 });
return new Response('OK');
}Creates a fixed-window limiter backed by Upstash Redis, checks the caller identifier, and returns HTTP 429 when the request exceeds the configured limit.
Public API endpoint
Model-generation route
Streaming response handler
Rate limiting protects an API by restricting how many requests a client may make during a defined period. The page demonstrates a fixed window of five requests per 30 seconds, using an identifier such as an IP address.
Follow-up: How should an endpoint signal that the limit was exceeded? Return an HTTP 429 response and avoid invoking the downstream model or application logic.
Create an Upstash Redis client from environment variables, pass it to Ratelimit, and configure a limiter with Ratelimit.fixedWindow. Then call ratelimit.limit with the client key and inspect success.
Follow-up: What additional result can be used for response metadata? The limit call also returns remaining, which can be used to expose or log the number of requests left.
The request identifier is used as the rate-limit key; the example derives it from the request IP and falls back to 'ip'. If success is false, the handler returns new Response('Ratelimited!', { status: 429 }).
Follow-up: Why is key selection significant? A shared fallback key can rate-limit unrelated callers together, so production code should use a reliable per-client identifier.
Upstash RedisUpstash RatelimitRatelimit.fixedWindowratelimit.limitmaxDurationHTTP 429