Send the First SMS
POST Method
SMS is a simple and a cost-effective way to communicate with customers. Before starting to send the SMS APIs, make sure that you have configured the SMS channel in Kalyra.io. This section details how to send your first outbound SMS using an API request.
Base URL
<https://api.kaleyra.io/v1/><SID>
To send your first SMS, follow the below steps:
- Before you get started, Create an API Key. To view the API Key and the SID, see View API Key and SID.
- API request to Send Your First SMS.
Request Format
To send your first SMS, use the /messages endpoints.
curl -X POST "https://api.kaleyra.io/v1/<SID>/messages" \
-H "api-key: <API_KEY>" \
-H "Content-Type: <CONTENT_TYPE>" \
-d "to=<TO_NUMBER>" \
-d "sender=<SENDER_ID>" \
-d "type=<TYPE>" \
-d "body=<BODY>" \
-d "ref=<OPTIONAL_PARAMETER>" \
-d "ref1=<OPTIONAL_PARAMETER1>" \
-d "ref2=<OPTIONAL_PARAMETER2>" \
-d "callback=<CALLBACK_OBJ>"
-d "callback_profile_id=<callback_profile_id>"
Sample Request Format
curl -X POST "https://api.kaleyra.io/v1/HXXXXXXX071US/messages" \
-H "api-key: Ac4XXXXX21f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "to=+1XXXXXXXXXX" \
-d "sender=KLRHXA" \
-d "body=Hello! This is my first SMS."\
-d "ref=Campaign name" \
-d "ref1=Client Name" \
-d "ref2=Product ID" \
-d "callback={"url":"https://webhook.site/0exxxxfd-axxc-4xx1-bxxe-cdxxxxxxxx70","method":"POST","header":{"x-api-key":"qwxxxxxxxxxxxxxxxxdv","x-randoem-id":"23xxxxey"},"retry":{"count":"3","interval":["10","10","10"]}}"
-d "callback_profile_id=IN_09b3dfa7-e0XX-4d84-ab2d-6ed654272XXX "
Note:
Make sure you replace the parameter values with the proper inputs in the above code.
Parameters and Data Types
The following table describes the parameters and data types used in the SMS API request.
Parameter | Data type | Description | Example | Mandatory |
---|---|---|---|---|
SID | String | Account SID (Security Identifier) | HXXXXXXX071US | Yes |
TO_NUMBER | Integer | Recipient's MSISDN | +1XXXXXXXXXX | Yes |
SENDER_ID | String | Originator/Alphanumeric ID (default KLRHXA) | KLRHXA | Yes |
API_KEY | String | API Key generated by Kaleyra.io | Ac4XXXXX21f | Yes |
TEMPLATE_ID | Integer | Unique ID of the template. Note: This is not a mandatory field. For IN customers this is a mandatory field and should be filled with the dlt_template_id | 2xxxxxxxxxxxx | No |
TYPE | String | Route type. Note: This is not a mandatory field. For customers in the Indian region, the available SMS Types for which you can create a template are OTP, Transactional (TNX), Transactional DND (TXND), and Marketing (MKT). For Europe, America, and Singapore regions, only OTP and Default (to be used for Marketing and Transactional) message types are available. | MKT | Yes |
REF (optional parameter) | String/Integer | Include the contextual information in this parameter. For Example: Name, Customer ID, and so on | Customer Name | No |
REF1 (optional parameter) | String/Integer | Include the contextual information in this parameter. For Example: Name, Customer ID, and so on | Company Name | No |
REF2 (optional parameter) | String/Integer | Include the contextual information in this parameter. For Example: Name, Customer ID, and so on | Designation | No |
CALLBACK | Object | This data allows Kaleyra to send back the statuses of the messages that you have sent. The callback URL can be accessed publicly. For more information on callback, see the SMS callback page. | { "url": "<https://webhook.site/0exxxxfd-axxc-4xx1-bxxe-cdxxxxxxxx70> ", "method": "POST", "header": { "x-api-key": "qwxxxxxxxxxxxxxxxxdv", "x-random-id": "23xxxxey" }, "retry": { "count": "3", "interval": XXXX071US", "0-4": "Yes", "1-0": " TO_NUMBER ","1-1": "Integer", " } } Note: Only URL and method are mandatory attributes, others are optional. Refer to the following table for the callback object attributes and the description. | No |
CALLBACK_PROFILE_ID | String | The callback profile ID of the callback profile to be used to send webhook to your system about SMS status updates. For more information related to callback profiles, see callback profiles page | IN_09b3dfa7-e05d-4d84-ab2d-6ed654272XXX | Yes |
The following table describes the callback object attributes.
Field | Description | Mandatory? |
---|---|---|
URL | The endpoint to receive callback information. | Yes |
Method | Method to receive callback. For example, GET and POST. | Yes |
Retry Object | ||
Count | The number of times the callback should be retried to send. | No |
Interval | The time interval in seconds to try sending the callback. | No |
Header | Additional data required by the endpoint to process callback. | No |
Optional Parameters
Add the required contextual information to your SMS using the Optional Parameters. You can include up to three parameters in the above SMS API call, and each can have a maximum of 255 characters.
Note:
For Example,
When you deliver a message to multiple customers, you can pass your own Message ID as optional parameters in the API call. This information is sent back in the callback from Kaleyra.io to your system for further reference.
You can also include information such as:
- Campaign Name
- Client Name
- Client ID
- Product ID and so on
Response Format
This section provides you the success and failure JSON response formats for different scenarios.
Success Response
{
"id": "ee8aXXXX-XXXX-XXXX-XXXX-9b717XXXX30d",
"sender": "KLRHXA",
"type": "DEFAULT",
"body": "Hello! This is my first SMS.",
"createdDateTime": "2019-11-04 10:42:23+00:00",
"totalCount": 1,
"data": [
{
"message_id": "ee8aXXXX-XXXX-XXXX-XXXX-9b717XXXX30d:1",
"recipient": "1XXXXXXXXXX"
}
],
"dlrurl": null,
"error": {}
}
The following table describes the API Response parameters.
Parameter | Description |
---|---|
body | The message content that is delivered to the handset. |
sender | The sender ID used to send the SMS. |
type | The message type. Note: The message type. Note: For customers in the Indian region, the available SMS Types for which you can create a template are OTP, Transactional (TNX), Transactional DND (TXND), and Marketing (MKT). For Europe, America, and Singapore regions, only OTP and Default (to be used for Marketing and Transactional) message types are available. |
template_ID | The template that is used by the Kaleyra customer. |
ID | The unique ID of the API request. |
createdDateTime | The request time of the API in UTC. |
total_Count | The number of contacts present in the API request. |
message_ID | This is the unique ID for each mobile number. |
recipient | The mobile number in the API request. |
unicode | This indicates if the customer is using message content as unicode or normal text(1/0/AUTO). |
flash | This indicates if the customer wants to deliver the message as a Flash message or not (1/0). |
ref, ref1, ref2 | These are the additional parameters the customers can use for their reference. These parameters will return the ‘callback response’. |
Failure Response
The following failure response is received when the JSON script has an incorrect syntax.
{
"code": "E401",
"message": "SMS length is exceeding 4000 characters.",
"error": {
"body": "SMS length is exceeding 4000 characters."
}
}
Note:
In case of an error, ensure that the parameter values are correct in the above code. Refer to the SMS Error Codes page for detailed information.
Two-way SMS for Customer Engagement
Kaleyra's Two-Way SMS API is designed to offer you an easy and efficient way to communicate with your customers. It allows you to instantly send and receive messages through your choice of long or short codes.
If you have purchased a two-way SMS number, you must pass the same number as a Sender ID in the above API call to initiate a two-way conversation with your customers.
Sample Request
curl --location --request POST '<host>/v1/HXXXXXXX071US/messages/' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'api-key: Ac4XXXXX21f' \
--data-urlencode 'to=196xxxxxx56' \
--data-urlencode 'body=Accepting number as sender test 1' \
--data-urlencode 'sender=917xxxxxx834' \
--data-urlencode 'type=MKT' \
--data-urlencode 'callback={"url":"https://webhook.site/0exxxxfd-axxc-4xx1-bxxe-cdxxxxxxxx70","method":"POST","header":{"x-api-key":"qwxxxxxxxxxxxxxxxxdv","x-randoem-id":"23xxxxey"},"retry":{"count":"3","interval":["10","10","10"]}}'
Note:
For Two-way SMS, the sender variable must be String/Numeric. For example +17XXXXXXXX4.
Updated 5 months ago