laravel-whatsapp-cloud-api maintained by ahmednour
Laravel WhatsApp Cloud API
A Laravel client for Meta's WhatsApp Cloud API — messages, templates, media, phone numbers, flows, QR codes, analytics, commerce settings and webhook verification, mirroring the official "WhatsApp Cloud API" Postman collection.
Extracted from a production Laravel app where it drives templated notifications, action-button delivery, and webhook-driven template status sync — kept generic here so it can be reused across projects instead of copy-pasted.
Requirements
- PHP 8.2+ (8.3+ for Laravel 13)
- Laravel 12 or 13
Installation
composer require ahmednour/laravel-whatsapp-cloud-api
The service provider is registered automatically through Laravel package discovery.
Publish the config file:
php artisan vendor:publish --tag=whatsapp-cloud-api-config
Set the environment variables:
WHATSAPP_PHONE_NUMBER_ID=
WHATSAPP_BUSINESS_ACCOUNT_ID=
WHATSAPP_API_TOKEN=
WHATSAPP_VERIFY_TOKEN=
WHATSAPP_APP_SECRET=
WHATSAPP_API_URL=https://graph.facebook.com/v21.0
Usage
Resolve WhatsAppService from the container — it composes one domain service per API surface:
use AhmedNour\WhatsAppCloudApi\WhatsAppService;
class NotifyController
{
public function __construct(private WhatsAppService $whatsApp) {}
public function send()
{
$result = $this->whatsApp->messages->sendTextMessage('15551234567', 'Hello!');
// $result === ['success' => true, 'message_id' => 'wamid...', 'error' => null]
}
}
Every call returns a normalized array — {success, data, error, status} for most endpoints, with a
few send-oriented shortcuts ({success, message_id, error}, {success, media_id, error}) for the
most common calls.
Domain services
| Property | Service | Covers |
|---|---|---|
$messages |
MessageService |
Text/media/interactive/template/reaction messages, read receipts, typing indicator |
$templates |
TemplateService |
Template CRUD, namespace, status, Meta error classification |
$media |
MediaService |
Upload, retrieve URL, download, delete |
$phoneNumbers |
PhoneNumberService |
Discovery, registration, verification |
$account |
AccountService |
WABA, business portfolios, app subscription, billing, token debug |
$businessProfile |
BusinessProfileService |
Business profile fields, resumable uploads |
$qrCodes |
QrCodeService |
Message QR code (click-to-chat) management |
$analytics |
AnalyticsService |
Messaging and conversation analytics |
$blockUsers |
BlockUserService |
Blocked user management |
$commerce |
CommerceService |
Cart and catalog visibility settings |
$flows |
FlowService |
WhatsApp Flows, assets, publishing, endpoint encryption |
Each is also independently injectable — app(MessageService::class) — if you don't need the full
aggregate.
Sending messages
$whatsApp->messages->sendTextMessage('15551234567', 'Your order shipped!');
$whatsApp->messages->sendImageMessageByUrl('15551234567', 'https://example.com/receipt.png');
$whatsApp->messages->sendTemplateMessage('15551234567', 'order_update', 'en', [
['type' => 'body', 'parameters' => [['type' => 'text', 'text' => 'John']]],
]);
Templates
$whatsApp->templates->createTemplate('order_ready', 'en', 'UTILITY', [
['type' => 'BODY', 'text' => 'Hi {{1}}, your order is ready.'],
]);
$whatsApp->getTemplateStatus('meta-template-id'); // ['success' => true, 'status' => 'approved', ...]
TemplateService also exposes Meta's template-submission error classifiers
(isDuplicateLanguageError, isLanguageDeletionError, isCategoryMismatchError,
nextTemplateName) so an app building its own submit-with-versioned-retry workflow on top of
createTemplate/editTemplate doesn't have to reimplement Meta's subcode handling.
Webhooks
WebhookVerifier validates both parts of the Meta webhook contract — the one-time GET handshake and
the X-Hub-Signature-256 header on every event delivery — without any framework request coupling:
use AhmedNour\WhatsAppCloudApi\WebhookVerifier;
class WhatsAppWebhookController
{
public function __construct(private WebhookVerifier $verifier) {}
public function verify(Request $request)
{
if (! $this->verifier->isValidSubscriptionRequest(
$request->query('hub_mode'),
$request->query('hub_verify_token'),
)) {
return response('Forbidden', 403);
}
return response($request->query('hub_challenge'), 200);
}
public function callback(Request $request)
{
if (! $this->verifier->hasValidSignature($request->getContent(), $request->header('X-Hub-Signature-256'))) {
return response()->json(['status' => 'forbidden'], 403);
}
// ...
}
}
hasValidSignature fails closed — it returns false (not true) when no app secret is configured,
so an unconfigured webhook can never be spoofed by omission.
Testing
composer test
composer lint
Changelog
See CHANGELOG.md for what has changed between releases.
Contributing
Issues and pull requests are welcome at
ahmed-nour-dev/whatsappi. Please run
composer test and composer lint before opening a pull request.
License
The MIT License (MIT). See LICENSE for details.