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.
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.
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.
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.
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.
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.
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.
Now you've mastered the basics of making and receiving phone calls with Twilio Functions.