Send a Link Button
In this code snippet you learn how to send a link style button on WhatsApp. This uses Vonage'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 |
|---|---|
VONAGE_APPLICATION_ID | The Vonage Application ID. |
VONAGE_APPLICATION_PRIVATE_KEY_PATH | Private key path. |
BASE_URL | For production use the base URL is |
MESSAGES_API_URL | There are two versions of the API, each with their own endpoints. For production the previous Messages API endpoint was |
WHATSAPP_NUMBER | The WhatsApp number that has been allocated to you by Vonage. For sandbox testing the number is 14157386102. |
VONAGE_WHATSAPP_NUMBER | Refer to |
VONAGE_NUMBER | Refer to |
TO_NUMBER | Replace with the number you are sending to. E.g. |
WHATSAPP_TEMPLATE_NAMESPACE | The namespace ID found in your WhatsApp Business Account. Only templates created in your own namespace will work. Using an template with a namespace outside of your own results in an error code 1022 being returned. |
WHATSAPP_TEMPLATE_NAME | The name of the template created in your WhatsApp Business Account. |
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
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:
curl -X POST $MESSAGES_API_URL \
-H 'Authorization: Bearer' $JWT \
-H 'Content-Type: application/json' \
-d '{
"message_type": "custom",
"to": "'$TO_NUMBER'",
"from": "'$WHATSAPP_NUMBER'",
"channel": "whatsapp",
"custom": {
"type": "template",
"template": {
"namespace": "'$WHATSAPP_TEMPLATE_NAMESPACE'",
"name": "'$WHATSAPP_TEMPLATE_NAME'",
"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"
}
]
}
]
}
}
}'
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
npm install @vonage/server-sdkCreate a file named send-link-button.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const { WhatsAppCustom } = require('@vonage/messages');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Write the code
Add the following to send-link-button.js:
vonage.messages.send(
new WhatsAppCustom({
custom: {
type: 'template',
template: {
namespace: WHATSAPP_TEMPLATE_NAMESPACE,
name: WHATSAPP_TEMPLATE_NAME,
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',
},
],
},
],
},
},
to: TO_NUMBER,
from: WHATSAPP_NUMBER,
}),
)
.then(({ messageUUID}) => console.log(messageUUID))
.catch((error) => console.error(error));
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:1.1.2'Create a file named SendWhatsappLinkButton and add the following code to the main method:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
}Write the code
Add the following to the main method of the SendWhatsappLinkButton file:
val messageId = client.messages.send(
whatsappCustom {
to(TO_NUMBER)
from(VONAGE_WHATSAPP_NUMBER)
custom(mapOf(
"type" to MessageType.TEMPLATE,
"template" to mapOf(
"namespace" to WHATSAPP_TEMPLATE_NAMESPACE,
"name" to WHATSAPP_TEMPLATE_NAME,
"language" to mapOf(
"code" to Locale.ENGLISH,
"policy" to Policy.DETERMINISTIC
),
"components" to listOf(
mapOf(
"type" to "header",
"parameters" to listOf(
mapOf(
"type" to MessageType.IMAGE,
"image" to mapOf(
"link" to HEADER_IMAGE_URL
)
)
)
),
mapOf(
"type" to "body",
"parameters" to listOf(
mapOf(
"type" to MessageType.TEXT,
"text" to "Anand"
),
mapOf(
"type" to MessageType.TEXT,
"text" to "Quest"
),
mapOf(
"type" to MessageType.TEXT,
"text" to "113-0921387"
),
mapOf(
"type" to MessageType.TEXT,
"text" to "23rd Nov 2019"
)
)
),
mapOf(
"type" to MessageType.BUTTON,
"index" to 0,
"sub_type" to "url",
"parameters" to listOf(
mapOf(
"type" to MessageType.TEXT,
"text" to "1Z999AA10123456784"
)
)
)
)
)
))
}
)Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.messages.whatsapp with the package containing SendWhatsappLinkButton:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:8.15.1'Create a file named SendWhatsappLinkButton and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the SendWhatsappLinkButton file:
var response = client.getMessagesClient().sendMessage(
WhatsappCustomRequest.builder()
.from(VONAGE_WHATSAPP_NUMBER).to(TO_NUMBER)
.custom(Map.of(
"type", "template",
"template", Map.of(
"namespace", WHATSAPP_TEMPLATE_NAMESPACE,
"name", WHATSAPP_TEMPLATE_NAME,
"language", Map.of(
"code", Locale.ENGLISH,
"policy", Policy.DETERMINISTIC
),
"components", List.of(
Map.of(
"type", "header",
"parameters", List.of(
Map.of(
"type", "image",
"image", Map.of(
"link", HEADER_IMAGE_URL
)
)
)
),
Map.of(
"type", "body",
"parameters", List.of(
Map.of(
"type", "text",
"text", "Anand"
),
Map.of(
"type", "text",
"text", "Quest"
),
Map.of(
"type", "text",
"text", "113-0921387"
),
Map.of(
"type", "text",
"text", "23rd Nov 2019"
)
)
),
Map.of(
"type", "button",
"index", "0",
"sub_type", "url",
"parameters", List.of(
Map.of(
"type", "text",
"text", "1Z999AA10123456784"
)
)
)
)
)
))
.build()
);
System.out.println("Message sent successfully. ID: "+response.getMessageUuid());Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.messages.whatsapp with the package containing SendWhatsappLinkButton:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Install-Package VonageWrite the code
Add the following to SendWhatsAppButtonLink.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(appId, privateKeyPath);
var vonageClient = new VonageClient(credentials);
var request = new WhatsAppCustomRequest
{
To = to,
From = brandName,
Custom = new Custom
{
type = "template",
template = new Template
{
@namespace = "whatsapp:hsm:technology:nexmo",
name = "verify",
language = new Language
{
code = "en",
policy = "deterministic"
},
components = new List<Component>
{
new()
{
type = "header",
parameters = new List<Parameter>
{
new()
{
type = "image",
image = new Image
{
link = "https://example.com/image.jpg"
}
}
}
},
new()
{
type = "body",
parameters = new List<Parameter>
{
new()
{
type = "text",
text = "Anand"
},
new()
{
type = "text",
text = "Quest"
},
new()
{
type = "text",
text = "113-0921387"
},
new()
{
type = "text",
text = "23rd Nov 2019"
}
}
},
new()
{
type = "button",
index = "0",
sub_type = "url",
parameters = new List<Parameter>
{
new()
{
type = "text",
text = "1Z999AA10123456784"
}
}
}
}
}
}
};
var response = await vonageClient.MessagesClient.SendAsync(request);Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
composer require vonage/clientCreate a file named send-button-link.php and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);Write the code
Add the following to send-button-link.php:
$custom = [
"type" => "template",
"template" => [
"namespace" => WHATSAPP_TEMPLATE_NAMESPACE,
"name" => WHATSAPP_TEMPLATE_NAME,
"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"],
],
],
],
],
];
$whatsApp = new \Vonage\Messages\Channel\WhatsApp\WhatsAppCustom(
TO_NUMBER,
FROM_NUMBER,
$custom
);
$client->messages()->send($whatsApp);Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
pip install vonageWrite the code
Add the following to send-button-link.py:
from vonage import Auth, Vonage
from vonage_messages.models import WhatsappCustom
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
)
message = WhatsappCustom(
to=TO_NUMBER,
from_=WHATSAPP_NUMBER,
custom={
"type": "template",
"template": {
"namespace": WHATSAPP_TEMPLATE_NAMESPACE,
"name": WHATSAPP_TEMPLATE_NAME,
"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"}],
},
],
},
},
)
response = client.messages.send(message)
print(response)Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
gem install vonageCreate a file named send-button-link.rb and add the following code:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)Write the code
Add the following to send-button-link.rb:
message = Vonage::Messaging::Message.whatsapp(
type: 'custom',
message: {
type: "template",
template: {
namespace: WHATSAPP_TEMPLATE_NAMESPACE,
name: WHATSAPP_TEMPLATE_NAME,
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"
}
]
}
]
}
}
)
client.messaging.send(
from: VONAGE_WHATSAPP_NUMBER,
to: TO_NUMBER,
**message
)Run your code
Save this file to your machine and run it:
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.