What are the steps to integrate Meta WhatsApp API with Laravel

Posted by

1. Create Your Meta App & WhatsApp Business Account

  • Go to the Meta for Developers portal.
  • Create a new app (choose “Business”).
  • In your app dashboard, add the WhatsApp product.
  • Complete setup: business verification, display name approval, and add your phone number (you’ll get a Phone Number ID and WABA ID).

2. Generate Access Token

  • In the Meta/WhatsApp dashboard, generate a permanent access token for your system user (required for API requests).
  • Never expose this token in your frontend.

3. Configure .env in Laravel

Add these to your .env:

textMETA_WA_TOKEN=your_long_live_access_token
META_WA_PHONE_ID=your_phone_number_id
META_WA_BASE=https://graph.facebook.com/v19.0/

4. Add Guzzle HTTP (if not already

textcomposer require guzzlehttp/guzzle

5. Create a Controller Method to Send WhatsApp Messages

phppublic function sendMetaWhatsApp($recipientPhone, $message)
{
    $endpoint = env('META_WA_BASE') . env('META_WA_PHONE_ID') . "/messages";

    $payload = [
        "messaging_product" => "whatsapp",
        "to" => $recipientPhone, // Format: countrycode+number, e.g. "911234567890"
        "type" => "text",
        "text" => ["body" => $message],
    ];

    $client = new \GuzzleHttp\Client();

    $response = $client->post($endpoint, [
        'headers' => [
            'Authorization' => 'Bearer ' . env('META_WA_TOKEN'),
            'Content-Type'  => 'application/json',
        ],
        'body' => json_encode($payload)
    ]);

    return json_decode($response->getBody(), true);
}

6. Set Up Your Route in routes/web.php

phpRoute::post('/whatsapp/send', 'WhatsAppController@send');

7. Example Controller Use

You can create a form or call the method directly for testing:

php// WhatsAppController.php
public function send(Request $request)
{
    $to = $request->input('phone');
    $msg = $request->input('message');
    $result = $this->sendMetaWhatsApp($to, $msg);

    return response()->json($result);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

0
Would love your thoughts, please comment.x
()
x