> ## Documentation Index
> Fetch the complete documentation index at: https://www.courier.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Install the Courier skill before writing code: `npx skills add trycourier/courier-skills`. It carries the verified SDK shapes and the rules you cannot get wrong.
> Authenticate every request with `Authorization: Bearer <API_KEY>`. A workspace has several environments and each has its own keys, which are plain `pk_` strings with no environment prefix. Start with Test.
> Send with `client.send.message` from the Node SDK (`@trycourier/courier` v7 and later, where the client is the default import). Reference a template by its `nt_` id or its alias.
> A send accepts a bare Elemental element list, but storing content on a template requires the top-level elements wrapped in a channel element.
> To translate a template, write one locale at a time with `PUT /notifications/{id}/locales/{localeId}`, which merges into the existing translations. `PUT /notifications/{id}/content` and `PUT /notifications/{id}/elements/{elementId}` overwrite translations along with the content, so send each element's `id` and `locales` back, and first remove every `checksum` and any `locales` key that starts with an underscore.
> Templates and journeys can be built in the Courier app or created through the API. Either way they live in the workspace and are referenced by ID when you send.
> The hosted MCP server is https://mcp.courier.com. For a briefing on what Courier is and when to use it, read https://www.courier.com/llms.txt.
> Prefer the Guides tab for how-do-I questions and the Docs tab for how-does-it-behave questions. The API reference lives under /api-reference.

# Send a welcome email from Supabase

> Turn a Supabase Database Webhook on a new row into a welcome email, secured with a shared-secret header because Supabase doesn't sign its webhook payloads.

Supabase fires a Database Webhook when a row changes. Mirror new signups into a table and one becomes a welcome email.

## What you'll build

```mermaid theme={null}
flowchart LR
    A["New row"] --> B["Database Webhook"]
    B --> C{"Secret matches?"}
    C -->|Yes| D["Create profile"]
    D --> E["Welcome email"]
    C -->|No| F["Reject"]
```

## Prerequisites

* [A Courier Test API key](https://app.courier.com/~/test/platform/api-keys)
* A Supabase project, and somewhere to host an HTTPS route
* A published [template](https://www.courier.com/docs/design/templates/overview.md) to send

## Secure it with a shared secret

Supabase Database Webhooks are built on `pg_net`, and they **don't sign the payload**. Clerk uses Svix and Stripe signs the raw bytes, so both let you prove a request came from them. Supabase gives you custom HTTP headers instead.

That means the check is yours to add, and it isn't optional. Your route will sit on a public URL, and without a secret anyone who finds it can post a fake signup.

Generate a long random value, add it as a header on the webhook, and compare it in the handler with a constant-time comparison.

## Set it up

**1. Mirror new signups into a public table**

Point the webhook at a table in `public`, not at `auth.users` directly.

The `auth` schema belongs to the `supabase_auth_admin` role, which holds only the permissions it needs for authentication. A trigger firing there and reaching outside the schema hits `permission denied for schema auth`. Most Supabase apps already keep a `profiles` table for exactly this reason.

```sql theme={null}
create table public.profiles (
  id uuid primary key references auth.users on delete cascade,
  email text,
  full_name text
);

-- security definer runs as the function's owner, so it may write to public.
create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = ''
as $$
begin
  insert into public.profiles (id, email, full_name)
  values (new.id, new.email, new.raw_user_meta_data ->> 'full_name');
  return new;
end;
$$;

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute function public.handle_new_user();
```

`security definer` is the load-bearing line. Without it the trigger runs as `supabase_auth_admin` and can't write to `public`.

**2. Create the webhook**

In the Supabase Dashboard, go to **Database → Webhooks** and create one:

* **Table**: `public.profiles`
* **Events**: `Insert`
* **Type**: HTTP Request, `POST`, pointing at your route
* **HTTP Headers**: add `x-webhook-secret` with your random value

Supabase sends this payload, whatever the table:

```json theme={null}
{
  "type": "INSERT",
  "table": "profiles",
  "schema": "public",
  "record": { "id": "…", "email": "…", "full_name": "…" },
  "old_record": null
}
```

`record` is the new row and `old_record` is the previous one, which is `null` on an insert.

**3. Write the route handler**

Compare the secret before doing anything else, and use `timingSafeEqual` rather than `===` so the comparison doesn't leak the value a character at a time.

```tsx app/api/supabase/route.ts lines theme={null}
import { timingSafeEqual } from "node:crypto";
import Courier from "@trycourier/courier";

const courier = new Courier({ apiKey: process.env.COURIER_API_KEY! });

function secretMatches(received: string | null) {
  const expected = process.env.SUPABASE_WEBHOOK_SECRET!;
  if (!received || received.length !== expected.length) return false;
  return timingSafeEqual(Buffer.from(received), Buffer.from(expected));
}

export async function POST(request: Request) {
  if (!secretMatches(request.headers.get("x-webhook-secret"))) {
    return new Response("Unauthorized", { status: 401 });
  }

  const { type, table, record } = await request.json();

  if (type !== "INSERT" || table !== "profiles") {
    return new Response("Ignored", { status: 200 });
  }

  // Supabase's uuid becomes the Courier user id, so both systems agree.
  await courier.profiles.create(record.id, {
    profile: { email: record.email, name: record.full_name },
  });

  await courier.send.message({
    message: {
      to: { user_id: record.id },
      template: "nt_01kx4h2jdafq8bk9aftxak4b40",
      data: { name: record.full_name ?? "there" },
    },
  });

  return new Response("OK", { status: 200 });
}
```

The trigger already flattened `raw_user_meta_data` into a column, so the handler reads `record.full_name` rather than digging through the auth payload.

**4. Add the environment values**

```bash .env.local theme={null}
COURIER_API_KEY=YOUR_COURIER_API_KEY
SUPABASE_WEBHOOK_SECRET=the-same-random-value
```

## Verify

**1. Create a user**

Sign up through your app, or add a user from **Authentication → Users** in the Supabase Dashboard.

**2. Check the webhook ran**

Supabase records each delivery with its response code under the webhook in **Database → Webhooks**. A `401` means the secret doesn't match.

**3. Confirm the send**

Open [Logs](https://app.courier.com/logs) and confirm the message.

## Adapt it for your own tables

The payload shape is the same for every table in `public`. That makes the same handler useful well beyond signup.

| Table and event | What to send |
| :- | :- |
| `profiles` INSERT | Welcome |
| `orders` INSERT | Order confirmation |
| `orders` UPDATE | A shipping update, comparing `record` to `old_record` |
| `comments` INSERT | Notify everyone [watching](https://www.courier.com/docs/guides/notify-everyone-watching.md) the parent record |

`old_record` carries the previous values, so your handler decides whether a change is worth a notification. Firing on every update turns a useful alert into noise.
