Paramclasses Academy - Blog

Tutorials Details

←
Post 14 of 20
→

How to Integrate WhatsApp API with a Website Using Webhooks

How to Integrate WhatsApp API with a Website Using Webhooks

November 27, 2026

How to Integrate WhatsApp API with a Website Using Webhooks

Introduction

Imagine a customer visits your website and sends a WhatsApp message asking about a course, product, service or order. Your business wants to receive that message directly inside its website dashboard so that the team can respond quickly.

How can your website know when a new WhatsApp message arrives?

This is where WhatsApp Webhooks become useful.

WhatsApp Cloud API allows a website or application to send messages through Meta's official messaging platform. Webhooks work in the opposite direction: they notify your application when events happen, such as an incoming customer message or a change in message delivery status.

In this beginner-friendly tutorial by Param Classes, we will understand webhooks, prepare a website endpoint, configure Meta's webhook settings, write a basic PHP example, receive WhatsApp events and troubleshoot common integration problems.

1. What Are WhatsApp Webhooks?

A webhook is an automated notification sent from one application to another when a particular event occurs.

Instead of repeatedly asking an API whether something has changed, your application receives a notification when the subscribed event happens.

For WhatsApp Business Platform, webhooks can help your application receive information about:

  • Incoming WhatsApp messages.

  • Message sent status.

  • Message delivery status.

  • Message read status, where available.

  • Failed message status.

  • Other supported WhatsApp Business Account events.

Example:

Suppose a customer sends this message to your business:

"Hello, I want to know more about your Web Development course."

Meta sends a webhook notification to your configured website endpoint. Your backend receives the event, extracts the message details and can store them in a database or display them in an admin dashboard.

2. WhatsApp Cloud API vs Webhooks

These two technologies work together, but they perform different tasks.

Feature WhatsApp Cloud API WhatsApp Webhooks
Main purpose Send messages and perform supported API actions Receive event notifications
Communication Your application sends requests to Meta Meta sends notifications to your endpoint
Example Send an order confirmation Receive a customer reply
HTTP method Commonly POST for sending messages GET for verification and POST for events
Business use Outgoing communication Incoming messages and status tracking

Simple example:

Website → Cloud API → Send WhatsApp message

WhatsApp Platform → Webhook → Website receives customer reply

Both are important when building a two-way WhatsApp integration.

3. Requirements Before Integrating Webhooks

Prepare the following items before starting:

1. Meta Developer Application

You need an application configured with the WhatsApp product.

2. WhatsApp Business Account

Your WhatsApp Business Account must be connected to the application and configured for the required messaging functionality.

3. WhatsApp Phone Number

Use a configured test number for initial development or an eligible business number for production.

4. Website Backend

Your website needs a backend that can receive and process HTTP requests. In this example, we will use PHP.

5. Public HTTPS URL

Meta must be able to reach your webhook endpoint over HTTPS. A localhost URL is not directly accessible to Meta.

6. Verify Token

Create a secret string that your application will use to verify Meta's initial webhook verification request.

7. App Secret

Your Meta application secret is used when validating webhook request signatures.

4. Understand the Webhook Workflow

Before writing code, understand how the complete integration works.

  1. A developer creates a webhook endpoint on the website.

  2. The developer enters the callback URL and verify token in Meta's webhook configuration.

  3. Meta sends a GET verification request.

  4. The website checks the verify token and returns the challenge value.

  5. Meta verifies the callback endpoint.

  6. The application subscribes to the relevant WhatsApp Business Account webhook events.

  7. When a subscribed event occurs, Meta sends a POST request containing JSON data.

  8. The website validates the notification and processes the event.

Example workflow:

Customer sends WhatsApp message → Meta processes the message → Meta sends webhook notification → Website receives JSON data → Backend stores the message → Admin dashboard displays the conversation.

5. Create a Webhook Endpoint on Your Website

First, create a backend endpoint that Meta can access.

For a simple PHP project, you can create a file named:

webhook.php

For a framework-based website, create an equivalent public route and controller method.

Your callback URL might look like:

https://yourdomain.com/webhook.php

For a CodeIgniter or Laravel project, use the public route URL configured for your webhook controller instead.

Important: Your endpoint must be publicly accessible and use a valid HTTPS certificate. Meta cannot directly send notifications to a normal local address such as http://localhost:8000.

During local development, you can use a secure HTTPS tunnelling service to expose your local endpoint temporarily. For production, use your real website domain.

6. Implement Webhook Verification Using PHP

When you configure the callback URL in Meta, Meta sends a GET request containing three important parameters:

  • hub.mode

  • hub.verify_token

  • hub.challenge

Your website must compare the received verify token with the secret value you configured. If they match, return the challenge value.

Here is a basic PHP example:

<?php

