twiliotwilio-apitwilio-twimltwilio-functionstwilio-programmable-voice

How to use Twilio API to create a Twilio Function (or any other Twilio built-in service) that forwards incoming calls?


In my app, the users see a list of Twilio numbers.

I simply want to give the user the ability to specify a "forward number" for each of these numbers, and my app would call the Twilio API to set this up properly.

I understand that I can give each Twilio number a webhook, which returns TwiML that can facilitate the forward.

But because call forwarding is such a basic feature (the app has no needs to configure anything other than this), I was thinking, Twilio must have something built-in for me so that I don't have to host such a webhook just for call forwarding?

So I looked into the docs and found:

  1. TwiML bins: snippets of TwiML directly hosted in Twilio – but can be created by console only and not API, useless for my scenario.

  2. Twilio Studio: basically their IVR builder. Does seem to have API access. But I only need basic forward and have zero use for call flows etc, so this seems completely overkill?

  3. Twilio function: this appears to be what I want. Twilio hosted functions that do not require self-hosted webhooks. It even has built-in templates for call forwarding in the console. But how do I create and update these programmatically via API so that I can pass in the numbers to forward to? From what I can see in the runtime API, functions can be created/updated with only the basic name/sid properties, so they're essentially empty functions? I do see this FunctionVersionContent API but it seems to be read only?

I'm lost at this point. Is there an API in Twilio I can call to set up basic call forwarding on a Twilio number that's all taken care of on Twilio's side, a feature that is provided to customers of basically any phone service?


Solution

  • Figured it out from Twilio support. This can be done via query parameters with TwiML bins or Twilio Functions.

    Even though TwilML bins cannot be created/edited with API, each bin has a URL that can be used as a webhook. So defining one bin like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Dial>{{forwardTo}}</Dial>
    </Response>
    

    Then update the VoiceUrl to use https://url.to.bin?forwardTo=e164number

    This only works if numbers are provisioned in the same account as the TwiML bin. If the numbers are provisioned in subaccounts, we can use public Twilio functions instead.