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

# Courier PHP SDK

> Typed access to the Courier API from PHP 8.1+ with named parameters. Install the beta SDK with Composer, then send, issue JWTs, and run Journeys.

The Courier PHP SDK gives typed access to the [Courier REST API](https://www.courier.com/docs/reference/api-overview.md) from any PHP 8.1+ application.

It takes optional arguments as named parameters and returns typed response objects.

Available on
[ GitHub](https://github.com/trycourier/courier-php).

> [!NOTE]
>
> The PHP SDK is in beta. APIs may change between releases. [Share feedback or report issues on GitHub.](https://github.com/trycourier/courier-php/issues/new)

## Installation

Install from Packagist with Composer:

```bash theme={null}
composer require trycourier/courier
```

Requires PHP 8.1+. Latest version on [Packagist](https://packagist.org/packages/trycourier/courier).

## Quick start

```php theme={null}
<?php

use Courier\Client;

$client = new Client(apiKey: getenv('COURIER_API_KEY'));

$response = $client->send->message(
  message: [
    'to' => ['user_id' => 'user_123'],
    'template' => 'nt_01kx4h2jdafq8bk9aftxak4b40',
    'data' => ['name' => 'Sarah Bennett'],
  ],
);

var_dump($response->requestID);
```

> [!TIP]
>
> Pass your API key in the `COURIER_API_KEY` environment variable, or to the constructor: `new Client(apiKey: 'YOUR_COURIER_API_KEY')`.

## Authentication

Get your API key from [Platform → API Keys](https://app.courier.com/settings/api-keys). Set it as an environment variable:

```bash theme={null}
export COURIER_API_KEY="YOUR_COURIER_API_KEY"
```

Or pass it to the client constructor:

```php theme={null}
$client = new Client(apiKey: 'YOUR_COURIER_API_KEY');
```

## Sending notifications

Design your notification in [Design Studio](https://www.courier.com/docs/design/templates/design-studio.md), then reference it by ID:

```php theme={null}
$response = $client->send->message(
  message: [
    'to' => ['user_id' => 'user_123'],
    'template' => 'nt_01kx4h2jdafq8bk9aftxak4b40',
    'data' => ['order_id' => '10042', 'item_name' => 'Courier Hoodie'],
  ],
);
```

## Available resources

The SDK covers the full Courier API.

| Resource | Namespace | Description |
| - | - | - |
| Send | `$client->send` | Send messages to one or more recipients |
| Messages | `$client->messages` | Retrieve status, history, and content of sent messages |
| Profiles | `$client->profiles` | Create, update, and retrieve user profiles |
| Users | `$client->users` | Manage preferences, tenants, and push tokens per user |
| Auth | `$client->auth` | Issue JWT tokens for client-side SDK authentication |
| Bulk | `$client->bulk` | Send messages to large recipient lists via jobs |
| Lists | `$client->lists` | Manage subscription lists and their subscribers |
| Audiences | `$client->audiences` | Define and query audience segments |
| Tenants | `$client->tenants` | Manage tenants for multi-tenant setups |
| Journeys | `$client->journeys` | Invoke and cancel runs. Create, list, publish, and archive [journeys](https://www.courier.com/docs/journeys/overview.md) |
| Brands | `$client->brands` | Manage brand settings (logos, colors, templates) |
| Notifications | `$client->notifications` | List and inspect notification templates |
| Translations | `$client->translations` | Workspace `.po` strings for the `{{t}}` helper |

## Common operations

### Checking message status

```php theme={null}
$message = $client->messages->retrieve('your-message-id');
echo $message->status;
echo $message->delivered;
```

### Managing user profiles

```php theme={null}
$client->profiles->create('user_123', profile: [
    'email' => 'sarah@acme-corp.com',
    'phone_number' => '+15551234567',
    'name' => 'Sarah Bennett',
]);

$profile = $client->profiles->retrieve('user_123');
```

### Issuing JWT tokens

Courier's [client-side SDKs](https://www.courier.com/docs/sdk-libraries/sdks-overview.md) (React, JavaScript, mobile) authenticate users with a JWT your backend issues. [Authenticate users](https://www.courier.com/docs/in-app/authenticate-users.md) covers the token flow, the scope strings, and token refresh. Use `$client->auth->issueToken`:

```php theme={null}
$result = $client->auth->issueToken(
  scope: 'user_id:user_123 inbox:read:messages inbox:write:events',
  expiresIn: '2 days',
);

echo $result->token;
```

| Scope | Permission |
| - | - |
| `user_id:<id>` | Which user the token is for (required) |
| `inbox:read:messages` | Read inbox messages |
| `inbox:write:events` | Mark messages as read/archived |
| `read:preferences` | Read notification preferences |
| `write:preferences` | Update notification preferences |
| `write:user-tokens` | Register push notification tokens |

## Configuration

### Error handling

The SDK throws typed exceptions for API failures. All extend `Courier\Core\Exceptions\APIException`:

```php theme={null}
use Courier\Core\Exceptions\APIConnectionException;
use Courier\Core\Exceptions\RateLimitException;
use Courier\Core\Exceptions\APIStatusException;

try {
  $client->send->message(message: [
    'to' => ['user_id' => 'user_123'],
    'template' => 'nt_01kx4h2jdafq8bk9aftxak4b40',
  ]);
} catch (APIConnectionException $e) {
  echo "The server could not be reached\n";
} catch (RateLimitException $e) {
  echo "Rate limited; back off a bit.\n";
} catch (APIStatusException $e) {
  echo "API error: " . $e->getMessage() . "\n";
}
```

### Retries

The SDK retries failed requests up to 2 times with exponential backoff.

```php theme={null}
// Disable retries
$client = new Client(requestOptions: ['maxRetries' => 0]);

// Or per-request
$client->send->message(
  message: ['to' => ['user_id' => 'user_123'], 'template' => 'nt_01kx4h2jdafq8bk9aftxak4b40'],
  requestOptions: ['maxRetries' => 5],
);
```

## Journeys

[Journeys](https://www.courier.com/docs/journeys/overview.md) are multi-step workflows: send, delay, branch, throttle, digest, and more. Invoke one by ID or alias to start a run. Cancel runs by ID, or by a shared cancelation token.

```php theme={null}
// Start a run (by journey ID or alias); the response includes runID
$response = $client->journeys->invoke(
  templateID: 'your-journey-id',
  userID: 'user_123',
  data: ['order_id' => 'A-1042'],
);
echo $response->runID;
```

> [!NOTE]
>
> `cancel` takes exactly one of `run_id` or `cancelation_token` (spelled with one "l"). Build and manage journeys from code with `create`, `list`, `publish`, and `archive`. See [Build journeys via API](https://www.courier.com/docs/journeys/build.md).

## More operations

A few more resources from the full [Courier REST API](https://www.courier.com/docs/reference/api-overview.md):

| Resource | Method | Use case |
| - | - | - |
| User preferences | `$client->users->preferences->retrieve($userId)` | Fetch a user's notification preferences for your [preference center](https://www.courier.com/docs/recipients/preferences/overview.md) |
| Cancel a message | `$client->messages->cancel($messageId)` | Cancel a delayed or queued message before delivery |
| Push tokens | `$client->users->tokens->addSingle($token, userID: $userId, providerKey: 'firebase-fcm')` | Register a device push token for [iOS](https://www.courier.com/docs/sdk-libraries/ios.md), [Android](https://www.courier.com/docs/sdk-libraries/android.md), [React Native](https://www.courier.com/docs/sdk-libraries/react-native.md), or [Flutter](https://www.courier.com/docs/sdk-libraries/flutter.md) |

> [card] [API Reference](https://www.courier.com/docs/reference/api-overview.md)
>
> Full REST API docs with request/response examples.

> [card] [Send API](https://www.courier.com/docs/send/overview.md)
>
> Learn about the Send endpoint, routing, and message options.

> [card] [Quickstart](https://www.courier.com/docs/get-started/send-a-message.md)
>
> Send your first notification in under two minutes.

> [card] [GitHub](https://github.com/trycourier/courier-php)
>
> Source code, issues, and changelog.

## Idempotent sends

Pass an idempotency key and Courier replays the first response for that key instead of sending again. The SDK sets the `Idempotency-Key` header for you. [Idempotency](https://www.courier.com/docs/reference/api-overview.md#idempotency) covers the scoping rule and the replay window.

```php theme={null}
$response = $client->send->message(
  message: ['to' => ['user_id' => 'user_123'], 'template' => 'nt_01kx4h2jdafq8bk9aftxak4b40'],
  idempotencyKey: '6f3b8c2a-9d4e-4b7a-a1c5-2e8f0d5b7a91',
);
```
