Event-driven email architecture in Supabase
Instead of manually calling email endpoints across multiple frontend forms, Supabase Database Webhooks listen for INSERT events on auth.users or public.orders, immediately executing a Supabase Edge Function.
Supabase Edge Function (supabase/functions/send-email/index.ts)
Here is the Edge Function receiving the Postgres webhook payload and dispatching the email.
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
interface WebhookRecord {
type: 'INSERT';
table: 'orders';
record: { id: string; user_email: string; total_amount: number };
}
Deno.serve(async (req) => {
const payload: WebhookRecord = await req.json();
const { user_email, id, total_amount } = payload.record;
const res = await fetch('https://api.sadasend.com/v1/emails', {
method: 'POST',
headers: {
'Authorization': `Bearer ${Deno.env.get('SADASEND_API_KEY')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: user_email,
subject: `Receipt for Order #${id}`,
text: `Thank you for your purchase of ${(total_amount / 100).toFixed(2)}!`,
}),
});
return new Response(JSON.stringify(await res.json()), { headers: { 'Content-Type': 'application/json' } });
});Configuring the Webhook in Supabase Dashboard
- Navigate to Database → Webhooks → Create a new webhook.
- Target table: orders on INSERT events.
- Webhook URL: https://<project-ref>.supabase.co/functions/v1/send-email
Building AI agents that send email?
Join the SadaSend early access waitlist to get scoped API keys, recipient allowlists, and Model Context Protocol (MCP) servers upon launch.