Skip to main content

Send WhatsApp message

A WhatsApp message goes out in one of two forms:

  • an approved template — at any time. Templates are created in your cabinet and approved by Meta; what each of them needs besides its values is returned by List WhatsApp templates;
  • a free-form message — text, a file, a location or a card with a button. WhatsApp delivers it only within 24 hours after the subscriber's last message to you.

The request is synchronous at /api/json.php: the gateway answers with msg_id for each message.

The same request can be queued asynchronously at /v1/json.

warning
  • The recipient has to write to you first or agree to receive messages. A cold mailing to a purchased base is not possible in WhatsApp.
  • The sender (whatsapp_signature) has to be connected to your account, and Meta has to approve its display name.
  • Outside the 24-hour window only an approved template is accepted.
URI: https://blackbox.business/api/json.php

All requests to API are sent in JSON format using the POST method.

Header parameters​

Requests must contain header Content-Type: application/json, otherwise, the request will be considered invalid even if it has valid JSON.

Request parameters​

authstringrequired
Your API key, which can be obtained in your personal cabinet
datalist[object]required
List of objects with request parameters
typestringrequired
Request type: whatsapp, or whatsapp+sms to send an SMS when the WhatsApp message is not delivered
idnumberrequired
Unique message identifier in the client system
phonenumberrequired
Phone number of the recipient, digits only, in international format
whatsapp_signaturestringrequired
Sender name connected to WhatsApp in your account
whatsapp_templatestring
Name of an approved template: lowercase latin letters, digits and _
Required outside the 24-hour window
whatsapp_languagestring
Template language code, for example en. Without it the approved language of the template is used
whatsapp_varlist[string]
Template values in order: the first one replaces {{1}}, the second {{2}} and so on
Their number must equal params of the template, each value up to 1024 characters
whatsapp_messagestring
Free-form text, up to 4096 characters. Delivered only within the 24-hour window
With a file it becomes the caption, up to 1024 characters
whatsapp_imagestring
Link to an image (JPG, PNG, up to 5 MB): the image header of the template or a free-form photo
whatsapp_videostring
Link to a video (MP4, up to 16 MB): the video header of the template or a free-form video
whatsapp_documentstring
Link to a document (PDF, up to 100 MB): the document header of the template or a free-form file
whatsapp_audiostring
Link to an audio file. Free-form messages only, without a caption
whatsapp_filenamestring
File name the recipient sees for a document
whatsapp_headerstring
Value of {{1}} in a text header of the template. Only for a header with a placeholder
whatsapp_locationobject
Location: the location header of the template or a free-form location message
latitudenumberrequired
Latitude, from -90 to 90
longitudenumberrequired
Longitude, from -180 to 180
namestring
Name of the place
addressstring
Address
whatsapp_button_varstring
End of the link of the template button with {{1}}, for example an order number
whatsapp_couponstring
Coupon code for the copy-code button of the template, up to 15 characters
whatsapp_codestring
One-time code of an authentication template, up to 15 characters. It fills both the text and the copy button
It can also be passed as the only value of whatsapp_var
whatsapp_linkstring
Free-form card: the link of the button
Works only together with whatsapp_button: one without the other is ignored and the text goes out as usual
whatsapp_buttonstring
Free-form card: the caption of the button, up to 20 characters
Works only together with whatsapp_link
whatsapp_preview_urlboolean
Show a preview of the first link of a free-form text
whatsapp_lifetimenumber
How long to try to deliver the message, in seconds
From 60 to 86400
sms_signaturestring
SMS sender name for the fallback. Required for whatsapp+sms
sms_messagestring
SMS text for the fallback. Required for whatsapp+sms
sms_lifetimenumber
Lifetime of the fallback SMS in seconds
hookstring
URL address of the script where the message delivery status will be sent

Request example​

{
"auth": "bb56a4369eb19***cfec6d1776bd25",
"data": [
{
"type": "whatsapp",
"id": 100500,
"phone": 447700900123,
"whatsapp_signature": "WhatsAppTest",
"whatsapp_template": "order_ready",
"whatsapp_language": "en",
"whatsapp_var": [
"Anna",
"A-1024"
],
"whatsapp_image": "https://url.com/storage/images/order.png",
"hook": "https://example.org/webhook/url.php"
}
]
}

Response examples​

HTTP Status Code: 200
Content Type: JSON application/json

{
"success": true,
"data": [
{
"success": true,
"data": {
"id": 100500,
"msg_id": 123456789,
"data": 1,
"parts": 1
}
}
]
}

What a template needs​

Besides the values in whatsapp_var, a template may take a file, a text, a location or a button value. List WhatsApp templates returns this as shape:

shape of the templatePass in the request
header: image, video or documentwhatsapp_image, whatsapp_video or whatsapp_document; for a document also whatsapp_filename
header: locationwhatsapp_location
header: text and header_var: truewhatsapp_header
url_button: truewhatsapp_button_var
copy_code: truewhatsapp_coupon
authentication: truewhatsapp_code — and nothing else

A value the template needs but the request lacks, or a value the template does not take, is refused at acceptance, before the message is charged: WhatsApp itself would refuse such a message only after sending.

More examples​

{
"auth": "bb56a4369eb19***cfec6d1776bd25",
"data": [
{
"type": "whatsapp",
"id": 100501,
"phone": 447700900123,
"whatsapp_signature": "WhatsAppTest",
"whatsapp_template": "spring_sale",
"whatsapp_var": [
"Anna"
],
"whatsapp_button_var": "spring-24",
"whatsapp_coupon": "SPRING15",
"hook": "https://example.org/webhook/url.php"
}
]
}

