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

# Invoke a journey

> Start a journey run from the API with an idempotency key, find it by run ID, and cancel one run or every run that shares a cancellation token.

> [!NOTE]
>
> [Journeys](https://www.courier.com/docs/journeys/overview.md) covers the snapshot, delays, and cancellation model.

Start a Journey run from the API, then find it by its run ID.

## Prerequisites

* A published journey with an **API** trigger
* [A Courier API key](https://app.courier.com/settings/api-keys)

## Run a journey

**1. Invoke a journey**

Start a run with the invoke endpoint, referencing the journey by its ID or alias in the path.

* `user_id`, or a `profile` with contact info, for the recipient.
* `data`, optional, matching the trigger schema.
* An `Idempotency-Key` header, so a retried request returns the same run instead of starting a second one.

The response returns a `runId`.

```javascript Node.js theme={null}
const journeysInvokeResponse = await client.journeys.invoke('welcome-journey', {
  user_id: 'user_123',
  data: { order_id: 'ORD-9042', order_total: 49.99 },
});
```

```python Python theme={null}
journeys_invoke_response = client.journeys.invoke(
    template_id="welcome-journey",
    user_id="user_123",
    data={
        "order_id": "ORD-9042",
        "order_total": 49.99,
    },
)
```

```bash cURL theme={null}
curl -X POST https://api.courier.com/journeys/welcome-journey/invoke \
  -H "Authorization: Bearer $COURIER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: unique-key-123" \
  -d '{
    "user_id": "user_123",
    "data": { "order_id": "ORD-9042", "order_total": 49.99 }
  }'
```

```ruby Ruby theme={null}
journeys_invoke_response = courier.journeys.invoke(
  "welcome-journey",
  user_id: "user_123",
  data: {order_id: "ORD-9042", order_total: 49.99}
)
```

```go Go theme={null}
journeysInvokeResponse, err := client.Journeys.Invoke(
	context.TODO(),
	"welcome-journey",
	courier.JourneyInvokeParams{
		JourneysInvokeRequest: courier.JourneysInvokeRequestParam{
			UserID: courier.String("user_123"),
			Data: map[string]any{
				"order_id":    "ORD-9042",
				"order_total": 49.99,
			},
		},
	},
)
```

```java Java theme={null}
JourneyInvokeParams params = JourneyInvokeParams.builder()
    .templateId("welcome-journey")
    .journeysInvokeRequest(JourneysInvokeRequest.builder()
        .userId("user_123")
        .data(JourneysInvokeRequest.Data.builder()
            .putAdditionalProperty("order_id", JsonValue.from("ORD-9042"))
            .putAdditionalProperty("order_total", JsonValue.from(49.99))
            .build())
        .build())
    .build();
JourneysInvokeResponse journeysInvokeResponse = client.journeys().invoke(params);
```

```php PHP theme={null}
$journeysInvokeResponse = $client->journeys->invoke(
  'welcome-journey',
  userID: 'user_123',
  data: ['order_id' => 'ORD-9042', 'order_total' => 49.99],
);
```

```csharp C# theme={null}
JourneyInvokeParams parameters = new()
{
    TemplateID = "welcome-journey",
    UserID = "user_123",
    Data = new Dictionary<string, JsonElement>()
    {
        { "order_id", JsonSerializer.SerializeToElement("ORD-9042") },
        { "order_total", JsonSerializer.SerializeToElement(49.99) },
    },
};

var journeysInvokeResponse = await client.Journeys.Invoke(parameters);
```

```bash CLI theme={null}
courier journeys invoke \
  --api-key "$COURIER_API_KEY" \
  --template-id welcome-journey \
  --user-id user_123 \
  --data '{order_id: ORD-9042, order_total: 49.99}'
```

```text MCP theme={null}
With Courier MCP, start my welcome-journey for user_123 with the order details.
```

**2. Inspect a run**

Take the `runId` from the invoke response, open the journey's **Logs** tab, and search for it. The detail view overlays each node's outcome on the graph. Click a node to see its step context: input data, profile, conditions evaluated, and output. Over the API, [`GET /journeys/runs/{run_id}/steps`](https://www.courier.com/docs/api-reference/journeys/list-steps-for-a-journey-run) returns the same steps. See [journey logs](https://www.courier.com/docs/monitor/logs.md#journey-logs) for the run states and what each view shows.

## Cancel a run

Cancel in-flight runs with the cancel endpoint. Target either a `cancelation_token`, which cancels every run sharing it, or a single `run_id`. You set the token in the journey's settings, so runs carry it from the moment they start.

```javascript Node.js theme={null}
const cancelJourneyResponse = await client.journeys.cancel({ cancelation_token: 'order-9042-flow' });
```

```python Python highlight={2} theme={null}
cancel_journey_response = client.journeys.cancel(
    cancelation_token="order-9042-flow",
)
```

```bash cURL highlight={5} theme={null}
curl -X POST https://api.courier.com/journeys/cancel \
  -H "Authorization: Bearer $COURIER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cancelation_token": "order-9042-flow"
  }'
```

```ruby Ruby theme={null}
cancel_journey_response = courier.journeys.cancel(cancel_journey_request: {cancelation_token: "order-9042-flow"})
```

```go Go highlight={3-4} theme={null}
cancelJourneyResponse, err := client.Journeys.Cancel(context.TODO(), courier.JourneyCancelParams{
	CancelJourneyRequest: courier.CancelJourneyRequestUnionParam{
		OfByCancelationToken: &courier.CancelJourneyRequestByCancelationTokenParam{
			CancelationToken: "order-9042-flow",
		},
	},
})
```

```java Java highlight={1-2} theme={null}
CancelJourneyRequest.ByCancelationToken params = CancelJourneyRequest.ByCancelationToken.builder()
    .cancelationToken("order-9042-flow")
    .build();
CancelJourneyResponse cancelJourneyResponse = client.journeys().cancel(params);
```

```php PHP highlight={2} theme={null}
$cancelJourneyResponse = $client->journeys->cancel(
  cancelationToken: 'order-9042-flow'
);
```

```csharp C# highlight={3} theme={null}
JourneyCancelParams parameters = new()
{
    CancelJourneyRequest = new ByCancelationToken("order-9042-flow")
};

var cancelJourneyResponse = await client.Journeys.Cancel(parameters);
```

```bash CLI highlight={3} theme={null}
courier journeys cancel \
  --api-key "$COURIER_API_KEY" \
  --cancelation-token order-9042-flow
```

To cancel one run instead, pass the `run_id` from the invoke response in place of `cancelation_token`. [`POST /journeys/cancel`](https://www.courier.com/docs/api-reference/journeys/cancel-journey-runs) takes exactly one of the two. Sending both or neither returns `400`.

It returns `202` with the run and its resulting status:

| Status | Meaning |
| - | - |
| `CANCELED` | The run was active (or already canceled), and is canceled. |
| `PROCESSED` | The run had already finished. Nothing changed. |
| `ERROR` | The run had already ended in an error. Nothing changed. |

Only active runs are affected, so canceling is idempotent and always safe. A `run_id` Courier can't find for your workspace returns `404`. To build the token itself, or to cancel from inside a flow, see the [cancel node](https://www.courier.com/docs/journeys/nodes/cancel.md).
