Supabase Edge Functions: Server Logic Without a Server
When server functions are needed, what their limits are, how to handle secrets, and how deployment, logs and debugging work.
All articles in the guide Supabase · 7
Functions exist where the direct-database-access model ends: when something cannot be trusted to the client.
When they are needed
Signs that a task requires a function:
Third-party secrets. A payment provider key, a mailing service key, a model key. They cannot live in client code, and in a Supabase project there is nowhere else for them.
Webhooks. An external service notifies you about a payment or event. Server code must receive it, because only server code can verify the signature.
Actions users must not invoke directly. Awarding credits, changing order status, anything where the system decides.
Calls to external APIs followed by writing the result.
Heavy operations you would rather not run in a browser.
When they are not needed: if the task is a database query under correct policies. Wrapping an ordinary select in a function adds a layer to maintain and slows the response. On policies, see authentication and RLS.
Limits
Worth knowing before building an architecture on them:
Execution time is capped. Long operations do not fit: split them or move them to a separate service.
Cold starts. A function that has not run recently answers more slowly the first time. Noticeable for an interactive action.
Runtime restrictions. Not everything that runs on an ordinary server runs here: some libraries and system capabilities are unavailable.
No persistent state. Nothing survives between invocations. State belongs in the database.
Network constraints. Outbound calls are possible with their own rules and timeouts.
The practical conclusion: functions are good for short actions and poor for background processing. A long-running process needs its own service or a queue - see pairing with n8n.
Secrets
The only correct arrangement:
- Secrets in project environment variables, not in function code.
- The function reads them at runtime. They never reach the client.
- Different values per environment. A test payment key and a live one must not be the same.
- The database service key belongs only here. If a function needs access bypassing policies, that is acceptable. In client code such a key never is.
Separately: a function that bypasses policies must check permissions itself. Bypassing the rules removes the protection, and if a user can invoke the function, the check falls to it.
Deployment, logs and debugging
Local development. Functions run locally, and that is the right way to debug logic: the edit cycle is faster than deploying.
Deployment is a separate command. This is where the most common problem appears: environment variables are set separately for the cloud. Locally they come from your settings file, which is exactly why a function that worked locally fails after deployment.
Logs are available in the project interface. Log the input, the result and errors - without that, diagnosis is guesswork.
Return meaningful errors. A function that returns an empty response on failure looks identical to one that succeeded.
The debugging order after a failed deployment:
- Check the project environment variables.
- Read the function logs.
- Check whether the outbound call succeeds from the runtime.
- Check permissions: the function may be hitting a policy rather than its own logic.
The overview is in the Supabase guide.
FAQ
When do I need edge functions in Supabase?
When logic cannot be given to the client: handling third-party secrets, processing payment webhooks, and actions users must not be able to invoke directly. Anything achievable with a database query under correct policies does not need a function.
Can I store external API keys in a function?
Not in the code but in project environment variables. The function reads them at runtime and they never reach the client. This is the only place in a Supabase project where third-party secrets can safely live.
Why does my function work locally and fail after deploying?
Usually because environment variables are not set in the project: locally they come from your settings file, and in the cloud they must be set separately. The second cause is network restrictions in the runtime that block an outbound call.
- Supabase: What It Is and Where to StartGuide
- Authentication and RLS in Supabase: Roles and AccessHow authentication works, what row level security policies are, which policies a typical project needs, and where MVPs most often break.
- Supabase Versus Firebase: Which to Choose for an MVPHow the data models differ, how cost behaves as you grow, how strong the vendor lock is, and how to choose for a specific project.
- Self-Hosted Supabase: Running It YourselfWhy you would run Supabase on your own server, what the stack includes, what must be backed up, how upgrades go, and what you give up compared with the cloud.
Done for you
I will build your MVP on Supabase
Auth, a database with RLS, storage, functions and payments, set up right the first time.
from $1,500 · 1 to 2 weeks