Send Media Messages

POST Method

The Send Media Messages API allows you to send media messages to WhatsApp users. Before you send the WhatsApp messages containing media from your system drives you need to upload the media files and generate the media ID. You can use the media ID in your API request to send the media files. For more information on how to upload media files, see Upload Media Files. You can use the media ID for multiple WhatsApp messages. Also, you can use any media URL link that is available publicly in the media message.

Prerequisites

  1. Before you get started, Create an API Key. To view the API Key and the SID, see View API Key and SID.
    A prerequisite for using Kaleyra WhatsApp APIs is to have an active WhatsApp plan on kaleyra platform.
  2. To set up a WhatsApp account on kaleyra.io, see Manual Signup
    and Embedded Signup.
  3. An active WhatsApp for Business API plan that includes:
    • A WhatsApp business number.
    • An associated profile with the business number.
    • A WhatsApp verified and approved profile.

Base URL

https://<api_domain>/v2/<SID>

API Domain and Value

api_domainValue
IN podapi.in.kaleyra.io
SG Podapi.ap.kaleyra.io
EU Podapi.eu.kaleyra.io

API request to send Media Messages

To send Media Messages, use the https://<api_domain>/v2/<sid>/whatsapp/<phone_number>/messages endpoint with the following request method:

Request Format

The following is the request format to send Media Messages:

curl -X POST \
  https://<api_domain>/v2/<sid>/whatsapp/<phone_number>/messages \
  -H 'Content-Type: application/json' \
  -H 'api-key: <api-key>' \
  -H 'cache-control: no-cache' \
  -d  '{
    "messaging_object": {
        "messaging_product": "<messaging_product>",
        "recipient_type": "<recipient_type>",
        "to": "<to_number>",
        "type": "<type>",
        "image": {
            "id": "<media_object_id>", 
            "link": "<media_url>" 
        }
    },
    "extra_info": {
        "ref": "<ref>",
        "ref1": "<ref1>",
        "ref2": "<ref2>",
        "wa_source": "<wa_source>"
    }
}'

Sample Request Format

The following is the sample request format to send Media Messages:

curl -X POST \
  https://api.in.kaleyra.io/v2/HXAP16XXXXXX97IN/whatsapp/91XXXXXXXXXX/messages \
  -H 'Content-Type: application/json' \
  -H 'api-key: <api-key>' \
  -H 'cache-control: no-cache' \
  -d  '{
    "messaging_object": {
        "messaging_product": "whatsapp",
        "recipient_type": "individual",
        "to": "+91XXXXXXXXXX",
        "type": "image",
        "image": {
            "id": "10xxxxxxxxxxxx91", /* Only if using uploaded media */
            "link": "<MEDIA_URL>" /* Only if linking to your media (Use Public URL) */
        }
    },
    "extra_info": {
        "ref": "customer1",
        "ref1": "marketing",
        "ref2": "dictionary",
        "wa_source": "api"
    }
}'

Parameter and Data Types

Following is the list of parameters and data types supported for sending media messages:

ParameterData TypeDescriptionExampleMandatory?
sidStringAccount SID (Security Identifier).HXAP16XXXXXX97INYes
phone_numberStringPhone number of end customer you want to send a message to.91XXXXXXXXXXYes
(valid phone numbers only)
content_typeStringIndicates the format of the content the API will be processing.Only allowed value is application/jsonYes
api_keyStringAPI key generated from kaleyra platform account.Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3Yes

Following is the list of attributes to be used in the payload to send the outgoing message:

ParameterData TypeDescriptionExampleMandatory?
messaging_objectJSON objectPayload is supported only in JSON format. The messaging_object is the payload which contains the message object as defined by Meta which is used to send the WhatsApp message.See the specific table for details.Yes
extra_infoJSON objectThis object will contain additional details that a user can send for certain functionalities.See the specific table for details.No

The following table describes the different attributes used for the messaging_object JSON object:

ParameterData TypeDescriptionExampleMandatory?
messaging_productStringMessaging service used for the request.Only allowed value is whatsappYes
recipient_typeStringCurrently, you can only send messages to individuals. Default is individual.individualNo
to_numberStringA valid WhatsApp number of the recipient. Ensure that the country code is prefixed to the number. (E164 format).
Note: You can add multiple recipients, separate each number using the comma (,) delimiter.
+91XXXXXXXXXXYes
typeStringThe type of message you want to send. If omitted, defaults to media.See the specific table for details.Yes

