WhatsApp Business API | Integration for Magento

This document explains the process of integrating Magento2 with Tyntec’s whatsapp business api.


This document explains the process of integrating Magento2 with Tyntec’s whatsapp business api.


  • Tyntec’s whatsapp business api token.
  • Magento 2 installation.
  • Under Magento order’s shipping information or customer details, there should be a whatsapp phone number for the integration to work properly.
  • Basic knowledge of how to create a Magento2 module.

End Result

At the end of this document, you would be able to -

  • Send automated messages to your customers whenever they place an order from a magento store.

Let’s start the setup-

The setup is similar to creating a custom module in Magento 2 for calling a REST api. Here are the steps:-

  1. Create the module folder.
  2. Create the etc/module.xml file.
  3. Create the registration.php file.
  4. Create an event trigger file.
  5. Setup Observer file.
  6. Run the bin/magento setup:upgrade script to install the new module.
  7. Check that the module is working.

We will go through the module creation process step by step.

1. Create the Module folder

Let’s create the folder app/code/Tyntec and inside this folder place another folder: Whatsapp. If you’re using the command line, the code would be:

cd to the magento root folder
mkdir app/code/Tyntec
mkdir app/code/Tyntec/Whatsapp

2. Create etc/module.xml file

Using the following command-line code, create the folder app/code/Tyntec/Whatsapp/etc:

mkdir app/code/Tyntec/Whatsapp/etc

Then put the following module.xml file into it:

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Tyntec_Whatsapp" setup_version="0.0.1">

3. Create registration.php file

Create the file app/code/Tyntec/Whatsapp/registration.php. Then put the following content into it:

<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Tyntec_Whatsapp',

4. Create event trigger file

Events are dispatched by modules when certain actions are triggered. Magento 2 already has a lot of predefined events which can be used. In this tutorial, we are going to use one such event named as sales_order_place_after. This event is triggered when the order is placed by a customer on magento portal. You can try many other events as well.

Create the file app/code/etc/events.xml. Then put the following content into it:

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer name="Tyntec_Whatsapp_Order_place_after" instance="Tyntec\Whatsapp\Observer\Order_place_after" />

5. Create an Observer file

Observers are executed whenever the event they are configured to watch is dispatched by the event manager. In our case, the observer would be executed when the above configured event sales_order_place_after is triggered.

namespace Tyntec\Whatsapp\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
    class Order_place_after implements ObserverInterface
        protected $_logger;
         * @param \Psr\Log\LoggerInterface $_logger
        public function __construct(
            \Psr\Log\LoggerInterface $_logger
            $this->_logger = $_logger;
        public function execute(Observer $observer)
            $order = $observer->getEvent()->getOrder();
       		$total_order_amount= $order->getGrandTotal();
       		//Assuming that the user has entered a phone number with ISD code. 
       		$phone_withISDcode= $order->getShippingAddress()->getTelephone();
       		$whatsapp_message= "Thanks for placing an order. Your order number is ".$orderNumber.". Total order amount is USD".$total_order_amount;
            // Setup a curl request to send a message to whatsapp number using Tyntec api. 
            $curl = curl_init();
            $url= ""; 
            // This is a normal send api request. It would work only if the user has interacted with the number in the last 24 hours. Always use template api for sending these notifications. You just need to change the array below.
			$data_array= array (
			  'to' => $phone_withISDcode,
			  'channels' => 
			  array (
			    0 => 'whatsapp',
			  'whatsapp' => 
			  array (
			    'contentType' => 'text',
			    'text' => $whatsapp_message,
            //encode array into json format. 
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            // Add Options:
		    curl_setopt($curl, CURLOPT_URL, $url);
		    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		      'Content-Type: application/json',
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		    // EXECUTE the curl request:
		    $result = curl_exec($curl);
		    if(!$result){die("Connection Failure");}

6. Install the module

Running this command makes your new module active, notifying Magento of its presence.

$ php bin/magento setup:upgrade

7. Test the integration

Place an order from your magento store. You can even do that directly from the admin panel for test purposes. Do not forget to add the phone number.

You would receive a notification on the given whatsapp number immediately.

Now you can do different types of automation as well like sending a message when an order is shipped or delivered. You would simply need to use a different event trigger.