> ## 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 message from Firebase Auth

> Send a welcome message from a 1st-gen Cloud Function on the Firebase user-created event, with your API key in Secret Manager and anonymous signups skipped.

Firebase Auth has no webhook. It runs your code instead, as a Cloud Function on the user-created event.

## What you'll build

```mermaid theme={null}
flowchart LR
    A["User signs up"] --> B["Cloud Function"]
    B --> C["Create profile"]
    C --> D["Welcome message"]
```

## Prerequisites

* [A Courier Test API key](https://app.courier.com/~/test/platform/api-keys)
* A Firebase project with Authentication enabled, and the Firebase CLI
* A published [template](https://www.courier.com/docs/design/templates/overview.md) to send

## Use the non-blocking trigger

Firebase gives you two ways to run code when a user is created, and for a welcome message the older one is the right one.

**`functions.auth.user().onCreate()`** is a 1st-gen trigger. It runs after the user exists, asynchronously, and nothing you do in it can affect the signup. There is no 2nd-gen equivalent, and Firebase says so plainly: 2nd gen doesn't support Authentication triggers. The two generations coexist in one file, so this isn't a reason to avoid 2nd gen elsewhere.

**`beforeUserCreated`** is the 2nd-gen blocking function, and it is the wrong tool here for three reasons:

* It **requires upgrading the project** to Firebase Authentication with Identity Platform.
* It **must respond within 7 seconds**, after which Firebase returns an error and the client operation fails. A slow send would block a signup.
* Deleting the function without unregistering its trigger **prevents all users from authenticating**.

Blocking is for deciding whether someone may sign up. Sending a welcome isn't that, and a notification outage should never stop a registration.

## Set it up

**1. Install the dependencies**

In your functions directory:

```bash theme={null}
npm install firebase-functions firebase-admin @trycourier/courier
```

**2. Store your Courier key as a secret**

Firebase keeps secrets in Cloud Secret Manager and injects them at runtime.

```bash theme={null}
firebase functions:secrets:set COURIER_API_KEY
```

Bind it to the function that needs it. Secret values are hidden until the function runs, so they can't be read at deploy time.

**3. Write the function**

The handler receives a `UserRecord`, and it uses three fields off it: `uid`, `email`, and `displayName`.

```javascript functions/index.js lines theme={null}
const functions = require("firebase-functions/v1");
const Courier = require("@trycourier/courier");

exports.sendWelcome = functions
  .runWith({ secrets: ["COURIER_API_KEY"] })
  .auth.user()
  .onCreate(async (user) => {
    const { uid, email, displayName } = user;

    if (!email) return; // phone-only and anonymous signups have none

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

    // Firebase's uid becomes the Courier user id, so both systems agree.
    await courier.profiles.create(uid, {
      profile: { email, name: displayName },
    });

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

The `runWith` call is what grants access to the secret, which then arrives on `process.env`. Importing from `firebase-functions/v1` explicitly keeps this working alongside 2nd-gen functions in the same file.

**Anonymous and phone-only signups have no `email`, and the trigger fires for both.** Returning early stops a crash on the first anonymous session.

**4. Deploy**

```bash theme={null}
firebase deploy --only functions:sendWelcome
```

## Verify

**1. Create a user**

Sign up through your app, or add one from **Authentication → Users** in the Firebase console.

**2. Read the function log**

```bash theme={null}
firebase functions:log --only sendWelcome
```

The trigger is asynchronous, so a failure appears here and nowhere else. It won't surface to the user.

**3. Confirm the send**

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

## What the trigger covers

This trigger covers more sign-up routes than Auth0's equivalent.

| Sign-up route | Fires |
| :- | :- |
| Email and password | Yes |
| A federated provider, including Google, on first sign-in | Yes |
| Anonymous session | Yes, with no `email` |
| Created through the Admin SDK | Yes |
| A custom token | **No** |

The custom-token gap is the one to plan around. If you mint your own tokens for an existing identity system, this trigger never runs, and the profile write belongs wherever you create that user instead.
