How to create an inbound Phone Proxy without any code using TwiML Bins

February 15, 2022
Written by
Reviewed by

How to create an inbound Phone Proxy without any code using TwiML Bins

 

You can quickly buy phone numbers from around the world using Twilio's Super Network. You can even specify area codes to buy a phone number that is local to a certain state, territory, or region. Combining that with Twilio Programmable Voice and Twilio Programmable SMS, you can quickly build a proxy phone number!

A proxy phone number is a public phone number hiding your real phone number that you, ideally, would want to keep private. When the proxy phone number is dialed or receives an SMS, it will be forwarded to your private phone number.

Why is this useful? Privacy; because:

  • You may not want to hand over your real phone number to marketeers, when signing up for services, etc. Instead, give out your proxy phone number and, if you desire, swap to a new proxy phone number as needed.
  • Maybe you are a streamer who takes phone calls while on air, but you don't want to share your private phone number with your audience.
  • Maybe you enjoy demoing Twilio and you don't want to accidentally dox yourself in the process 😉

So how do you set this up? If you are a professional software developer, you could integrate Twilio's APIs into your code, like in this JavaScript/Node.js blog post. But, you don't have to be a software developer or write code to take advantage of Twilio's APIs. If you don't want to write code, you can use Twilio's low-code/no-code tools instead:

  • Use TwiML Bins to respond to phone calls and text messages using TwiML and the Mustache templating language.
  • Use Twilio Studio, our most powerful tool, by dragging and dropping widgets onto a canvas and connect them together to create application flows. When you need even more flexibility, you can call into Twilio Functions from Twilio Studio to run JavaScript code, and you can also use TwiML Bins inside your Twilio Studio flows.

A phone proxy could work for inbound and outbound calls or messages. This tutorial, however, will only handle inbound phone calls and SMS by using a Twilio Phone Number and multiple TwiML Bins.

If you want your phone proxy to handle inbound and outbound messages and phone calls, follow this tutorial to create a bidirectional phone proxy using JavaScript/Node.js and Twilio Functions.

Prerequisites

