Getting Started with SMS and Voice Programmable Communications
Published on May 18, 2021

We recently ran a webinar with David Leary from Intuit Developer to go over the basics of using the Nexmo SMS and Voice APIs. If you are planning to attend the upcoming Intuit Small Business Hack and want a heads-up of what's possible, or if you're just interested in getting a quick overview of SMS and Voice programmable communications with Nexmo, here are the sections of the webinar that directly address sending an SMS, receiving an SMS, making an outbound phone call and receiving an inbound phone call. All examples also come with code snippets and links to further in-depth reading.

Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs.

How to Send an SMS

This section of the webinar covers how to send an outbound SMS. It covers using alphanumeric senders and using a number that has been purchased from Nexmo as the from number.

const Nexmo = require('nexmo');

const nexmo = new Nexmo({
apiKey: API_KEY,
apiSecret: API_SECRET,
}, {debug: true});

nexmo.message.sendSms(
FROM_NUMBER,
TO_NUMBER,
'Hello from @leggetter', (error, result) => {
if(error) {
console.error(error);
}
else {
console.log(result);
}
});

For more information on sending an SMS check out our post on sending an SMS with Node.JS, the SMS API guide and SMS API reference. When sending SMS you may also want to know if the message has been delivered. To achieve this you can register for an SMS Delivery Receipt. There's also a blog post on receiving an SMS delivery receipt with Node.JS.

How to Receive an SMS

In this part of the webinar we cover receiving an inbound webhook containing the inbound SMS information.

const Nexmo = require('nexmo');

const app = require('express')();
app.set('port', (process.env.PORT || 5000));
app.use(require('body-parser').urlencoded({ extended: false }));

app.listen(app.get('port'), () => {
console.log('Example app listening on port', app.get('port'));
});

app.post('/sms', (request, response) => {
console.log('Received message text "%s"', request.body.text);

response.sendStatus(200);
});

You can find more info on receiving an SMS in our post on receiving an SMS with Node.JS, the SMS API guide and SMS API reference.

How to Make an Outbound Phone Call

Here's how to make an outbound phone call using the Nexmo Voice API.

const Nexmo = require('nexmo');

const nexmo = new Nexmo({
apiKey: API_KEY,
apiSecret: API_SECRET,
applicationId: APPLICATION_ID,
privateKey: PRIVATE_KEY
});

nexmo.calls.create({
to: [{
type: 'phone',
number: TO_NUMBER
}],
from: {
type: 'phone',
number: FROM_NUMBER
},
answer_url: ['https://nexmo-community.github.io/ncco-examples/conference.json']
}, (err, res) => {
if(err) { console.error(err); }
else { console.log(res); }
});

We have a blog post that covers making an outbound phone call with Node.JS, there's a Voice outbound phone call API guide and of course a Voice API reference. Also, take a look at the NCCO reference for information on controlling Nexmo conversations and calls as used in the answer_url in the above example.

Receiving an Inbound Phone Call

Finally, here's how to receive and control an inbound phone call. This introduces the concept of Nexmo Conversation Control Objects (NCCOs).

const Nexmo = require('nexmo');

const nexmo = new Nexmo({
apiKey: API_KEY,
apiSecret: API_SECRET,
applicationId: APPLICATION_ID,
privateKey: PRIVATE_KEY
});

const app = require('express')();
const bodyParser = require('body-parser');
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.listen(app.get('port'), () => {
console.log('Example app listening on port', app.get('port'));
});

app.get('/answer', (request, response) => {
console.log('Incoming call from "%s"', request.query.from);

// record - All or part of a Call No
// conversation - A conference.
// connect - To a connectable endpoint such as a phone number
// talk - Send synthesized speech to a call
// stream - Send audio files to a call
var ncco = [
// {
// action: 'talk',
// text: 'hello from me',
// loop: 3,
// bargeIn: true
// },
{
action: 'stream',
streamUrl: ['http://www.ladyofthecake.com/rdmp3/theme.mp3'],
loop: 3,
bargeIn: true
},
{
action: 'input',
eventUrl: ['https://nexmo.ngrok.io/event']
}
];

response.json(ncco);
});

app.post('/event', (request, response) => {
console.log('Received event', request.body);

response.sendStatus(200);
});

More info on receiving an inbound phone call is covered in our post on receiving an inbound phone call with Node.JS, in the Voice inbound phone call API guide and the Voice API reference. The above example also mentions record, talk, stream and other NCCO actions. So, take a look at the NCCO reference for information on controlling Nexmo conversations and calls.

More Programmable Communications Goodness!

There's lots more information about Nexmo APIs in the docs that haven't been mentioned above. For example, the Verify API for 2FA and one-time passwords and Number Insight for looking up information on phone numbers. We've also got a selection of programmable communications tutorials that cover building specific communications use cases such as private voice communications, SMS customer support, interactive voice response, and more using the Nexmo APIs.

If you're going to Small Business Hack, good luck, and we'll see you there. If you just dropped by to learn about programmable communications we hope you find this useful. Either way, feel free to join the Nexmo Community Slack if you have any questions.

Phil Leggetter

Phil is Head of Developer Relations at Hookdeck, an asynchronous messaging platform, and a proud Vonage alumni.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.