Vonage Developer
Published in

Vonage Developer

How to Send and Receive SMS Messages With Node.js and Azure Functions

If you want to try it out first, I’ve set up a few Nexmo phone numbers for different countries, and you can send an SMS message to +447451272987 or +13095902987 to get your messages back as emojis. If you want me to set up a number for your country, , I’ll provision it and update the post here for others to use.

Webhooks are ever-more popular in the API world, and Nexmo uses them for quite a few of our APIs. So I end up writing quite a few of them. Since I’m a Node.js fan, I used to do most of them with Express. It was a hassle to spin up a webserver and get that deployed somewhere, so I started looking for alternatives. Serverless kept popping up in my research as one of the good alternatives, so I thought I’d use and Node.js to send and receive SMS messages.

For SMS, I’m going to use the Nexmo , which allows you to send and receive a high volume of SMS anywhere in the world. Once you get your virtual phone number, you can use the API to manage outbound messages (“sending”) and inbound messages (“receiving”).

In this article, you will learn how to send and receive SMS messages with and an .

The code for this tutorial can be found on .


Before you begin, make sure you have:

Set up an Azure Functions App

There are a few ways you can set up an Azure Functions App. I’ve chosen to do so from the .

I’ve named it send-receive-sms, created a new resource for it, and chose Node.js as the runtime. I've chosen the defaults in terms of deployment, region, and plans.

Once the deployment finishes (usually around 2 minutes), you can go to the newly created resource and create your first function. There is a wizard helping you through the process, and I’ve selected as my development environment and “Direct publish” as my deployment method. That gave me a set of instructions on how to set up VS Code for Azure.

Once I was done with the setup, I created my first function, an HTTP Trigger. I’ve called it inbound, but you can use any name you want. For the security mechanism, you have a few options that control the authorization level on the function. "Function" requires a function-based API key, "Admin" requires your master API key from Azure, and "Anonymous" doesn't require an API key. I've chosen "Anonymous" as the security mechanism for it. That created a new file with a default template.

Receiving an SMS Message

Create a Webhook Function

I’ve replaced the contents of the function to match the we use on our developer portal. It combines the body and query parameters of a request and logs that to the console. It’s a simple Webhook you can use to receive SMS messages on the Nexmo platform.

Webhook Azure Function

Deploy Function

Because Nexmo needs to be able to access a public URL, I’ve deployed my function with Azure. You can do so in VS Code. Once the deployment finishes, you’ll get back a live URL for your function, for example, mine is

Set Inbound URL

To receive a SMS message on the Nexmo platform, you have to own a Nexmo number. If you don’t have one, you can buy one in the :

When a Nexmo phone number receives an SMS message, Nexmo will pass that message to a Webhook you have specified in the Nexmo dashboard. We’ll use our Azure Function as the receiving Webhook. To set up the webhook URL, go to the little gear icon next to and fill in the “Inbound Webhook URL” field with the URL you got after deploying the Azure function (mine was

You can test it works by sending an SMS from your phone to your Nexmo phone number, and it will show up in the Azure function logs.

Sending an SMS Message

I thought it would be nice if the Function did a little more than logging the SMS messages, so I’m going to make it respond to SMS messages as well, changing the message from text to emojis.

I’m going to use the Nexmo Node.js SDK for sending the SMS message, so you’ll need to install it in the Azure function. The top-level folder for your function has a package.json file in it that gets installed with each deployment. So if you navigate to that folder in the Terminal and use npm install nexmo, that will give you access to the Nexmo SDK inside the Azure function.

I’ve also updated the function to initialize the Nexmo SDK, convert the SMS message to emojis and then send it as an SMS back to the original sender.

You’ll notice my code initializes the Nexmo SDK with process.env["NEXMO_API_KEY"] and process.env["NEXMO_API_SECRET"]. The Azure Function App has Application settings and I've added my NEXMO_API_KEY and NEXMO_API_SECRET in there, and those get updated with each deployment from my local machine. You can do so as well, or replace those values with your actual API key and secret. You can find those on the "Getting Started" page in the .

The SDK method for sending an SMS, nexmo.message.sendSms(), takes the sender ID, the recipient phone number, the text message and options as parameters. I've added {type: "unicode"} in the options because I'm sending emojis. It also accepts a callback that gets called when the API request is done.

The response data contains an array for all the messages that were sent, with information about their status. In most cases, it’s going to be one element in that array, but if the SMS was longer than 160 characters, it gets split into a multipart SMS, and then the array contains data about each part sent. If the status of the message is 0, the SMS was sent successfully, otherwise, the error data for the message is on the error-text property of the message.

Because my text has an emoji in it, I’m setting the type unicode in the options object, otherwise, that emoji is going to be sent on the network as ?.

Try It Out

Now send an SMS message from your phone to your Nexmo number. You should get back an SMS message converting your text characters to emojis. If you send Hello World! in the SMS, you should get back 🌷🍈🍛🍗🌃🍐🍈🌃🍗🍈🍑🍇🍈🍕🌑.

I hope it worked and you’ve just learned how to send and receive SMS messages with the Nexmo APIs. Azure Functions and Node.js.

Originally published at on January 29, 2020.



Updates, ideas and news on communications APIs, libraries, SDKs and developer tools by the Developer Relations Team at Vonage.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Lakatos

Leading technology @interledger by day, @mozillareps & @moztechspeakers by night. I do things for T-Shirts. Made . 1/2 @DevRelAvocados