trycourier / courier
Courier PHP SDK
Installs: 598 450
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 17
Forks: 7
Open Issues: 3
pkg:composer/trycourier/courier
Requires
- php: ^8.1
- php-http/discovery: ^1
- psr/http-client: ^1
- psr/http-client-implementation: ^1
- psr/http-factory-implementation: ^1
- psr/http-message: ^1|^2
Requires (Dev)
- dev-main
- v2.4.0-alpha0
- 2.2.0
- 2.1.0
- v2.0.2
- v2.0.0
- v1.12.0
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- dev-release-please--branches--main--changes--next
- dev-next
- dev-generated
- dev-fern-bot/2025-10-02T18-12Z
- dev-fern-bot/12-02-2024-0324PM
- dev-fern-bot-patch-1
- dev-fern-bot/11-25-2024-0822PM
- dev-C-10109/php-sdk-accounts-tenants
- dev-user-account-api
- dev-chris/c-9805-update-php-sdk
- dev-i18n1.1
- dev-C-6481
- dev-drew/c-6187-add-support-for-token-management-to-php
- dev-v2-support
- dev-C-4964
- dev-drew/c-3969-php-sdk-automations-api
- dev-more-endpoints
- dev-C-3747-notifications-api
- dev-add-template
This package is auto-updated.
Last update: 2025-10-22 02:38:49 UTC
README
Note
The Courier PHP API Library is currently in beta and we're excited for you to experiment with it!
This library has not yet been exhaustively tested in production environments and may be missing some features you'd expect in a stable release. As we continue development, there may be breaking changes that require updates to your code.
We'd love your feedback! Please share any suggestions, bug reports, feature requests, or general thoughts by filing an issue.
The Courier PHP library provides convenient access to the Courier REST API from any PHP 8.1.0+ application.
It is generated with Stainless.
Documentation
Installation
To use this package, install via Composer by adding the following to your application's composer.json
:
{ "repositories": [ { "type": "vcs", "url": "git@github.com:trycourier/courier-php.git" } ], "require": { "org-placeholder/courier": "dev-main" } }
Usage
This library uses named parameters to specify optional arguments. Parameters with a default value must be set by name.
<?php use Courier\Client; use Courier\UserRecipient; use Courier\Send\SendMessageParams\Message; $client = new Client(apiKey: getenv("COURIER_API_KEY") ?: "My API Key"); $response = $client->send->message( (new Message) ->withTo((new UserRecipient)->withUserID("your_user_id")) ->withTemplate("your_template") ->withData(["foo" => "bar"]), ); var_dump($response->requestId);
Value Objects
It is recommended to use the static with
constructor BaseCheck::with(id: "id", ...)
and named parameters to initialize value objects.
However, builders are also provided (new BaseCheck)->withID("id")
.
Handling errors
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of Courier\Core\Exceptions\APIException
will be thrown:
<?php use Courier\UserRecipient; use Courier\Core\Exceptions\APIConnectionException; use Courier\Send\SendMessageParams\Message; try { $response = $client->send->message( (new Message) ->withTo((new UserRecipient)->withUserID("your_user_id")) ->withTemplate("your_template") ->withData(["foo" => "bar"]), ); } catch (APIConnectionException $e) { echo "The server could not be reached", PHP_EOL; var_dump($e->getPrevious()); } catch (RateLimitError $_) { echo "A 429 status code was received; we should back off a bit.", PHP_EOL; } catch (APIStatusError $e) { echo "Another non-200-range status code was received", PHP_EOL; echo $e->getMessage(); }
Error codes are as follows:
Cause | Error Type |
---|---|
HTTP 400 | BadRequestException |
HTTP 401 | AuthenticationException |
HTTP 403 | PermissionDeniedException |
HTTP 404 | NotFoundException |
HTTP 409 | ConflictException |
HTTP 422 | UnprocessableEntityException |
HTTP 429 | RateLimitException |
HTTP >= 500 | InternalServerException |
Other HTTP error | APIStatusException |
Timeout | APITimeoutException |
Network error | APIConnectionException |
Retries
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default.
You can use the maxRetries
option to configure or disable this:
<?php use Courier\Client; use Courier\UserRecipient; use Courier\RequestOptions; use Courier\Send\SendMessageParams\Message; // Configure the default for all requests: $client = new Client(maxRetries: 0); // Or, configure per-request: $result = $client->send->message( (new Message) ->withTo((new UserRecipient)->withUserID("your_user_id")) ->withTemplate("your_template") ->withData(["foo" => "bar"]), requestOptions: RequestOptions::with(maxRetries: 5), );
Advanced concepts
Making custom or undocumented requests
Undocumented properties
You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
Note: the extra*
parameters of the same name overrides the documented parameters.
<?php use Courier\UserRecipient; use Courier\RequestOptions; use Courier\Send\SendMessageParams\Message; $response = $client->send->message( (new Message) ->withTo((new UserRecipient)->withUserID("your_user_id")) ->withTemplate("your_template") ->withData(["foo" => "bar"]), requestOptions: RequestOptions::with( extraQueryParams: ["my_query_parameter" => "value"], extraBodyParams: ["my_body_parameter" => "value"], extraHeaders: ["my-header" => "value"], ), ); var_dump($response["my_undocumented_property"]);
Undocumented request params
If you want to explicitly send an extra param, you can do so with the extra_query
, extra_body
, and extra_headers
under the request_options:
parameter when making a request, as seen in the examples above.
Undocumented endpoints
To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using client.request
, like so:
<?php $response = $client->request( method: "post", path: '/undocumented/endpoint', query: ['dog' => 'woof'], headers: ['useful-header' => 'interesting-value'], body: ['hello' => 'world'] );
Versioning
This package follows SemVer conventions. As the library is in initial development and has a major version of 0
, APIs may change at any time.
This package considers improvements to the (non-runtime) PHPDoc type definitions to be non-breaking changes.
Requirements
PHP 8.1.0 or higher.