$user->notify(new OrderShipped($order)) queues after the transaction commits, and one Courier call sends both.
first_send.php, then run it with your API key and email address:
courier-laravel-quickstart
The finished Laravel app from this guide, ready to clone and run.
Build this with a coding agent
To have a coding agent add Courier to your Laravel app, paste this prompt into Claude Code, Cursor, or Codex from the project root:npx skills add trycourier/courier-skills, and connect the Courier MCP server so the agent can check Courier Logs and templates itself.
Rules for AI agents
- Use
trycourier/courierand$client->send->message(message: [...], idempotencyKey: ...)with camelCase keys, such asuserID. - Read
COURIER_API_KEYthroughconfig('services.courier.key'), and callenv()only insideconfig/. - Return
CourierChannelfromvia()and build the message intoCourier(). - Implement
ShouldQueueand call$this->afterCommit()in the notification’s constructor. - Pass the notification’s
idasidempotencyKey, and returnnew CourierRetryPolicyfrommiddleware()to retry only what Courier runs again. - Never call Twilio or your mail provider directly, because Courier routes email and SMS.
What you’ll build
This Laravel app notifies a user through Courier when their order ships:- An email and a text from one send, once the shipment commits
- A
CourierChannelthat any Laravel notification can use - Queue retries that never send twice
Prerequisites
For the first send
- A Courier API key
- PHP 8.1 or newer and Composer
For the full build
- PHP 8.3 or newer
- A Laravel 12 or newer app
- A connected SMS provider, such as Twilio
Clone the finished Laravel app
Clone the Courier Laravel quickstart to run the finished app with your own key:Install the Courier PHP SDK in Laravel
The Courier PHP SDK istrycourier/courier. Laravel already includes Guzzle, its HTTP client, so install only the SDK:
courier entry to config/services.php:
config(), because php artisan config:cache stops Laravel loading .env. Add your key and address to .env, with an optional E.164 DEMO_PHONE:
Add a phone number and an orders table in Laravel
Courier’s SMS channel needs the Laravel user’s phone number. Runphp artisan make:model Order and php artisan make:migration create_orders_table, then match the quickstart’s migration:
database/migrations/*_create_orders_table.php
database/migrations/*_create_orders_table.php
php artisan migrate. Then in app/Models/User.php, add 'phone_number' to $fillable (#[Fillable] on Laravel 13) and these methods. Laravel channels look up the recipient with routeNotificationFor<Channel>().
app/Models/User.php (additions)
app/Models/User.php (additions)
Send email and SMS from a custom Laravel notification channel
A Laravel notification channel is a class with asend() method, and CourierChannel makes one Courier call to POST /send per notification. The PHP SDK takes camelCase keys, such as userID, and sends them as snake_case.
app/Notifications/Channels/CourierChannel.php
Limit Laravel queue retries to connection errors and rate limits
CourierRetryPolicy lets the Laravel queue retry only the errors Courier runs again for the same idempotency key, and fails the job on any other.
app/Notifications/Channels/CourierRetryPolicy.php
app/Notifications/Channels/CourierRetryPolicy.php
app/Notifications/Channels/CourierRetryPolicy.php
Queue the Laravel notification after the transaction commits
OrderShipped is a standard Laravel notification that via() routes to Courier through CourierChannel, and middleware() adds the retry policy.
app/Notifications/OrderShipped.php
Ship an order and notify the user in Laravel
markShipped() writes the status and calls notify() in one Laravel transaction, so a rolled-back shipment queues no Courier send.
app/Models/Order.php
$order->markShipped() from a controller or job, and run a worker for Laravel’s default database queue:
Verify
Ship one order and find it in Courier. In the cloned repo,php artisan courier:send does it on the sync queue and prints the request id:
php artisan queue:work, then run App\Models\Order::first()->markShipped() in php artisan tinker. The worker writes the request id to storage/logs/laravel.log. Open it in Courier Logs to see each channel and provider. A user with no phone number gets email only.
Go to production with Courier
Courier is notification infrastructure for humans and AI agents. Go live moves your Laravel app from Test to Production:- Switch keys. Set a Production
COURIER_API_KEYand connect your providers in that environment. - Keep a worker up. Run
queue:workunder Supervisor or Laravel Horizon, or queued notifications never send. - Honor opt-outs. Add
'preferences' => ['subscriptionTopicID' => ...]to the message so the hosted page’s choices apply.
FAQ
Should I use Laravel Notifications or Courier?
Should I use Laravel Notifications or Courier?
Laravel Notifications and Courier work together, because Courier is one more channel in
via(). Keep mail and database, and route SMS, failover, and hosted preferences through CourierChannel.How do I send SMS from Laravel?
How do I send SMS from Laravel?
Laravel sends SMS through Courier when the recipient has an E.164
phoneNumber and routing.channels includes sms. Connect an SMS provider such as Twilio in Courier first.Do I need a queue worker to send notifications from Laravel?
Do I need a queue worker to send notifications from Laravel?
Laravel runs queued Courier notifications only while
php artisan queue:work runs. With QUEUE_CONNECTION=sync, Laravel sends inline after the commit with no retry, so a failed send throws from markShipped() after the order is saved.How do I stop duplicate notifications when a queued job retries?
How do I stop duplicate notifications when a queued job retries?
Laravel queue retries can’t duplicate a Courier notification, because the notification’s
id is the idempotency key. CourierRetryPolicy fails the job on any error Courier would replay for that key.