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

# Sync events between Segment and Courier

> Send Segment identify, track, and group events into Courier to update profiles and start journeys, and stream message and audience events back.

## Segment to Courier

Courier's Segment destination sends data from your web or mobile app into Courier.

Events appear on the [Segment integration page](https://app.courier.com/integrations/catalog/segment) in Courier Studio, where you map them to notification workflows. For example, an `analytics.identify` event can trigger an automation that welcomes a new user.

[Start a journey from Segment](https://www.courier.com/docs/guides/notify-from-segment.md) walks that setup end to end.

**Supported Segment events:**

* `analytics.group`
* `analytics.identify`
* `analytics.track`

### Connecting Segment

**1. Open the Segment Destinations catalog**

In the Segment app, open the [Destinations](https://segment.com/docs/connections/destinations) catalog page.

**2. Add a destination**

Click **Add Destination**.

**3. Select the Courier destination**

Search the catalog for Courier and select the Courier destination.

**4. Choose a source**

Choose the Source that sends data to the Courier destination.

**5. Get your Courier API Key**

On the [Courier Integrations page](https://app.courier.com/integrations), search for Segment and open it to get your Courier API key. If there's no key there, copy one from [Platform → API Keys](https://app.courier.com/settings/api-keys).

**6. Enter the API Key in Segment**

In Segment, enter the Courier API Key or Auth Token in the Courier destination's API Key field.

**7. Validate the setup**

Open the destination's "Event Tester" in Segment and click "Send Event" at the bottom of the page. In Courier Studio, refresh or wait a few seconds. The test event appears in the list if setup succeeded.

### Identify calls

Segment Identify calls connect a user to their actions (Segment events) and record traits about them.
Segment identifies the user by User ID, and the call can carry traits such as name and email.
Courier uses those traits to update recipient profiles over time. See the [Segment Identify spec](https://segment.com/docs/connections/spec/identify/) and [Courier Profiles API](https://www.courier.com/docs/api-reference/user-profiles/get-a-profile.md) for details.

Example payload

```json theme={null}
{
  "messageId": "segment-test-message-iskh4123",
  "timestamp": "2024-05-21T18:00:18.913Z",
  "type": "identify",
  "email": "sarah@acme-corp.com",
  "traits": {
    "trait1": 2,
    "email": "sarah@acme-corp.com"
  },
  "userId": "user_123"
}
```

### Track calls

Segment Track calls record actions your users perform, along with any properties on those actions. See the [Segment Track spec](https://segment.com/docs/connections/spec/track/) for details.

Track events appear in Courier with a `track/` prefix. Courier reads data from the track's `properties` object. Map track events to Courier journeys to send notifications from them.

An example Segment API call:

```javascript theme={null}
analytics.track('Login Button Clicked', {
  messageId: "segment-test-message-a8rmf",
  timestamp: "2021-12-07T08:41:59.410Z",
  type: "track",
  email: "sarah@acme-corp.com",
  projectId: "4GgKeBoVJkT9EZL4vAmduv",
  properties: {
    property1: 1,
    property2: "test",
    property3: true
  },
  userId: "user_123",
  event: "Segment Test Event Name"
})
```

Courier receives that call as a track event:

```json theme={null}
{
  "messageId": "segment-test-message-a8rmf",
  "timestamp": "2021-12-07T08:41:59.410Z",
  "type": "track",
  "email": "sarah@acme-corp.com",
  "projectId": "4GgKeBoVJkT9EZL4vAmduv",
  "properties": {
    "property1": 1,
    "property2": "test",
    "property3": true
  },
  "userId": "user_123",
  "event": "Segment Test Event Name"
}
```

A journey started by that event reads its properties under `data.properties`, so a condition or branch on `property1` uses the path `data.properties.property1`:

```json theme={null}
{
  "data": {
    "properties": {
      "property1": 1,
      "property2": "test",
      "property3": true
    }
  }
}
```

### Troubleshooting

A persistent "No Segment events received yet." message usually means Segment couldn't set up your Courier workspace as a destination. To get the test event through:

1. Confirm the API Key was copied into Segment exactly.
2. Check where the `email` property sits in Segment's test JSON object. On an identify call it belongs inside the `traits` object, not at the top level. Move it and resend the test event.
3. If it still fails, reach out to Courier Support.

***

## Courier to Segment

Configure Courier as a Segment Source, and Courier message and audience events flow back into Segment for analytics.

### Setting up Courier as a Segment source

**1. Open the Sources page**

In Segment, open the *Sources* page under "Connections".

**2. Select your framework**

Select your framework, for example Node.js.

**3. Obtain a write key**

Segment then shows instructions for adding it to that codebase. Copy the write key from this page.

**4. Paste the write key into Courier**

In Courier's Segment configuration, paste your [Segment Write Key](https://segment.com/docs/connections/find-writekey/) into the *Segment write key* field.

Courier starts sending updates to Segment once the write key is saved.

### Courier events sent to Segment

With Courier as a Segment source, Segment can track these events:

#### Message events

* `Message Clicked`
* `Message Delivered`
* `Message Opened`
* `Message Sent`
* `Message Undeliverable`
* `Message Unroutable`

#### Audience events

* `Audience User Matched`
* `Audience User Unmatched`

#### Preferences events

* `User Subscribed`
* `User Unsubscribed`
