5 min read

Rate-limiting paid-API routes — Upstash, Cloudflare, edge-native

Every route calling OpenAI / Stripe / Anthropic / paid vendor needs per-IP + per-user rate limits. Edge-native is best for vibe-coded apps.

Paid-API routes are the highest-cost-per-attack surface. Rate-limit at edge to stop the attack before it reaches inference.

What it is

Rate limiting bounds requests per identity per time window. Edge-native (Cloudflare / Vercel Edge / Upstash) puts the limit before the expensive inference call.

Vulnerable example

// /api/chat/route.ts — no rate limit
export async function POST(req: Request) { /* expensive openai call */ }

Fixed example

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(20, "1 m"), // 20/min per IP
});
export async function POST(req: Request) {
  const ip = req.headers.get("x-forwarded-for") ?? "anon";
  const { success } = await ratelimit.limit(ip);
  if (!success) return new Response("Too many requests", { status: 429 });
  // continue to expensive call
}

How Securie catches it

Securie findingmedium
apps/web/app/api/route.ts:22

Rate-limiting paid-API routes

Static-rules + AuthAuthz specialist catch paid-API routes without rate limits at PR time.

Suggested fix — ready as a PR
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(20, "1 m"), // 20/min per IP
});
export async function POST(req: Request) {
  const ip = req.headers.get("x-forwarded-for") ?? "anon";
  const { success } = await ratelimit.limit(ip);
  if (!success) return new Response("Too many requests", { status: 429 });
  // continue to expensive call
}
Catch this in my repo →Securie scans every PR · ships the fix as a one-click merge · free during early access

Checklist

  • Per-IP rate limit at edge
  • Per-user rate limit (more restrictive)
  • Per-tenant spend cap (cost-firewall)
  • Vendor-side cap as backstop
  • Monitoring alerts

FAQ

Cloudflare Workers vs Upstash?

Both work. Cloudflare for full edge; Upstash for portability across runtimes.

Related guides