> For the complete documentation index, see [llms.txt](https://codemash.gitbook.io/codemash/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codemash.gitbook.io/codemash/cloud/membership/triggers/new.md).

# Create a trigger

Create a membership trigger — pick the user event, then the action (send an email, push or SMS, call a webhook, run code, send an in-app event).

**Where:** Membership → Triggers → **Add New** · `/projects/<project>/membership/triggers/new`

## Before you start

* For **Send Email**: the Email module is on, with an [email template](/codemash/cloud/emails/templates.md) and an email integration (every project gets the Fake one, **Test Me**). For **Send Push Notification** / **Send SMS**: the same in that module. For **Call Webhook**: a destination in [Project → Settings → Webhooks](/codemash/cloud/project/settings/webhooks.md).
* You need the `membership:create` permission on triggers.

## Steps

1. Press **Add New**. Under **If this** → **Select trigger actions**, pick the event, for example **When registered**. The box below tells you what the action gets (*Usage: 'New\.User.\*'*). **Add activation code** adds code that runs first and can stop the trigger.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-39622efc6933a867e287f6c9daccab74ef784e8c%2Ftriggers-new-1.png?alt=media" alt="Add New Trigger: seven event cards, When registered is picked"><figcaption><p>The seven events.</p></figcaption></figure>
2. Under **... then**, open the action list and pick the action.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-91c1c60500004488fb71ea00a4be18ec6ebc3f9f%2Ftriggers-new-2.png?alt=media" alt="The action list open: Run Code, Send Email, Send Push Notification, Send SMS, Call Webhook, Send In-App (Realtime / SSE)"><figcaption><p>The action list.</p></figcaption></figure>

   | Action                                                     | What it does                                               |
   | ---------------------------------------------------------- | ---------------------------------------------------------- |
   | **Run Code**                                               | Runs an installed marketplace function with the user data. |
   | **Send Email** / **Send Push Notification** / **Send SMS** | Sends a template to the recipients you choose.             |
   | **Call Webhook**                                           | POSTs a signed event to the destinations you tick.         |
   | **Send In-App (Realtime / SSE)**                           | Sends the event live to connected apps or AI agents.       |
3. For **Send Email**, choose the recipients. The cards are **To new user**, **All users**, **Collection**, **Specified Users** and **Email Addresses**. For **Email Addresses**, type the address in **To: \***; **Single email strategy** sends each address its own email; **CC:** and **BCC:** are optional.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-ef635e65b890879c6391c01d3f43c0135e9ce415%2Ftriggers-new-3.png?alt=media" alt="Email Addresses picked, To team@example.com, Single email strategy not ticked, CC and BCC empty, then Template Details with Select template New sign-up"><figcaption><p>Recipients: an address.</p></figcaption></figure>
4. Under **Template Details**, choose **Select template** (for example `New sign-up`) and **Send in specific language** (leave **Each recipient's language**). **Template Tokens** lists the tokens of the template; until each has a value, a yellow box lists what is *Still missing*. Type a value, or press the tag button next to a token and pick a value (Project, Initiator, Recipient, Email Signatures, Email Footers).

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-7f00a9f61869365fcff495aaadc01179a8b58a99%2Ftriggers-new-4.png?alt=media" alt="Template Tokens: Recipient.FirstName Team; Email.SignatureId and Email.FooterId empty and red; the box Pick a value for every token before sending. Still missing: @Model.Email.SignatureId, @Model.Email.FooterId"><figcaption><p>Tokens still missing.</p></figcaption></figure>

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-419529994b46be57b150460fb416cbbf8dd07334%2Ftriggers-new-5.png?alt=media" alt="Template Tokens all set: Recipient.FirstName Team, Email.SignatureId emlsgn_3yOxEM…, Email.FooterId emlftr_2VgNmY…, Project.Name, Project.Url"><figcaption><p>Every token has a value. The signature and footer bring their own tokens (<code>Project.Name</code>, <code>Project.Url</code>).</p></figcaption></figure>
5. Under **Advanced settings**, **Send email on behalf of** sets who speaks in the message (`@Model.Initiator.User`); **Select email provider** is set to the project default (**Test Me (Fake)** here).
6. Fill the trigger's own fields and press **Save**:

   | Field                            | Required | What it does                                                           | Example                                      |
   | -------------------------------- | -------- | ---------------------------------------------------------------------- | -------------------------------------------- |
   | Order                            | no       | Run order when several triggers listen to one event; lower runs first. | `1`                                          |
   | Break execution queue on failure | no       | Stops the later triggers of this event when this one fails.            | off                                          |
   | Name the trigger                 | yes      | The name in the list.                                                  | `Tell the team about sign-ups`               |
   | Description                      | no       | Free text.                                                             | `Emails the team when a new user registers.` |
   | Is this trigger enabled?         | no       | On: the trigger runs. Off: saved but quiet.                            | on                                           |

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-e49e6f900955fbe2e219a53b64c60d7cbf0abb88%2Ftriggers-new-6.png?alt=media" alt="Lower part of the form: Send email on behalf of, Select email provider Test Me (Fake), Order, Name the trigger Tell the team about sign-ups, Description, the switch Is this trigger enabled? drawn off"><figcaption><p>The trigger's own fields.</p></figcaption></figure>

{% hint style="warning" %}
A new trigger starts **enabled**. Today the **Is this trigger enabled?** switch is still drawn in the off position on a new trigger; that is a display fault — the trigger is saved enabled (the list shows **Enabled: Yes**). Disable it from the **Actions** menu if it should not run yet.
{% endhint %}

{% hint style="warning" %}
**To new user** cannot be saved today: **Save** does nothing and the server answers *Unsupported recipientsSourceType: Trigger subject*. So a trigger cannot email the user the event is about yet; a welcome email for new users is set in [Settings → Onboarding](/codemash/cloud/membership/settings/onboarding.md) (**Welcome email template**). To email a fixed inbox, use **Email Addresses** as in this example.
{% endhint %}

## Result

Cloud goes back to **Membership Triggers** with the new row (**When registered**, **Send Email**, **Enabled: Yes**). The next registration runs it: in the example, registering `katherine.johnson@example.com` produced an email campaign with status **Completed** through **Test Me**, visible in **Email → Campaigns**.

<figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-5dc7982c9d17be6ac7414f30f733f4cd2c01d15f%2Ftriggers-new-7.png?alt=media" alt="Membership Triggers list with Tell the team about sign-ups, When registered, Send Email, Enabled Yes"><figcaption><p>The new trigger in the list.</p></figcaption></figure>

<figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-cd5293ee090dd2259940825d372985384286974d%2Ftriggers-new-8.png?alt=media" alt="Email Campaigns list with one row: Completed, Specific Email Addresses, template New sign-up, integration Test Me (Default)"><figcaption><p>The email the trigger sent, in Email → Campaigns.</p></figcaption></figure>

A run that cannot send (template misses a project language, provider off) marks the trigger **Needs attention** — see [Email from a trigger](/codemash/cloud/emails.md).

## Change or remove it later

* **Edit** — [Edit a trigger](/codemash/cloud/membership/triggers/edit.md).
* **Disable / Enable** — [Enable or disable a trigger](/codemash/cloud/membership/triggers/enable.md).
* **Delete** — [Delete a trigger](/codemash/cloud/membership/triggers/delete.md).

## Do it from code

```ts
const { id } = await norbix.hub.membership.saveMembershipTrigger({
  trigger: {
    type: 'Membership',
    when: 'OnBlocked',
    name: 'Tell security about blocks',
    isEnabled: true,
    action: {
      type: 'email',
      templateId: 'tmpl_6btnbTgAdejURfa25HN4oV', // Confirm account closing
      integrationId: 'int_47LNjxwLlHjmUEWXJbLmiZ', // the email provider (Test Me)
      deliverySettings: {
        recipientsSourceType: 'Email',
        recipients: ['security@example.com'],
        mappedTokens: [
          { key: 'Recipient.FirstName', value: 'Security team', resolver: 'Custom' },
          { key: 'Email.SignatureId', value: 'emlsgn_3yOxEMADM1n3jwBx1LkBoO', resolver: 'Custom' },
          { key: 'Email.FooterId', value: 'emlftr_2VgNmYXcOk1CEE40QAjNe5', resolver: 'Custom' },
        ],
      },
    },
  },
});
console.log(id); // trg_…
```

```sh
norbix hub membership trigger get trg_76FRQnqeqTbMDCwqAzCflQ
```

The [SDK reference](/codemash/sdks/membership/triggers/save-membership-trigger.md) has one example per action. The client setup is on the [Membership page](/codemash/cloud/membership.md#use-it-from-code). API: [Save Membership Trigger](/codemash/api-reference/membership/triggers/save-membership-trigger.md)
