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 check Supabase RLS?Ask Lovable to list every table and its row-level security status and policies in plain language — Lovable Cloud users don't have Supabase dashboard access, so this has to go through chat.
- AnswerHow do I prevent spam registrations?Verify email addresses on sign-up, add a CAPTCHA to the registration form, rate-limit sign-up attempts per IP, and check the token server-side before creating an account.
- AnswerHow do I stop bots?Add a CAPTCHA (e.g. Cloudflare Turnstile) to public forms, validate inputs server-side, and rate-limit submissions — client-side-only checks like a hidden field are easy for real bots to bypass.
- 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.