Send an SMS using Nexmo and Azure Logic Apps
Every once in a while I have to send an SMS to someone for some reason, and I need to be able to do it programmatically.
I know that SMS is an "old school" approach, but it just works. Especially nowadays when we're bombarded by a zillion of Internet-connected apps, SMS (and phone calls) is something that stands out. Another "selling point" is that an SMS doesn't need an Internet connection to be delivered, meaning that the recipient can either have a "dumb" phone or be somewhere with a lousy Internet connection.
Those situations aren't even rare. Take me for example - I live in a village that's 10 km away from the center of Zagreb, the capital of Croatia. Although we have 4G/LTE, there are some spots while I'm driving home without an Internet connection, sometimes the cell tower goes nuts, etc. An additional problem is that sometimes I leave my iPhone at home or in my car, enjoying some time in the nature, carrying only a "dumb" phone. At those moments, the only way for a client or a system alert to reach me is by sending an SMS or calling me.
Why Nexmo
For my "whole" life I've been using Twilio because "everyone" is using it, and it just works. When I've decided to start writing about all the little helper tools that I've built, I've started making them again to take screenshots, and then I saw that there are new contestants available in Logic Apps. After some research, I've decided to start playing with Nexmo, because it seemed more usable than Plivo and TxtSync, and unlike Infobip, you can get an account right away.
Keep in mind that these Logic Apps that I'm describing are helper functions. I'm not using them in large scale production. I'm only using two service providers for large scale production - Twilio (look at the reasons above) and Infobip. Infobip is a really cool multinational company but based in Croatia (the country I'm from). Their SMS service is superb, but since they're targeting larger businesses, you won't be able to get an account without going through the sales channel.
Get a Nexmo account
The first step is to go to Nexmo site, get an account, and add some money it.
Once you're done with that, go to the "Getting started" page because you're going to need the API Key and the API Secret to connect Nexmo with Logic Apps.
Create a new Logic App
You'll need a new Logic App which responds to HTTP(S) POST requests. Since that's a common requirement in general and a common requirement in this series of blog posts, I've extracted the steps into a separate blog post available here.
Please go there, follow the steps, and then come back here. You're going to need to paste a JSON sample payload specific to this blog post, so here it is:
{
"senderId":"CM",
"recipientPhoneNumber": "+385912345678",
"smsBody": "Hello world"
}
Add the next step
Once you're done with the previous step, click on the "+ New step" button, and select "Nexmo Send an SMS".
The first time you add something that's connecting to Nexmo, you're going to need to create a connection to it. The "Connection name" field expects you to give a name to the connection and it can basically be anything. The "API Key" and "API Secret" fields are the ones where you should paste the values from the Nexmo Getting Started web page. Once you're done with it, click "Create", and you'll land on a "Send an SMS" window.
What you're expected to do here is fill out the three fields - "Sender Id", "To Phone Number", and "Text". Luckily, those are the values that have been sent to us in a JSON through the HTTP(S) POST request and we've deserialized them in the previous step. Because of that, when you click on one of those fields, a new window will pop up on the right offering you to select content available from the previous step. Once you're done with filling out the fields, hit the "Save" button.
When you first hit the "Save" button on a new Logic App, the HTTP(S) POST URL gets generated, so expend the first step - the window named "When a HTTP request is received", and copy paste the URL.
Testing using Postman
If you're not using Postman, perhaps you could give it a run. It has a free tier, it makes testing your APIs extremely easy, and it allows you to create collections of HTTP(S) requests, so that you can define them once, save them, and then reuse them whenever needed.
First of all, change the HTTP(S) verb into POST. Paste the URL from the previous step into the address bar. Click on "Body" beneath the address bar, select "raw" in the radio buttons section beneath the "Body" tab, and select "JSON (application/json)" from the drop-down at the right side of the radio buttons section.
Once you've configured that, paste a JSON into the body text field, and hit the "Send" button.
{
"senderId":"CM",
"recipientPhoneNumber": "+385912345678",
"smsBody": "Hello world"
}
It should take less than a second to finish, and if successful you should see the "Status: 202 Accepted", and in a couple of seconds the phone which has the phone number you've entered should receive a message.
That's it... enjoy sending SMS messages :)
Just keep in mind that while executing thousands of Logic Apps is cheap, sending thousands of SMS messages might not be ;)