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

> Turn Clerk's user.created webhook into a Courier profile and a welcome email, through your own route that verifies the Svix signature before it sends.

Clerk fires `user.created` the moment someone signs up. This guide turns that into a Courier profile and a welcome message.

## What you'll build

```mermaid theme={null}
flowchart LR
    A["Sign-up"] --> B["Clerk webhook"]
    B --> C{"Signature valid?"}
    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 Clerk application, and somewhere to host an HTTPS route
* A published [template](https://www.courier.com/docs/design/templates/overview.md) to send

## Why this needs a route in your app

Courier has an [inbound webhook](https://www.courier.com/docs/monitor/webhooks/inbound.md) that many sources can post to directly. Clerk isn't one of them, for three reasons.

**The field names don't line up.** Courier reads `event` and `userId` from a payload. Clerk sends `type` and `data.id`. Posted straight through, the event arrives named `custom` with no recipient attached.

**The user doesn't exist yet.** An inbound event only starts a journey when its `userId` matches an existing Courier user. On `user.created` there is nothing to match, so the profile has to be written first.

**Courier doesn't verify Clerk's signature.** Courier's inbound URL is its own credential and accepts any JSON sent to it. Verify the signature in your own endpoint before forwarding the event, because that is what stops a forged sign-up.

So the shape is Clerk to your route to Courier. Your route is where verification happens and where the profile is created.

## Set it up

**1. Install the packages**

```bash theme={null}
npm install @clerk/backend @trycourier/courier
```

Put both secrets in your environment. Clerk shows the signing secret when you create the endpoint in the next step.

```bash .env.local theme={null}
COURIER_API_KEY=YOUR_COURIER_API_KEY
CLERK_WEBHOOK_SIGNING_SECRET=whsec_...
```

**2. Write the route handler**

`verifyWebhook` checks the Svix signature Clerk sends and throws when it fails. Verify before you read anything out of the body.

```tsx app/api/clerk/route.ts lines theme={null}
import { verifyWebhook } from "@clerk/backend/webhooks";
import Courier from "@trycourier/courier";

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

export async function POST(request: Request) {
  let evt;
  try {
    // Throws on a bad signature or a replayed request.
    evt = await verifyWebhook(request);
  } catch {
    return new Response("Invalid signature", { status: 400 });
  }

  if (evt.type !== "user.created") {
    return new Response("Ignored", { status: 200 });
  }

  const { id, email_addresses, primary_email_address_id, first_name } = evt.data;

  // Match the primary id rather than taking [0]. A user with several
  // addresses can have the primary at any index.
  const email = email_addresses?.find((e) => e.id === primary_email_address_id)
    ?.email_address;

  if (!email) {
    return new Response("No email on user", { status: 200 });
  }

  // Use Clerk's id as the Courier user id so both systems agree.
  await courier.profiles.create(id, {
    profile: { email, given_name: first_name ?? undefined },
  });

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

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

**Clerk's `id` becomes the Courier `user_id`.** One identifier across both systems means every later send addresses the user without a lookup table.

**A non-matching event returns `200`.** Clerk retries anything that isn't a success, so returning an error for an event you deliberately ignore creates a retry loop.

**3. Point Clerk at the route**

In the Clerk dashboard, under **Configure → Webhooks**, add an endpoint for your route's public URL and subscribe it to **`user.created`**.

Copy the signing secret Clerk shows into `CLERK_WEBHOOK_SIGNING_SECRET`.

> [!NOTE]
>
> Clerk needs a public HTTPS URL, so `localhost` won't receive events. Use a tunnel such as `ngrok` while developing, and Clerk's **Testing** tab to replay an event without signing up each time.

## Verify

**1. Send a test event**

Use the **Testing** tab in Clerk's webhook settings to send a `user.created` event. Your route should answer `200`.

**2. Check the profile exists**

Read it back with [`GET /profiles/{user_id}`](https://www.courier.com/docs/api-reference/user-profiles/get-a-profile), using the Clerk user id. The email you expect should be on it.

**3. Check the message went out**

Open [Logs](https://app.courier.com/logs) and confirm the send. A `SENT` status means the provider accepted it.

If the profile exists but nothing sent, the send is the problem rather than the webhook. [Debug delivery](https://www.courier.com/docs/guides/debug-delivery.md) works that half.

## Adapt it for other events

The same handler shape covers the rest of Clerk's catalog. Switch on `evt.type` and keep one route:

| Clerk event | What to do |
| :- | :- |
| `user.created` | Create the profile, send a welcome |
| `user.updated` | Update the profile so an email change reaches the right inbox |
| `user.deleted` | Delete the profile, per [erasure](https://www.courier.com/docs/workspaces/compliance.md#data-access-and-deletion-requests) |
| `organizationMembership.created` | Add the user to a [tenant](https://www.courier.com/docs/tenants/overview.md) |

That last row is the one to reach for in a B2B app. Clerk organizations and Courier tenants model the same thing, so keeping them in step means branding and preferences follow the user automatically.
