Call Edge Functions from Database Functions

How do you call Edge Functions from Database Functions via Triggers. This seems like it should be a no-brainer but there's no obvious way to do it in the UI apart from writing complex plpgsql and calling the REST API directly but this feels like a hacky way to do it so I'm assuming I'm missing something? Use case Sending a user a welcome email. When a user is created in the database I want to trigger an edge function that handles my email routing.
3 Replies
garyaustin
garyaustin2mo ago
You can use the Supabase webhooks (but those are just a trigger to a plpgsql function handled for you).
Or you use a plpgsql function with pg_net or http extension. Postgres triggers have no concept of edge functions or http calls natively. The recommend approach though is to use a queue system https://supabase.com/docs/guides/queues so you can retry on errors and not slow down the signup process. You would have a cron task running to process the queue getting entries from the trigger function. Also see this from a user yesterday... https://discord.com/channels/839993398554656828/1397781331391549450
cerwind
cerwindOP2mo ago
Appreciate it, thanks! It does seems quite convoluted to send a simple email on a database insert but I come from Django land so I appreciate a lot of postgres magic happens underneath I take for granted. It would be really nice to have this in-built into the actual trigger as I imagine calling an API on database changes is quite a common flow for most users.
garyaustin
garyaustin2mo ago
Supabase is built on top of unmodified postgres so unless postgres adds an http trigger it won't happen. Supabase does have auth hooks though. They just added one for before user signup and I've seen code indicating they will add after user signup hook also. Those can have an http endpont that gets called.

Did you find this page helpful?