# Supabase Edge Functions & Email Webhooks: Realtime Database Triggers

_By Tayyab Mughal, Founder & AI Chief · 7 August 2026 · 2 min read_

> How to automatically trigger transactional emails on Supabase database insert events using Supabase Edge Functions and Webhooks.

Trigger instant welcome emails, invoice receipts, and password alerts when rows are inserted into your Supabase Postgres database.

## 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.

```typescript
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

---

_Tags: Supabase, Postgres, Webhooks, Serverless_
