Forwarding SMS and voice calls to an alternate phone number
Tags: twilio, serverless, javascript, sms
Published: 2020-04-13 20:45:03

---
canonical_url: https://www.twilio.com/blog/forwarding-sms-phone-calls-alternate-number
---

Balancing privacy and convenience can be tricky. Today I’ll show you how to forward messages and phone calls with Twilio so that you can give out a phone number where people can reach you, while keeping your real phone number a secret.

There are lots of reasons you might want to hide your real phone number. For example:

- Transacting with internet strangers such as buying/selling on Craigslist
- Giving delivery folks a number to reach you
- Putting a phone number on your business card

## Prerequisites to create a forwarding number

- A Twilio account - [sign up for a free one here](http://www.twilio.com/referral/vEfhDc)
- [A Twilio phone number](https://www.twilio.com/docs/conversations/quickstart#sign-up-for-twilio-and-provision-your-first-sms-enabled-number) with SMS and voice capabilities
- A phone number you want to forward calls and messages to. For the rest of this post, we’ll call it your cell phone number to simplify things but it can be any number with SMS and voice capabilities.
- A friend with a SMS-enabled phone number to help you test things out

## Forwarding phone calls with Twilio Studio

[Twilio Studio](https://www.twilio.com/docs/studio) is a visual application builder that allows you to write conditional logic for handling messaging flows without writing any code.

Head to the [Studio Dashboard](https://www.twilio.com/console/studio/dashboard). Click the “+” button to create a new Studio Flow starting from scratch. Let’s call it “message forwarding.”

![Screenshot of the "New Flow" dialog box in Twilio Studio. The "Flow Name" input box has the text "message forwarding".](https://dev-to-uploads.s3.amazonaws.com/i/aam8fden2wncae75hjwn.png)

From the Widget Library on the right-hand side, drag a “Connect Call To” widget on to the canvas. Connect it to the Incoming Call trigger.

![Screenshot of Twilio Studio flow. A "Connect Call To" widget is connected to the "Incoming Call" trigger.](https://dev-to-uploads.s3.amazonaws.com/i/uxhxz12ucw9cihmsarsj.png)

Configure the widget to connect the call to a single number and type your cell phone number in the box.

![Screenshot of "Connect Call To" widget configuration. The "Connect Call To" dropdown has "Single Number" selected, and the input box below contains a fake cell phone number.](https://dev-to-uploads.s3.amazonaws.com/i/2jeu912s0pxq8m7iuzby.png)

Save the widget configuration, and then click `Publish` at the top.

Go to the [Phone Numbers dashboard](https://www.twilio.com/console/phone-numbers/). Under “Voice & Fax”, configure your Twilio phone number to use the `message forwarding` Studio Flow you’ve just created.

![Screenshot of Twilio phone number configuration. Under "Voice & Fax", the "Configure With" dropdown has "Webhooks, TwiML Bins, Functions, Studio, or Proxy." Under "A Call Comes In," "Studio Flow" and "message forwarding" are selected.](https://dev-to-uploads.s3.amazonaws.com/i/ag8cgstwvu4bqmco1zt0.png)

Grab a friend. Ask them to call your Twilio phone number to test things out. ☎️ 🎉

## One-way SMS forwarding with Twilio Studio

There are [a few ways of doing one-way SMS forwarding with Twilio](https://www.twilio.com/blog/handling-your-business-calls-and-texts-like-a-boss-4). Since we’ve already got a Studio Flow going, let’s build on top of that.

Drag a `Send Message` widget onto the canvas. Connect it with the `Incoming Message` trigger.

Configure the widget so that it sends a message to your cell phone number, from your Twilio phone number. The message body should be:

```bash
From: {{trigger.message.Body}} : {{widgets.send_message_1.inbound.Body}} 
```

![Screenshot of configuration for Twilio Studio "Send Message" widget. Send Message From has a fake Twilio number in the input box, and "Send Message To" has a fake cell phone number in the input box.](https://dev-to-uploads.s3.amazonaws.com/i/cowpczzjb5g2fjia8gg2.png)

![Screenshot of Twilio Studio flow. The "Send Message" widget is connected to the "Incoming Message" trigger. In the "Send Message" widget configuration, the Message Body is `"from {{trigger.message.Body}}: {{widgets.send_message_1.inbound.Body}}"`](https://dev-to-uploads.s3.amazonaws.com/i/u48jnh61ce1xd59s0ock.png)

Don’t forget to publish the changes to your Flow. Ask your friend to help you test by sending a text message to your Twilio number.

## Two-way SMS forwarding with Twilio Functions

Let’s say you want to be able to *reply* to incoming messages from your cell phone. Using Twilio Functions, we’ll write some code to accomplish that.

In order to run this Function, you need 3 pieces of information:
- Your cell phone number
- The third-party phone number you want to exchange messages with
- The Twilio phone number serving as the gateway in the middle

We’ll reconfigure the same Twilio number we were using with Studio earlier to call the Twilio Function whenever a new SMS comes in. Inside the Function, there’s some logic to determine which direction to forward the information.

![A hand-drawn diagram. On the left, a pink anthrophomorphic cell phone labeled "Your Cell Phone." In the middle, a smiling blue circle labeled "Twilio Number." Underneath that, a blue box labeled "Twilio Function." On the right, a purple anthrophomorphic cell phone labeled "3rd Party." There are arrows flowing from Your Cell Phone to/from Twilio Number, from Twilio Number to/from Twilio Function, and from Twilio Number to/from 3rd Party.](https://dev-to-uploads.s3.amazonaws.com/i/kmkb7bk1cok6s2khilg6.png)

How do you get the third-party phone number? You can leave the Studio Flow we created earlier hooked up. When a message comes in you want to reply to, copy the phone number out of the message body and drop it into the code below.

On the [Functions dashboard](https://www.twilio.com/console/functions), create a new Function called `forward SMS`. Copy the following code into it:

```javascript
exports.handler = function (context, event, callback) {
    const yourPhoneNumber = ''; // replace this
    const thirdPartyPhoneNumber = ''; // replace this
    const twilioPhoneNumber = event.To;

    let twiml = new Twilio.twiml.MessagingResponse();
    if (event.From === yourPhoneNumber) {
      twiml.message(event.Body, {
        to: thirdPartyPhoneNumber,
        from: twilioPhoneNumber
      });
    } else {
      twiml.message(`${event.From}: ${event.Body}`, {
        to: yourPhoneNumber,
        from: twilioPhoneNumber
      });
    }
    callback(null, twiml);
  }
```
Save your Function -- it’ll deploy automatically. [Configure your Twilio phone number](https://www.twilio.com/console/phone-numbers/) so that this Function is called when an incoming message arrives.

![Screenshot of Twilio phone number configuration. Under "Messaging", the "Configure With" dropdown has "Webhooks, TwiML Bins, Functions, Studio, or Proxy" selected. Under "A Message Comes In," "Function" and "forward SMS" are selected.](https://dev-to-uploads.s3.amazonaws.com/i/a43hqmt1ib3kgzukvffy.png)

Ask your friend to send you a SMS. Send them one back, and verify that they received it. Offer to buy them a beverage of their choice to say thanks for the help. ☕

One drawback of this approach is that you’re limited to one conversation at a time. Since the 3rd party phone number is hard coded, it needs to be changed each time you want to converse with a different person. If you’re a high roller who needs to juggle multiple simultaneous conversations with strangers, you could [add some conditional logic to specify the number you want to send a message to](https://www.twilio.com/blog/sms-forwarding-and-responding-using-twilio-and-javascript).

## Wrapping it up

In this blog post you’ve learned a few ways of forwarding messages using Twilio Studio and Functions. Go forth and distribute your phone number to the world, more safely.

If you have any questions or issues, feel free to send me a SMS at +1 970-238-3818. You can also reach me on Twitter at [@annthurium](https://twitter.com/annthurium).