Receiving messages via Webhook

This documentation describes how to setup webhooks for the WhatsApp Business API.

Introduction

Webhooks is a mechanism which enables applications to communicate with each other programmatically. It allows you to send real-time data from one application to another whenever a given event occurs. You can use the webhook to determine which endpoint we should forward the real-time data (aka Notifications). Whenever a specific event occurs, the WhatsApp Business API Client sees the event, collects the data, and immediately sends a notification (user-defined HTTP callbacks) to the webhook URL specified.

There are 3 main objects you can receive via webhooks:

  • messages: Used to notify you when you get a new message and what is in the new message.

  • statuses: Used to notify you when there's a status change in a message you sent.

  • errors: When there are any out-of-band errors that occur in the normal operation of the application, this array provides a description of the error.

It is essential that your Webhook URL returns an HTTPS 200 OK response to notifications. Otherwise, the WhatsApp Business API client considers that notification as failed and tries again after a delay.

To deploy a live webhook that can receive events from the WhatsApp Business API client, your code must have HTTPS support and a valid SSL certificate.

All incoming messages will automatically show as delivered (two grey ticks). To make them appear as read (two colored ticks), you have to mark the messages as read.

Recommendations

For stable functioning of WABA it is recommended make the webhook performance as fast as possible. That means:

  • Design your service to respond as quickly, and as close to your network speed as possible.

  • Respond with a 200 status code immediately after receiving a notification and storing it. The callback's payload should not be processed before responding as this can lead to unacceptable delays; instead, send the response first then (asynchronously) process the payload.

  • Reduce network latency by setting up your webhook server closer to 360dialog's datacenters (Central and Eastern Europe).

  • Sending one message as opposed to getting one causes up to three callback notifications (sent, delivered and read). It means that the speed of processing concurrent requests should increases in accordance with the load of the number. In addition to a direct increase in the speed of processing notifications, we recommend to make your webhook process as many parallel requests as possible. Otherwise, this can lead to the occurrence and overflow of the message queue.

Webhook URL

The webhook URL is a resource address to which the WhatsApp Servers sends notifications, which are triggered by specific events. A suitable webhook URL must be supplied by you or by your Partner Software Provider / ISV.

You only need to set your Webhook URL once. Although you can update it or remove it at any time.

You must set a Webhook URL once per number.

If you generate a new API-KEY, the webhook URL for that number will be removed. So you must reset it using the new API-KEY.

If the WABA is registered in On-premise API

If you are using On-Premise hosting, use the base-url waba.360dialog.io.

If the WABA is registered in Cloud API

If you are using Cloud API hosting, you must use the base-url waba-v2.360dialog.io

Schema

Set Webhook URL

POST https://[base-url]/v1/configs/webhook

Send a POST request and store the URL from the response.

Headers

Name
Type
Description

Content-Type

string

application/json

D360-API-KEY

string

Request Body

Name
Type
Description

URL

string

www.example.com/webhook

{
  "url": URL,
  "headers": {
    "header_1": string,
    "header_2": string
  }
}
{
  "url": URL,
  "headers": {
    "header_1": string,
    "header_2": string
  }
}

Example setting webhook with Basic Auth:

If the webhook URL needs to be authorized by user, USER and PASS should be provided in the header Authorization that contains Basic base64(USER:PASS).

Request body example for USER=testuser and PASS=testpass

{
  "url": "https://www.example.com/webhook",
  "headers": {
    "Authorization": "Basic dGVzdHVzZXI6dGVzdHBhc3M="
  }
}

Get Webhook URL

If the webhook URL is already set, make a GET request to retrieve the existing resource.

Schema

Retrieve existing webhook URL

GET https://[base-url]/v1/configs/webhook

Path Parameters

Name
Type
Description

Content-Type

string

application/json

D360-API-Key

string

unique API-Key

{
  "url": URL,
  "headers": {
    "header_1": string,
    "header_2": string
  }
}

Example:

D360-API-KEY: adafABC43
Content-Type: application/json

Receiving notifications for incoming messages

When a customer sends you a message, the WhatsApp Business API client will send an HTTP POST request notification to the webhook URL with the details that are described in the following documents:

Make sure to check out the Best Practices docs for more information.

Detailed components

Via WABA

After a Webhook is set, you will be able to receive the following events:

  • phone_number_name_update"

  • "phone_number_quality_update"

  • "account_update"

  • "account_review_update"

  • "message_template_status_update"

  • "business_capability_update"

  • "template_category_update"

  • "payment" (if eligible)

Please see more information about them here.

Error scenarios

  • 302-redirect: Redirects are not followed by the webhook gateway Please ensure that there is always HTTPS 200 as a response code.

  • SSL-errors: Please ensure that the callback-URLs are using SSL/TLS, and that the certificates are valid and that the certificate chain is complete.

  • You start processing in the callback thread. Please do only acknowledge messages and notifications and start processing in a separate thread.

  • Callbacks will be terminated after a defined timeout and there will be a retry.

  • Duplicates messages can be sent to a WhatsApp webhook as the only guarantee provided is that messages will be received at least once (as opposed to exactly once). If this is affecting how messages are processed on your end, then we suggest deduping webhook messages based on the message_id.

Last updated

Was this helpful?

Revision created