$verifyToken = getenv('WHATSAPP_VERIFY_TOKEN');

if ($_SERVER['REQUEST_METHOD'] === 'GET') {

    $mode = $_GET['hub_mode'] ?? '';
    $token = $_GET['hub_verify_token'] ?? '';
    $challenge = $_GET['hub_challenge'] ?? '';

    if (
        $mode === 'subscribe' &&
        $verifyToken !== false &&
        hash_equals($verifyToken, $token)
    ) {
        http_response_code(200);
        echo $challenge;
        exit;
    }

    http_response_code(403);
    echo 'Verification failed';
    exit;
}

How does this code work?

  1. It reads the configured verification token from an environment variable.

  2. It checks whether the incoming request uses GET.

  3. It reads the verification parameters.

  4. It compares the received token with your configured token.

  5. If the token matches, it returns the challenge.

  6. Otherwise, it returns a forbidden response.

PHP commonly converts periods in incoming query parameter names into underscores when accessing them through $_GET. That is why the example uses hub_mode, hub_verify_token and hub_challenge.

Never use a publicly known or easily guessed verify token.

7. Handle Incoming WhatsApp Webhook Events

After successful verification, Meta can send POST requests to your endpoint when subscribed events occur.

A simplified incoming message notification may contain a structure similar to this:

{
  "object": "whatsapp_business_account",
  "entry": [
    {
      "changes": [
        {
          "field": "messages",
          "value": {
            "messages": [
              {
                "from": "919876543210",
                "id": "wamid.EXAMPLE_MESSAGE_ID",
                "timestamp": "1790000000",
                "type": "text",
                "text": {
                  "body": "Hello, I need course details."
                }
              }
            ]
          }
        }
      ]
    }
  ]
}

This is an illustrative payload. Actual notifications can contain different fields depending on the event.

You can process incoming POST requests using PHP:

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $rawBody = file_get_contents('php://input');

    $data = json_decode($rawBody, true);

    if (!is_array($data)) {
        http_response_code(400);
        exit('Invalid JSON');
    }

    $entries = $data['entry'] ?? [];

    foreach ($entries as $entry) {

        foreach ($entry['changes'] ?? [] as $change) {

            if (($change['field'] ?? '') !== 'messages') {
                continue;
            }

            $value = $change['value'] ?? [];

            foreach ($value['messages'] ?? [] as $message) {

                $sender = $message['from'] ?? '';
                $messageType = $message['type'] ?? '';

                $messageText = '';

                if ($messageType === 'text') {
                    $messageText =
                        $message['text']['body'] ?? '';
                }

                // Store the event securely in your database.
                // Process different message types separately.
            }
        }
    }

    http_response_code(200);
    echo 'EVENT_RECEIVED';
    exit;
}

This example demonstrates basic JSON processing. A production application should also validate the request signature, handle different message types, store events safely and implement duplicate-event protection.

8. Configure WhatsApp Webhooks in Meta Developer Dashboard

After creating your endpoint, configure it in Meta.

Step 1: Open Meta for Developers

Visit:

https://developers.facebook.com/

Log in and open your developer application.

Step 2: Open WhatsApp Configuration

Navigate to the WhatsApp product and locate its configuration or webhook settings.

Step 3: Enter Callback URL

Enter your public HTTPS endpoint.

Example:

https://yourdomain.com/webhook.php

Step 4: Enter Verify Token

Enter the same verify token that your PHP application uses.

Step 5: Verify and Save

Meta sends the verification request. If your endpoint returns the expected challenge, the callback verification succeeds.

Step 6: Subscribe to Relevant Fields

Subscribe to the WhatsApp Business Account webhook fields needed by your application, especially the messages field when you want incoming message and message-status notifications.

Follow the current Meta dashboard instructions to complete the WhatsApp Business Account subscription.

9. Understand Message Status Notifications

Webhooks are not limited to incoming customer messages. They also help businesses track the progress of outgoing messages.

Common message status values include:

Sent: The message has been accepted for sending.

Delivered: The message has reached the recipient's device.

Read: The recipient has opened the message, when read receipts are available.

Failed: The message could not be delivered.

For example, an online store can use these status events to update its customer communication dashboard.

Remember that receiving a successful response from the send-message API and receiving a delivery confirmation through a webhook are two different events.

10. Store WhatsApp Messages in a Website Database

For a practical business application, you may want to store incoming messages and status events in a database.

A simple message table could contain:

Column Purpose
id Unique database record
sender_number Customer WhatsApp number
message_id WhatsApp message identifier
message_type Text, image or another supported type
message_body Extracted message content
received_at Event timestamp
status Processing or message status

For a real application, design your database according to your messaging workflow.

