How do I rate-limit an API?
Last checked 31 Aug 2026
Short answer
Add per-user or per-IP request limits inside your Edge Function (or via a Postgres table tracking recent requests), since Lovable doesn't apply rate limiting to your custom endpoints automatically.
Why you need to add this yourself
Lovable Cloud and Supabase apply infrastructure-level protections, but application-level rate limiting for your own Edge Functions and public forms is something you (or Lovable, on request) need to build explicitly.
A simple approach
- Create a small table (e.g.
rate_limits) recordingidentifier(user ID or IP),endpoint, andtimestamp. - In the Edge Function, before doing the real work, count recent rows for that identifier/endpoint within a time window (e.g. last 60 seconds).
- If the count exceeds your threshold, return a
429 Too Many Requestsresponse instead of processing.
A prompt that helps
Add rate limiting to the /submit-contact-form Edge Function: no more than 5 requests per IP per minute, returning a clear error when exceeded.
Where it matters most
- Public forms without login (contact forms, waitlist sign-ups)
- Any Edge Function calling a paid third-party API (email sending, AI calls) — an abusive loop here has a real cost, not just a nuisance
- Authentication endpoints (login/reset-password attempts) to slow brute-force guessing
What usually goes wrong
Rate limiting only by user ID misses anonymous abuse (no account needed to hit a public form), and rate limiting only by IP can over-block shared networks (offices, mobile carriers). Combine both where the endpoint allows it, and log rejected attempts so you can tell if the limit is too strict. See /questions/how-do-i-prevent-database-abuse and /questions/how-do-i-stop-bots for the surrounding strategy.
Sources
Was this answer helpful?
Related questions
- AnswerHow do I protect API keys?Never put API keys in front-end code — store them as secrets used only inside server functions (Edge Functions), where the browser never sees them.
- AnswerAre Lovable websites secure?Lovable includes built-in security scanning and API-key protection, but real security also depends on you setting up row-level security and role checks correctly — it isn't automatic just because you used Lovable.
- AnswerWhere should secrets be stored?In your project's server-side secrets store, used only inside Edge Functions — never in front-end code, client-side environment variables, or committed to your Git repository.
- AnswerHow do I run a security audit on Lovable?Open your project's Security view in Lovable to see automated scan results, then ask Lovable in chat to review RLS policies, role storage, and any exposed secrets in detail.