You will need these items to follow along:

  • A phone with a personal phone number
  • Another phone number to test phone proxying (or ask a friend to help test)
  • A web browser
  • A free or paid Twilio account (If you register here, you'll receive $10 in Twilio credit when you upgrade to a paid account!)

To keep things as approachable as possible, this tutorial will use the Twilio Console exclusively.

Buy a Twilio phone number

After creating a Twilio account, you will be taken to your account dashboard. If you don't have a Twilio phone number already, click on the Get a Trial Number button.

Twilio Console interface with the mouse hovering over a button with text "Get a Trial Number"

Accept the phone number suggested by Twilio by clicking the Choose this Number button.

Modal in Twilio Console proposing a phone number. Pointer clicking on button "Choose this Number"

Alternatively, you can navigate to Phone Numbers > Manage > Buy a number. Here you can look for different numbers and quickly buy them:

Sidebar navigation with "Buy a number" selected. On the Twilio Console, a list of available phone numbers to buy is shown. Pointer is clicking the "Buy" button next to a phone number.

Now that you have a phone number, you can move on to configuring how Twilio should respond to SMS and phone calls made to your Twilio Phone Number.

What is TwiML

To turn your phone number into a proxy, you need to provide instructions to Twilio whenever an SMS or phone call is made to your Twilio Phone Number. Twilio requests these instructions, but you configure how those instructions should be requested.

For example, you can tell Twilio to request instructions via a Twilio Function or a generic webhook to programmatically generate the instructions. Alternatively, you can use the low-code/no-code alternatives – TwiML Bins and Studio Flows – to provide instructions.

To provide these instructions, you'll use the Twilio Markup Language or TwiML. This TwiML will tell Twilio how to respond to a message or a phone call.

Here's sample TwiML provided by Twilio:

<Response>
    <Say voice="alice">Thanks for the call. Configure your number's voice U R L to change this message.</Say>
    <Pause length="1"/>
    <Say voice="alice">Let us know if we can help you in any way during your development.</Say>
</Response>

When someone calls your phone number and Twilio receives these TwiML instructions, the caller will hear the contents of the "Say" node in "Alice's" voice, followed by a 1-second pause, and then followed by the content of the second "Say" node.

The verbs and attributes of TwiML are different when responding to messages compared to responding to voice calls. When responding to messages, you'll use ​​TwiML for Programmable SMS, and when responding to voice calls, you'll use TwiML for Programmable Voice.

Create TwiML Bins to forward SMS and phone calls

Now that you understand TwiML, it's time to create some TwiML bins. What are TwiML Bins?
You can use TwiML Bins to freely store your TwiML instructions and even use the Mustache template language to dynamically construct your TwiML. Once you save your TwiML into a TwiML bin, you can configure Twilio Phone Numbers to use the bin as its instructions for incoming messages or phone calls.

Back in the Twilio Console, click on Explore Products + in the left navigation menu, look for the TwiML Bins product and click on it. Click the Create new TwiML Bin button and fill out the form.


Enter "Proxy SMS" into the FRIENDLY NAME field and copy/paste the code below into the TWIML field.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Message to="[YOUR_PERSONAL_PHONE_NUMBER]">
    {{From}}: {{Body}}
  </Message>
</Response>

Replace [YOUR_PERSONAL_PHONE_NUMBER] with your personal phone number in E.164 format (+11234567890).

This TwiML Bin will send a message to your personal phone number. The body of the message is constructed using Mustache syntax. {{From}} will insert the phone number of the sender, and {{Body}} will insert the body of the incoming message, into the message sent to you.

For example, when I text "Hello" to your Twilio Phone Number from my personal phone number +11234567890, you will receive a text message on your personal phone number that looks like "+11234567890: Hello".

This way, you know what phone number sent the SMS, and what the body of the SMS was, without actually handing out your phone number.

You can generate dynamic TwiML using Mustache, but Mustache is purposely "logic-less", meaning that it's capabilities are, by design, limited only to templating. This also means that you cannot do text comparisons, which is what you'd need to proxy both inbound and outbound messages and phone calls. If you need more flexibility than what Mustache offers, use Twilio Studio to generate your TwiML.

Click the Create button, and then click the My TwiML bins link.

Click on the + button on the top left to create another TwiML Bin, this time to forward phone calls. Fill out the form with "Proxy Phone Calls" as the FRIENDLY NAME and copy/paste the code below into the TWIML field.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Number>[YOUR_PERSONAL_PHONE_NUMBER]</Number>
  </Dial>
</Response>

Replace [YOUR_PERSONAL_PHONE_NUMBER] with your personal phone number in E.164 format (+11234567890) and click Create.

These TwiML instructions will instruct Twilio to dial your personal phone number when your Twilio Phone Number is called. You will be able to see the phone number that's calling you on your phone, but they will only see the Twilio Phone Number.

Configure TwiML Bins as the message and phone call response

Now that you have created the TwiML Bins, you can use them to configure where your Twilio Phone Number should request instructions from for incoming messages and phone calls.

In the left side navigation, navigate to Phone Numbers > Manage > Active numbers. Click on your Twilio phone number to navigate to its settings.

Twilio Console page listing active phone numbers in your Twilio account. Cursor is clicking on the single phone number in the list.

Find the A CALL COMES IN fields under the Voice & Fax section. Change the dropdown to TwiML Bin and select the Proxy Phone Call option in the subsequent dropdown.

Configure Twilio Phone Number to respond with the "Proxy Phone Call" TwiML bin when a call comes in

Next, scroll down to the A MESSAGE COMES IN fields under Messaging. Change the dropdown to TwiML Bin and select the Proxy SMS option in the subsequent dropdown.

Configure Twilio Phone Number to respond with the "Proxy SMS" TwiML bin when a message comes in

Finally, click the Save button on the bottom left.

Testing the phone proxy

To test out the functionality, you will have to use a secondary phone number, or ask a friend to test this with you.

Use the second phone number to call your Twilio Phone Number. The phone call will be forwarded to your private phone number and you will be able to see the second phone number, but the caller will not see your private phone number, only the Twilio Phone Number.

Use the second phone number text your Twilio Phone Number and you should receive a text message looking like this: "[their phone number]: [their text message body]"

Enhancements and security warnings

This solution works to hide a single phone number with a single Twilio proxy phone number. When you want to proxy multiple phone numbers or create proxies on-demand, you need a more scalable solution.

Twilio has got your back with Twilio Proxy. Using Twilio Proxy, you can programmatically create a proxy session between two phone numbers and let participants communicate over a proxy instead of a direct connection.

Conclusion: Use low-code Twilio tools to proxy phone numbers

In software development, there are always many ways to accomplish a task. Now, with low-code/no-code tools even more people are empowered to build applications. Using TwiML you can instruct Twilio how to respond to messages and phone calls, and with TwiML Bins you can easily host and generate TwiML using Mustache templating. By combining TwiML Bins and Twilio Phone Numbers, you created a phone proxy that hides your real phone number while being able to receive inbound texts and phone calls.

Due to Mustache's limitations, you can only generate TwiML for inbound proxying, but you could use Twilio Studio – a more powerful low-code/no-code tool – to also proxy outbound SMS and phone calls.

Additional resources

Check out the following resources for more information on the topics and tools presented in this tutorial:

Niels Swimberghe is a Belgian American software engineer and technical content creator at Twilio. Get in touch with Niels on Twitter @RealSwimburger and follow Niels’ personal blog on .NET, Azure, and web development at swimburger.net.