Use the WhatsApp message ID or another suitable event identifier to prevent duplicate records. Meta may retry webhook notifications when your endpoint does not acknowledge them successfully, so duplicate handling is important.

Also, avoid storing unnecessary personal information and protect customer data with suitable access controls.

11. Secure Your WhatsApp Webhook Integration

Security is essential because webhook endpoints receive business and customer information.

Follow these practices:

1. Use HTTPS

Your production callback URL must use a valid TLS/SSL certificate.

2. Validate the Verify Token

Do not accept an arbitrary verification request.

3. Validate the Request Signature

Meta includes an X-Hub-Signature-256 header with event notifications. Validate the SHA-256 HMAC signature using the raw request body and your Meta App Secret before trusting the payload.

4. Protect Credentials

Store the App Secret, access tokens and verify token in environment variables or a secure secrets manager.

5. Prevent Duplicate Processing

Use message IDs and event identifiers to avoid processing the same notification multiple times.

6. Respond Quickly

Acknowledge valid webhook notifications with HTTP 200 after safely accepting the event. For longer tasks, place the event in a queue and process it asynchronously.

7. Validate Incoming Data

Do not trust webhook content blindly. Validate fields before saving data or triggering business actions.

12. Common Webhook Errors and Solutions

1. Callback URL Verification Failed

Possible reasons:

  • Incorrect callback URL.

  • Verify token mismatch.

  • Challenge value not returned correctly.

Solution: Check the endpoint and confirm that the verify token matches exactly.

2. Meta Cannot Reach Your Endpoint

Possible reasons:

  • Website is offline.

  • HTTPS certificate is invalid.

  • Endpoint is blocked by server configuration.

  • URL is not publicly accessible.

Solution: Test your endpoint from outside your local network and check server SSL and firewall settings.

3. Webhook Events Are Not Received

Possible reasons:

  • Required webhook field is not subscribed.

  • WhatsApp Business Account is not subscribed to the application.

  • App or business configuration is incomplete.

  • The endpoint is returning errors.

Solution: Review the WhatsApp webhook configuration and inspect your server logs.

4. Duplicate Messages Appear

Possible reason: Meta may retry notifications when acknowledgement fails or is delayed.

Solution: Store and check message IDs before inserting or processing an event again.

5. JSON Data Is Empty

Possible reason: The endpoint is not reading the raw request body correctly.

Solution: Use file_get_contents('php://input') and inspect the incoming request safely.

13. Real-World Example: Website Customer Support System

Imagine Param Classes wants to connect its website with WhatsApp so that students can ask questions about courses.

The workflow can operate like this:

  1. A student sends a WhatsApp message asking about a course.

  2. Meta sends the incoming message event to the website webhook.

  3. The PHP backend validates the notification.

  4. The application stores the message in its database.

  5. The admin dashboard displays the student's message.

  6. A team member prepares a response.

  7. The backend sends the reply through WhatsApp Cloud API.

  8. Delivery and read-status updates are received through webhooks.

This system can help businesses manage customer enquiries, course registrations, order updates and support conversations from one application.

14. Frequently Asked Questions (FAQs)

Q1. What is a WhatsApp webhook?

A WhatsApp webhook is an HTTPS callback endpoint that receives event notifications from Meta, such as incoming messages and message-status updates.

Q2. Is WhatsApp Cloud API required for webhooks?

WhatsApp webhooks are part of the WhatsApp Business Platform integration. You need the appropriate WhatsApp application and account configuration to receive the relevant events.

Q3. Can I use PHP to integrate WhatsApp webhooks?

Yes. PHP can receive webhook requests, verify callback requests, parse JSON payloads and store message information in a database.

Q4. Can I use localhost for WhatsApp webhooks?

Meta cannot directly access a normal localhost address. Use a publicly reachable HTTPS endpoint or a secure development tunnel for testing.

Q5. What is a webhook verify token?

It is a secret string that your application compares during Meta's initial callback verification request. It is different from your WhatsApp API access token.

Q6. Why should I validate X-Hub-Signature-256?

It helps your application verify that an event notification was signed using your Meta App Secret and has not been modified in transit.

Q7. Can webhooks receive customer replies?

Yes. After the required webhook configuration and subscriptions are completed, your application can receive incoming message notifications.

Conclusion

WhatsApp webhooks make website integration more powerful by allowing applications to receive incoming messages and message-status notifications automatically.

In this tutorial, we learned what webhooks are, how they work with WhatsApp Cloud API, how to create a PHP endpoint, verify the callback URL, process incoming JSON notifications, configure Meta's webhook settings, store message data and secure the integration.

At Param Classes, we encourage learners to practise these concepts by building a small website integration first. Once the basic webhook is working, you can extend it into a complete customer support dashboard, CRM integration or automated messaging system.

←
Post 14 of 20
→