Download the PHP package beziworld/laravel-twilio without Composer
On this page you can find all versions of the php package beziworld/laravel-twilio. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download beziworld/laravel-twilio
More information about beziworld/laravel-twilio
Files in beziworld/laravel-twilio
Package laravel-twilio
Short Description Lightweight Laravel integration for Twilio: SMS, WhatsApp, Verify, Lookup, signed webhooks, events, notification channel and multiple accounts.
License MIT
Homepage https://github.com/beziworld/laravel-twilio
Informations about the package laravel-twilio
Laravel Twilio
Lightweight Laravel integration for Twilio: SMS, MMS and WhatsApp (including content templates and scheduling), Verify, Lookup, basic voice calls, signed webhooks dispatched as Laravel events, a notification channel and multiple accounts.
Polska wersja dokumentacji
Why this package
The official twilio/sdk covers every Twilio product, and
laravel-notification-channels/twilio adds a notification
channel on top of it. This package takes a different approach:
- No
twilio/sdkdependency. Requests go through the Laravel HTTP client, soHttp::fake()works in your tests and there is no large generated SDK to install. - One package for messaging, WhatsApp templates, Verify, Lookup, voice and account balance.
- Webhooks included: routes for status callbacks and incoming messages, X-Twilio-Signature validation (form and
JSON bodies, proxies, configurable public URL),
MessageStatusUpdatedandMessageReceivedevents and TwiML replies. - Multiple accounts and subaccounts as named connections, plus runtime credentials for multi-tenant apps; webhook signatures are checked with the Auth Token of the account that sent them.
- Typed, immutable responses, one exception hierarchy, PHP 8.3+, Laravel 12 and 13, PHPStan level 8.
Installation
Keep TWILIO_AUTH_TOKEN set even when you use an API key: Twilio signs webhooks with the Auth Token.
SMS and MMS
with('ShortenUrls', true) passes any other Message parameter. Fetch, list and manage messages:
Scheduling
Scheduled messages require a Messaging Service and must be 15 minutes to 35 days ahead:
Recipients and senders receive the whatsapp: prefix automatically.
Verify
verifyCode() returns false for a wrong code and for verifications that expired, were already approved or ran out of
attempts (Twilio answers those with HTTP 404). Pass serviceSid: to use another Verify service.
Lookup
Voice and balance
Any other endpoint is available through the generic request helper, authenticated with the connection credentials:
Webhooks
-
Enable the routes:
Route Name Purpose POST /twilio/webhooks/statustwilio.webhooks.statusMessage status callbacks GET\|POST /twilio/webhooks/incomingtwilio.webhooks.incomingIncoming SMS and WhatsApp messages -
Point Twilio at them: the status URL as
StatusCallback(per message orTWILIO_STATUS_CALLBACK) or on the Messaging Service, the incoming URL as "A message comes in" on the number, the WhatsApp sender or the Messaging Service. - Listen to the events:
Without a reply Twilio receives an empty <Response/> and sends nothing. For more control build the TwiML yourself:
$event->response()->message('Hi', ['https://example.com/image.jpg'])->redirect('https://example.com/next').
MessagingResponse can also be returned from your own controllers (it is Responsable).
Signature validation
Every webhook request must carry a valid X-Twilio-Signature: an HMAC-SHA1 of the full URL followed by the sorted POST
parameters, keyed with the Auth Token. The middleware:
- picks the Auth Token by the
AccountSidparameter, so each configured connection (and each tenant registered withresolveAuthTokenUsing()) is validated with its own token and the event carries the connection name; - validates form-encoded POSTs, GET requests (full URL with query) and JSON bodies signed with
bodySHA256; - accepts the URL with and without the default port, as the official SDKs do;
- uses the public origin from
TWILIO_WEBHOOK_URLwhen set, otherwise the request as seen by Laravel (configure trusted proxies behind a load balancer). The raw path and query string are kept, because any re-encoding breaks the signature.
Protect your own Twilio routes, for example voice webhooks, with the same middleware:
The validator can be used directly as well:
(new SignatureValidator($authToken))->validate($signature, $url, $request->post()).
TWILIO_VALIDATE_SIGNATURE=false disables validation; use it only locally.
Status callbacks may arrive out of order and more than once, so keep listeners idempotent.
Notification channel
The recipient is taken from the notifiable:
On-demand: Notification::route('twilio', '+48500100200')->notify(new OrderShipped($order));
A plain string becomes an SMS. Messages may set their own recipient with ->to() and another account with
->connection('marketing'). A notifiable without a phone number is skipped silently.
Multiple accounts
Define connections in config/twilio.php, like database connections:
Options outside connections (status_callback, timeout, retries, base_urls) are shared and can be overridden
per connection.
Error handling
All exceptions extend Beziworld\Twilio\Exceptions\TwilioException:
| Exception | When |
|---|---|
ApiException |
Twilio rejected the request (errorCode(), httpStatus(), moreInfo(), payload(), isRateLimited(), isNotFound(), isAuthenticationError()) or could not be reached |
InvalidMessage |
Message without recipient, content or sender, template combined with body, scheduling without a Messaging Service |
InvalidConfiguration |
Missing Account SID or credentials, unknown connection, missing Verify service |
CouldNotSendNotification |
Notification without a valid toTwilio() |
Retries
Requests rejected with HTTP 429 (error 20429) were not processed by Twilio and are retried TWILIO_RETRIES times (default
2) with exponential backoff. Connection errors are retried only for reads: a message request that timed out may still
have been accepted, and repeating it could send the message twice.
Testing your application
Development
Author
Created and maintained by Kamil Ćwiertnia, beziworld.eu.
- E-mail: [email protected]
- Issues and feature requests: GitHub issues
- Security vulnerabilities: see SECURITY.md
Need a custom integration or a Laravel project? Get in touch through beziworld.eu.
Other packages by the author
- beziworld/laravel-smsapi: SMSAPI SMS gateway
- beziworld/laravel-przelewy24: Przelewy24 payments
- beziworld/laravel-tpay: Tpay payments and BLIK
License
MIT. See LICENSE.md. This package is not affiliated with, endorsed by or sponsored by Twilio Inc. "Twilio" and "WhatsApp" are trademarks of their respective owners.
All versions of laravel-twilio with dependencies
ext-json Version *
illuminate/notifications Version ^12.0|^13.0
guzzlehttp/guzzle Version ^7.8
illuminate/contracts Version ^12.0|^13.0
illuminate/http Version ^12.0|^13.0
illuminate/routing Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0