The following table describes the different attributes used for the type object:

ParameterData TypeDescriptionExampleMandatory?
id StringRequired when type is audio, document, image, sticker, or video and you are not using a link.
Note: The media object ID. Do not use this field when the message type is set to text.
10xxxxxxxxxxxx91Yes
linkStringRequired when the type is audio, document, image, sticker, or video and you are not using an uploaded media ID (i.e. you are hosting the media asset on your public server).

The protocol and URL of the media to be sent. Use only with HTTP/HTTPS URLs.

Do not use this field when the message type is set to text.
https://www.kaleyra.com/wp-content/uploads/kaleyra.pngYes

📘

Notes:

  • If you like Meta to cache the media asset for future messages see, Media Caching.
  • When we request the media asset from your server you must indicate the media's MIME type by including the Content-Type HTTP header. For example: Content-Type: video/mp4. for a list of supported media and their MIME types see, Supported Media Types.

The following table describes the different attributes used for the extra_info JSON object:

ParameterData TypeDescriptionExampleMandatory?
ref StringInclude any contextual information in this parameter you want to associate with the request. For Example, Name, Customer ID, and many more.
Limit: The variable can be 255 characters long only. This parameter will be available in report and callback.
customer1No
ref1 StringInclude the contextual information in this parameter you want to associate with the request. For Example, Name, Customer ID, and many more.
Limit: The variable can be 255 characters long only. This parameter will be available in report and callback.
marketingNo
ref2 StringInclude the contextual information in this parameter you want to associate with the request. For Example, Name, Customer ID, and many more.
Limit: The variable can be 255 characters long only. This parameter will be available in report and callback.
dictionaryNo
wa_sourceStringIndicates the source from where the API has been triggered. Supported values for wa_source are "api", "clevertap", "zoho", "webengage", "shopify", "leadsquared", "responsys", "braze", "hubspot", "salesforce", "zapier", "eloqua", and "moengage".
Note: If the customer passes any other value other than above-specified values, then the system will override that as the "API".
apiNo

📘

Note:

In the case of ref, ref1, and ref2, if the length of the string exceeds 255 characters, it will be truncated.

Sample Success Response

The following success message appears with the status 202 Accepted:

202 Accepted 
{
"code":"WA202"
"message":"Request Accepted",
"data":{
  "message_id":"AXXXXXXXXXXXXXXXXXXX"
}
"error":{}
}

Sample Failure Response

The following are the failure responses:

401 Unauthorized
{
    "code": "RBC001",
    "message": "Incorrect SID or API key.",
    "data": [],
    "error": {
        "error": "Incorrect SID or API key."
    }
}
401 Unauthorized phone number
{
    "code": "WA401",
    "message": "Phone number is not present or does not belong to the account.",
    "data": {},
    "error": {
        "phone_number": "Phone number is not present or does not belong to the account."
    }
}
400 Incorrect payload
{
    "code": "WA400",
    "message": "Refer to correct payload format",
    "data": {},
    "error": {
        "payload": "Incorrect payload format"
    }
}
500 Internal Server Error
{
    "code": "WA500",
    "message": "Please try again later",
    "data": {},
    "error": {
        "error": "Internal server error"
    }
}
400 Low balance
{
    "code": "E110",
    "message": "Please check your balance, You have a low balance!",
    "data": {},
    "error": {
        "balance": "Please check your balance, You have a low balance!"
    }
}
401 Unauthorised Account type
{
    "code": "WA-401",
    "message": "API is not available for given customer.Please contact support for more info",
    "data": {},
    "error": {
        "account": "API is not available for given customer.Please contact support for more info"
    }
}
401 Unauthorized phone number version
{
    "code": "WA-401",
    "message": "The phone number you're using is associated with different Version of our WA API. Please use the appropriate endpoint.",
    "data": {},
    "error": {
        "phone_number": "The phone number you're using is associated with different Version of our WA API. Please use the appropriate endpoint."
    }
}