Using RESTful API to send text messages
Discussing practical implementation of letting your clients send text messages via your own RESTful API
Sending text messages is one of the most recent features added to SAARA system. It supports several ways to deliver Mobile Terminated (MT) messages to the end client:
- Most simplest way is to open a web interface on SAARA server, login as end user and send SMS from there. It is simple and handy for one off sending of one or few messages. It is not suitable for server-to-server connection or automated message delivery.
- SMPP protocol is industry standard for text message sending between Message Entities (ESMEs) and Mesage Centers. However, it requries implementation of SMPP which is not always straigh forward and easy.
- RESTful APIs are a popular method for machine-to-machine communication, and can be implemented much easier, since there are plenty of free and commercial suftware packages, libraries and tools which implement or support this functionality.
Here we will discuss the latter method. The step by stepo guide of how to set up SAARA system for MT message sending can be found at this link, and more specifically, the guide how to set up API support for SMS sending is located here.
The API is implemented with following API call:
POST /sms/send
{
"to": "1234567890",
"from": "9876543210",
"content": "Hello, world!"
}
The above request sends text mesage "Hello, world"
to phone number 1234567890
and the sender ID displayed on the recipients screen will be 987654321
The API will respond with confirmation where the body will contain message like:
{
"code": 0,
"status": 200,
"data": "accepted",
"request_id": "29737936-96c9-496f-aa13-1cd3a4544417"
}
Note, there is request_id
attribute in response. It can be used in further requests to obtain status of the message delivery and billing data if necessary.
API client can be implemented in many languages. The quickest way, without actually writing any line of code is either via browser addons, like RESTClient add-on for Mozilla Firefox or Advanced REST Client for Google Chrome. Other way is to use curl
, Linux CLI tool:
curl -X POST -H 'Authorization: token U51U9KhRl0t53n9W' -i 'http://server.address/api/sms/send' --data '{
"to": "1234567890
",
"from":"9876543210
",
"content":"Hello world!"
}'