Programmable SMS Quickstart for Node.js - Twilio
Register for SIGNAL by 8/31 for $250 off. Register now.

Programmable SMS Quickstart for Twilio Functions

You can use Twilio Functions with Programmable SMS to send and receive text messages without managing your own infrastructure. All you need is a Twilio account and a few lines of Node.js code.

Get a Twilio phone number

Before you can use Programmable SMS with a Function, you'll need to sign up for a Twilio account and purchase an SMS-capable phone number. When you search for a number to buy, make sure the number you choose supports SMS messages by checking the number's capabilities before you purchase it.

Buy a SMS or MMS capable phone number

 

Click "Buy" to add one of these numbers to your account.  Now that you have a Twilio account and a programmable phone number, it's time to write some Functions code.

Create a Runtime Function

To create a new function, go to Functions page in the Twilio Console and click the "+" icon.

You can either create a new Function from scratch or start with one of our templates. For this quickstart, choose the "Hello SMS" template and click "Create".

That will create your new Function and prepopulate it with code that looks like this.

Loading Code Samples...
Language
Format:
  • XML
exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.MessagingResponse()
  twiml.message("Hello World")
  callback(null, twiml)
}
<Response>
  <Message>Hello World</Message>
</Response>
Receiving an inbound SMS
Receiving an inbound SMS

Receiving an inbound SMS

Take a look at the path property at the top of your function. Type "/sms" in that field and then click "Save" at the bottom of the form. Now copy the full URL for the path field. You'll need it for our next step.

Go back to the phone number you purchased earlier. You'll find it in your list of Twilio phone numbers in the Console.

Click on your phone number and then scroll down to the "Messaging" section. Find the "A message comes in" field, choose "webhook" from the select menu, and then paste the full URL to your Twilio Function. Then click "Save" on your phone number.

Your number should be ready for action! Try sending it a text message and see if you get a reply.

If you don't get a reply, try looking in the Console debugger or your Twilio Function logs, which are updated live when you're editing your function.

Send an outbound SMS message

With a single API request, we can send an outbound text message from the Twilio phone number we purchased earlier. Copy and paste in this code sample to your Function.

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
  context.getTwilioClient().messages.create({
    to: '+17035551212',
    from: '+12025551212',
    body: 'Hello Monkey!'
  }).then(msg => {
    callback(null, msg.sid);
  }).catch(err => callback(err));
}
Send SMS using Runtime Functions
Send SMS using Runtime Functions

Send SMS using Runtime Functions

This code uses Twilio's Programmable SMS API to send a text message using the twilio-node helper library, which we automatically provide for you in every Function.

You'll want to update the to value to be your cell phone number and the from value to be your Twilio phone number. Be sure to use E.164 formatting for both numbers.

Before it can work, though, you'll need to update your Functions configuration to tell Twilio to include your account credentials in every function's environment. Check the box that says "Enable ACCOUNT_SID and AUTH_TOKEN."

NOTE: You'll also need to uncheck the box on your function called "check for valid Twilio signature" before your function will accept requests from outside Twilio.

This feature locks down your function so only Twilio can access it, which helps prevent abuse. But it's easier to test your function when this feature is turned off. 

Now try invoking your Function by sending an HTTP request to it. You can send an HTTP request to your function any way you like. We recommend just copying the "path" value and opening it in a new browser tab.

You should receive a new message on your phone shortly. If you didn't, try checking the logs which appear beneath your function's code for error messages.

Now you've mastered the basics of sending and receiving text messages with Twilio Functions.

Check out the full Twilio Runtime docs for more information about how to use Functions. And pop over to the Programmable SMS docs to learn more about sending and receiving text messages.

Happy hacking!

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Samples...
Format:
  • XML
exports.handler = function(context, event, callback) {
  let twiml = new Twilio.twiml.MessagingResponse()
  twiml.message("Hello World")
  callback(null, twiml)
}
<Response>
  <Message>Hello World</Message>
</Response>
exports.handler = function(context, event, callback) {
  context.getTwilioClient().messages.create({
    to: '+17035551212',
    from: '+12025551212',
    body: 'Hello Monkey!'
  }).then(msg => {
    callback(null, msg.sid);
  }).catch(err => callback(err));
}