Laravel / PHP PHP Free

Laravel: Send WhatsApp Messages via API

Send WhatsApp messages from Laravel through a provider API — order updates, OTPs and alerts in a few lines.

29 lines · Updated Nov 26, 2026

Preview of Laravel: Send WhatsApp Messages via API
laravel-send-whatsapp-message.php
<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class WhatsAppService
{
    public function send(string $to, string $message): bool
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer ' . config('services.whatsapp.token'),
        ])->post(config('services.whatsapp.endpoint'), [
            'to' => $to,
            'message' => $message,
        ]);

        return $response->successful();
    }
}

// config/services.php
// 'whatsapp' => [
//     'endpoint' => env('WHATSAPP_API_URL'),
//     'token' => env('WHATSAPP_API_TOKEN'),
// ],

// Usage:
// app(WhatsAppService::class)->send('923001234567', 'Your order is on the way!');

About this snippet

WhatsApp is where customers actually read messages — order confirmations, OTPs, delivery updates and appointment reminders get far more attention there than in email. This snippet sends a WhatsApp message from a Laravel application through a messaging API using Laravel's HTTP client, so you can notify customers straight from your code.

How it works

  1. Your API credentials (token and sender ID) are read from config/.env, never hard-coded.
  2. Laravel's HTTP client sends a POST request with the recipient's number and the message.
  3. The response is checked; failures are logged so you know when a message did not go out.

How to use it

  • Get API access from your provider — Meta's WhatsApp Cloud API or a WhatsApp Business Solution Provider — and verify your business number.
  • Store phone numbers in international format, e.g. 923001234567.
  • Send messages from a queued job, not during the web request, so a slow API never slows down checkout.

Rules you must know

  • Business-initiated messages outside a 24-hour customer-service window must use pre-approved message templates.
  • Only message people who opted in. Unsolicited messages can get your number restricted.
  • Unofficial "WhatsApp Web" automation tools break WhatsApp's terms and risk a ban — use an official API.

Common problems

  • 401 / invalid token: the access token has expired; use a permanent system-user token.
  • Message not delivered: the template is not approved, or the number format is wrong.

FAQ

Is it free? Providers charge per conversation or message, with prices varying by country and message category. Check your provider's current pricing.

Related: Delayed queued job with retries · What is Laravel Livewire?

How to install

Save the class as app/Services/WhatsAppService.php, add the config block to config/services.php, and set WHATSAPP_API_URL and WHATSAPP_API_TOKEN in .env. Replace the endpoint payload with your provider's format.

Share this snippet

See all
Chat with us