Verification code (authentication template)​

{
"auth": "bb56a4369eb19***cfec6d1776bd25",
"data": [
{
"type": "whatsapp",
"id": 100502,
"phone": 447700900123,
"whatsapp_signature": "WhatsAppTest",
"whatsapp_template": "login_code",
"whatsapp_code": "481516",
"hook": "https://example.org/webhook/url.php"
}
]
}

Free-form document within the 24-hour window​

{
"auth": "bb56a4369eb19***cfec6d1776bd25",
"data": [
{
"type": "whatsapp",
"id": 100503,
"phone": 447700900123,
"whatsapp_signature": "WhatsAppTest",
"whatsapp_message": "Your invoice for September",
"whatsapp_document": "https://url.com/storage/files/invoice.pdf",
"whatsapp_filename": "Invoice A-1024.pdf",
"hook": "https://example.org/webhook/url.php"
}
]
}

Free-form location within the 24-hour window​

{
"auth": "bb56a4369eb19***cfec6d1776bd25",
"data": [
{
"type": "whatsapp",
"id": 100504,
"phone": 447700900123,
"whatsapp_signature": "WhatsAppTest",
"whatsapp_location": {
"latitude": 50.4501,
"longitude": 30.5234,
"name": "Pickup point",
"address": "1 Main St"
},
"hook": "https://example.org/webhook/url.php"
}
]
}

WhatsApp with an SMS fallback​

{
"auth": "bb56a4369eb19***cfec6d1776bd25",
"data": [
{
"type": "whatsapp+sms",
"id": 100505,
"phone": 447700900123,
"whatsapp_signature": "WhatsAppTest",
"whatsapp_template": "order_ready",
"whatsapp_var": [
"Anna",
"A-1024"
],
"sms_signature": "SMSTest",
"sms_message": "Anna, your order A-1024 is ready",
"hook": "https://example.org/webhook/url.php"
}
]
}

Errors​

An error comes in one of two places:

  • The whole request is refused when a field has a wrong type or format: success: false and the text in error at the top of the response. Nothing is created.
  • One message is refused when its content does not fit the sender or the template: success: false and the text in data[].error of that message. It is not created and not charged; the other messages of the request go on.

Request​

ErrorMeaning
Whatsapp signature parameter is requiredNo whatsapp_signature
Whatsapp message parameter is requiredNeither a template nor a free-form message (text, file or location)
WhatsApp template parameter must match [a-z0-9_]The template name is not in Meta's format
Whatsapp var value must not exceed 1024 charactersA value in whatsapp_var is too long
Whatsapp header must not exceed 60 characterswhatsapp_header is too long
Whatsapp button_var must not exceed 2000 characterswhatsapp_button_var is too long
Whatsapp filename must not exceed 240 characterswhatsapp_filename is too long
Whatsapp button caption must not exceed 20 characterswhatsapp_button is too long
Whatsapp lifetime must be in range from 60 to 86400whatsapp_lifetime out of range

Message​

ErrorMeaning
WhatsApp is not confirmed for userWhatsApp is not connected to your account
Error in WhatsApp senderThe sender is not connected to your account or has no WhatsApp route
WhatsApp sender display name is not approvedMeta has not approved the display name of the sender yet
WhatsApp template not foundNo template with this name and language for this sender
WhatsApp template is not approvedThe template is in review, rejected or paused
Wrong number of WhatsApp template parametersThe number of values in whatsapp_var differs from params of the template
WhatsApp template header content is requiredThe template has a media, location or text-with-placeholder header and its content is missing — see the table above
WhatsApp template header takes no valueThe text header of the template is static: do not pass whatsapp_header
WhatsApp template header type mismatchFor example, a video for a template with an image header
WhatsApp template has no headerDo not pass a file, a location or whatsapp_header
WhatsApp template has one headerPass one of: a file, whatsapp_location or whatsapp_header
WhatsApp template button value is requiredThe template has a dynamic link or copy-code button: pass whatsapp_button_var or whatsapp_coupon
WhatsApp template has no such buttonThe template has no button for the value passed
WhatsApp authentication template needs a codeAn authentication template without whatsapp_code
WhatsApp authentication template takes only a codeNothing but the code goes with an authentication template
WhatsApp code is only for authentication templateswhatsapp_code for a template that is not an authentication one
WhatsApp code is too longwhatsapp_code is longer than 15 characters
WhatsApp coupon is too longwhatsapp_coupon is longer than 15 characters
One kind of WhatsApp content per messageA free-form message carries one of: a file, a location, a card with a button
Only one WhatsApp media file per messageTwo files in one message
WhatsApp location and audio carry no textA location or an audio file goes without whatsapp_message
WhatsApp location coordinates are out of rangeLatitude outside −90…90 or longitude outside −180…180

Subscriber replies​

A reply of the subscriber opens the 24-hour window for free-form messages to them.

  • If the reply answers a message sent with hook, the webhook of that message is sent again with the list replies.
  • Otherwise the gateway sends a POST with form fields to the notification address in the API settings:
FieldValue
actionwhatsapp/inbound
phoneNumber of the subscriber
messageText of the reply or the caption of a file
typeType of the reply: text, image, video, audio, document, location, button and others
datetimeTime of the reply, YYYY-MM-DDThh:mm:ss±hh:mm
buttonThe button the subscriber pressed
media[url], media[type]Link to the file and its MIME type
location[latitude], location[longitude], location[name], location[address]The location the subscriber sent
msg_idYour message the reply answers, when it is known

A link to a file is valid for 24 hours: download the file when the notification arrives. For a reply with a file the notification comes once the file is stored with us, usually within seconds.