Send a Link Button

In this code snippet you learn how to send a link style button on WhatsApp. This uses Nexmo's custom object facility. You can reference the WhatsApp developer documentation for the specifics of the message format.

When the message recipient clicks on the link button, they will be prompted for permission to continue to the target link.

Example

Ensure the following variables are set to your required values using any convenient method:

Key Description
WHATSAPP_NUMBER The WhatsApp number that has been allocated to you by Nexmo.
TO_NUMBER The phone number you are sending the message to.
HEADER_IMAGE_URL The URL of the image to display in the template message header.

NOTE: Don't use a leading + or 00 when entering a phone number, start with the country code, for example, 447700900000.

Prerequisites

Create an application
If you do not have an application you can create one. Make sure you also configure your webhooks.

Write the code

Add the following to send-button-link.sh:

Copy to Clipboard
curl -X POST \
  https://api.nexmo.com/v0.1/messages \
  -H 'Authorization: Bearer' $JWT  \
  -H 'Content-Type: application/json' \
  -d '{
  "from": {
    "type": "whatsapp",
    "number": "'$WHATSAPP_NUMBER'"
  },
  "to": {
    "type": "whatsapp",
    "number": "'$TO_NUMBER'"
  },
  "message": {
    "content": {
      "type": "custom",
      "custom": {
        "type": "template",
        "template": {
          "namespace": "88b39973_f0d5_54e1_29cf_e80f1e3da4f2",
          "name": "oculus_shipment_update",
          "language": {
            "code": "en",
            "policy": "deterministic"
          },
          "components": [
            {
              "type": "header",
              "parameters": [
                {
                  "type": "image",
                  "image": {
                    "link": "'$HEADER_IMAGE_URL'"
                  }
                }
              ]
            },
            {
              "type": "body",
              "parameters": [
                {
                  "type": "text",
                  "text": "Anand"
                },
                {
                  "type": "text",
                  "text": "Quest"
                },
                {
                  "type": "text",
                  "text": "113-0921387"
                },
                {
                  "type": "text",
                  "text": "23rd Nov 2019"
                }
              ]
            },
            {
              "type": "button",
              "index": "0",
              "sub_type": "url",
              "parameters": [
                {
                  "type": "text",
                  "text": "1Z999AA10123456784"
                }
              ]
            }
          ]
        }
      }
    }
  }
}'

View full source

Run your code

Save this file to your machine and run it:

bash send-button-link.sh

Try it out

When you run the code a WhatsApp message containing a link button is sent to the recipient. In this example the button is a link to package tracking information.

Further information