Rate this page:

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.

Show me how it's done!

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.

Let's process an incoming SMS message

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.

        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.

        Now let's send an outbound SMS message.

        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.

              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.

              So cool! What else can I do?

              What's next

              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 Voice docs to learn more about making and receiving phone calls.

              Happy hacking!

              Rate this page:

              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 by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.


                    Thank you for your feedback!

                    We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    Refer us and get $10 in 3 simple steps!

                    Step 1

                    Get link

                    Get a free personal referral link here

                    Step 2

                    Give $10

                    Your user signs up and upgrade using link

                    Step 3

                    Get $10

                    1,250 free SMSes
                    OR 1,000 free voice mins
                    OR 12,000 chats
                    OR more