Rate this page:

Programmable Voice Quickstart for Twilio Functions

You can use Twilio Functions with Programmable Voice to make and receive phone calls 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 Voice with a Function, you'll need to sign up for a Twilio account and purchase a Voice-capable phone number.

When you search for a number to buy, make sure the number you choose supports phone calls by checking the number's capabilities before you purchase it.

Buy voice-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 answer an incoming phone call

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 Voice" template and click "Create".

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

        Receiving an incoming phone call using Functions

        Receiving an incoming phone call using Runtime Functions

        Receiving an incoming phone call using Functions

        Take a look at the path property at the top of your function. Type "/voice" 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 "Voice" section. Find the "A call 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 calling it and see what happens.

        If the call doesn't work, try looking in the Console debugger or your Twilio Function logs, which are updated live when you're editing your function.

        Now let's make an outbound phone call

        Make an outbound phone call

        With a single API request, we can make an outbound phone call from the Twilio phone number we purchased earlier. We'll create a new Function to initiate the phone call and use our first function to provide Twilio with the TwiML we want to use on the call.

        Go back to your Functions dashboard and create a new function. This time, choose the "Blank" template.

        Give your new Function a path of "/new-call" and then copy and paste this code.

              Make a phone call using a Runtime Function

              Make a phone call using a Runtime Function

              Make a phone call using a Runtime Function

              This code uses Twilio's Programmable Voice API to make a phone call 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."

              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 call on your phone shortly. If you don'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 making and receiving phone calls 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!

              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