How do I secure Edge Functions?
Last checked 31 Aug 2026
Short answer
Verify the caller's identity and role inside the function itself, validate all input, use secrets rather than hard-coded keys, and don't assume the front end already checked permissions.
Why Edge Functions need their own checks
An Edge Function is a public HTTP endpoint. Anyone who finds its URL can call it directly, bypassing whatever your front end normally shows or hides. Treat every Edge Function as if it will be called by someone who skipped your UI entirely.
What to check inside the function
- Authentication — verify the caller's JWT/session; reject unauthenticated requests unless the function is genuinely meant to be public.
- Authorisation — if the action is role-gated (e.g. admin-only), check the caller's role against your
user_rolestable inside the function, not just on the client. - Input validation — validate and sanitise every parameter; don't trust that the front end only ever sends well-formed data.
- Secrets — read API keys from the server-side secrets store, never hard-code them in the function file.
- Rate limiting — for functions that are expensive or abusable (email sending, AI calls), add basic throttling per user or IP.
A prompt that helps
Review my Edge Functions and confirm each one checks the caller's authentication and role before doing anything privileged, validates its inputs, and doesn't hard-code any secrets.
What usually goes wrong
A function written to be called from one specific admin page ends up reachable by anyone, because the "it's only called from the admin page" assumption isn't actually enforced inside the function — the URL is public regardless of which page links to it. See /questions/how-do-i-protect-admin-pages and /questions/how-do-i-rate-limit-an-api for the related patterns.
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.