Put a limit on the endpoints that cost you money or let someone guess
A rate limit is a counter: how many times one caller may use an endpoint in a window of time. Without one, a script can call your sign-in a thousand times a minute, send ten thousand password-reset emails to a victim, or run up your AI bill overnight.
Most AI-built apps ship without any limit because the framework does not add one for you. Scanwright reports an endpoint as "no rate limiting observed" when repeated requests keep getting a normal answer.
Which endpoints need a limit first?
Start with the ones where repetition is either dangerous or expensive:
- Sign-in and password reset: limit attempts per account and per address.
- Sign-up and contact forms: they send email and create rows.
- Anything that calls a paid API: AI completions, SMS, geocoding, payments.
- Search and export endpoints: cheap to call, expensive to serve.
Per user, per address, or both?
Both. A per-address limit stops one script; a per-account limit stops a distributed one aimed at a single victim. Keep the windows short (a minute) with a generous count for normal use, and add a slower daily cap for the costly endpoints.
Where to put it
At the edge if your host offers it (Vercel and Cloudflare both have request limits per route), otherwise in middleware in front of the route. For serverless functions use a shared store (Upstash Redis, or your database) so every instance sees the same counter.
Add a limiter in front of the sensitive routes, keyed on the user when signed in and on the address otherwise, and answer 429 Too Many Requests when it trips. Keep the message plain: "Too many attempts, try again in a minute."
- List the routes that send email, call a paid API, or authenticate.
- Add a limiter (edge rule, middleware, or a small helper backed by Redis) with a per-minute window.
- Return HTTP 429 with a Retry-After header; show a friendly message in the app.
- Re-scan: the "no rate limiting observed" findings should disappear.
// middleware.ts (Next.js) with Upstash Ratelimit
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';
import { NextResponse } from 'next/server';
const limiter = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(10, '1 m') });
export async function middleware(req) {
if (!req.nextUrl.pathname.startsWith('/api/auth') && !req.nextUrl.pathname.startsWith('/api/ai')) return NextResponse.next();
const key = req.headers.get('x-forwarded-for') ?? 'anon';
const { success, reset } = await limiter.limit(key);
if (!success) return new NextResponse('Too many attempts, try again in a minute.', { status: 429, headers: { 'Retry-After': String(Math.ceil((reset - Date.now()) / 1000)) } });
return NextResponse.next();
}
export const config = { matcher: ['/api/:path*'] };
FAQ
Will a limit block real users?
Not if the numbers fit real behaviour. Ten sign-in attempts a minute is plenty for a person and far too few for a script. Watch your logs for a week and adjust.
Does Supabase Auth already limit sign-in?
Supabase applies project-wide limits on some auth endpoints, but your own API routes and edge functions have none unless you add them. Check both.