How to integrate a Voice API with use cases and sample code

An image showing a telephone received with less than greater than symbols to indicate interaction

In the previous post on getting started with Voice API, we showed you a side-by-side comparison of Voice API vs SIP trunking and how you can go global with the right Voice API backed by a global network. 

It’s time to move on to the good stuff—use cases and sample code. Bandwidth customers have implemented and scaled interesting voice customer experiences using the Bandwidth Voice API. What’s unique here? This easy-to-integrate and scale Voice API sits on an owned-and-operated network with global reach.

In this post, we demonstrate an inbound and an outbound Voice chat API use case with sample code to get your voice innovation kicked off. 

If you have trouble following, get help from our API experts. Don’t hesitate to lean into our award-winning customer support. 

Inbound voice use case: IVR

Interactive Voice Response (IVR) systems have revolutionized customer experience (CX) across industries. Industry research indicates that IVR self-service achieves a First Call Resolution (FCR) rate of 74%, higher than other contact channels. Additionally, when customers experience FCR through IVR self-service, their satisfaction (CSAT) is 30% higher compared to non-FCR calls[1]. Further, improving IVR containment rates—where customers’ issues are resolved without human intervention—by 5% to 20% can reduce call center costs by 10% to 30%, yielding a solid return on investment[2].

Investing in IVR technology is not simply an upgrade, it is a strategic way to build strong customer relationships and provide excellent customer service. You can use the Voice API to build easy-to-navigate IVR experiences, elevating customer satisfaction and operational efficiency in one go.

Considerations before building

Before implementing a voice API solution for IVR, it’s crucial to consider the specific needs and goals of your use case. Here’s a quick checklist of considerations to build a sensible IVR.

IVR building checklist

Call flow considerations:

  • What are the primary objectives and desired outcomes of implementing an IVR system?
  • Is the intent to have a human pick up, and if not, should it route to a voicemail? 
  • In the event of a call overflow, how will customers be redirected to alternative contact points or specific teams?

Customer experience considerations:

  • How will the call flow be structured to ensure a seamless and intuitive customer journey?
  • What is the desired customer interaction when a human agent is not available? Should calls route to a voicemail or an alternative contact method?
  • How can the IVR experience be optimized to reflect the brand’s voice and provide personalized interaction for callers?

IVR flow: Sample code

Got your IVR plan down? It’s time for action. Below, we show you a few IVR call flow examples with code snippets to integrate the Bandwidth Voice API. 

For more developer support, dig into Bandwidth developer docs or get help from an expert

Inbound auto-answer

Let’s try setting up an auto-answer for the customer when they call a non-voice number. We’ll aim to hit them with an automated message of Number is not voice capable, please text us.”

You can do this via text-to-speech (example below) or by playing an audio file (potentially a more branded experience).

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <SpeakSentence voice="ruth">Hello! This number is text-only. We've sent you an SMS with a link. Please click on the link to tell us the issue you're facing and we'll have the right team contact you..</SpeakSentence>
</Response>

Inbound call tree

An inbound call tree is how you traditionally visualize IVR. Below are the quick steps to build this call tree with the Bandwidth voice calling API.

Initial BXML will include a <Gather> verb. One of the primary aspects of this verb is the gatherUrl. Here,  Bandwidth will send the digit (or digits) that were pressed. Based on that information, your application will respond with new BXML telling Bandwidth what to do next.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
   <Gather gatherUrl="https://gather.url/nextBXML" firstDigitTimeout="10" terminatingDigits="#">
      <SpeakSentence voice="ruth">Hello! Press 1 for an order status. Press 2 to update or cancel your order. Press 3 to talk to Customer Care. Press 0 to hear your options again.</SpeakSentence>
   </Gather>
</Response>

POST http://yourUrl.example/gather
Content-Type: application/json

{
	"eventType"        : "gather",
	"eventTime"        : "2019-06-20T15:56:11.554Z",
	"accountId"        : "55555555",
	"applicationId"    : "7fc9698a-b04a-468b-9e8f-91238c0d0086",
	"from"             : "+15551112222",
	"to"               : "+15553334444",
	"direction"        : "outbound",
	"callId"           : "c-95ac8d6e-1a31c52e-b38f-4198-93c1-51633ec68f8d",
	"callUrl"          : "https://voice.bandwidth.com/api/v2/accounts/55555555/calls/c-95ac8d6e-1a31c52e-b38f-4198-93c1-51633ec68f8d",
	"startTime"        : "2019-06-20T15:54:22.234Z",
	"answerTime"       : "2019-06-20T15:54:25.432Z",
	"digits"           : "2"
}

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Transfer transferCallerId="+11234567891">
        <PhoneNumber>+11234567892</PhoneNumber>
    </Transfer>
</Response>

Inbound voicemail

Another IVR variation is a voicemail box solution. You can use our BXML verbs to build a voicemail box solution with the Bandwidth phone calling API.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <SpeakSentence voice="ruth">Please leave your message after the beep.</SpeakSentence>
    <PlayAudio>http://audio.url/beep.wav</PlayAudio>
    <Record recordCompleteUrl="https://myapp.com/nextBXML" maxDuration="60"/>
</Response>

Inbound call tracking (call forwarding or transferring)

Here’s one way to achieve call tracking with the Bandwidth voice call API. 

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Transfer transferCallerId="+11234567891">
        <PhoneNumber>+11234567892</PhoneNumber>
    </Transfer>
</Response>

We’ve explored more approaches to call tracking with sample codes here in detail. If in doubt, what works best for your business, you can always just talk to our experts

Outbound voice use case: Voice notifications

Customers expect high touch from brands, even if you can’t man all the posts. Outbound voice notifications come in handy here. A personalized reminder here about an upcoming appointment or a callback about test results there makes the patient or customer recipient feel seen and valued. 

Voice notifications through Voice call API have many applications to help the business appear proactive and personalized—crucial updates, appointment reminders, delivery notifications, and more, optimizing engagement and customer satisfaction levels. The Medical Group Management Association reports that 88% of healthcare leaders use automated appointment reminders, leading to benefits such as reduced no-show rates and better appointment utilization[3]

Considerations before building

While Voice API is all for personalization, some critical considerations while building voice notifications help you protect your bottom line while scaling. 

Voice notifications implementation checklist

Pre-build considerations:

  • Are you adhering to global regulations by the number type and/or market?
  • STIR/SHAKEN and spam—are you placing calls from numbers on your account with your carrier or on behalf of an external number?

Customer experience considerations:

  • What is your audience size and how fast do you need to reach them?
  • Do you need to leverage voicemail detection or an alternative?
  • Is there an action to be completed by the call recipient when they get the voice notification? 

Outbound voice notifications: Sample code

There’s simple enough code to implement this. For more developer support, dig into Bandwidth developer docs or get help from an expert

Outbound  voice notifications (using Voicemail Detection)

There’s simple enough code to implement this. For more developer support, dig into Bandwidth developer docs or get help from an expert.

POST https://voice.bandwidth.com/api/v2/accounts/{accountId}
{
    "from": "{FROM_NUMBER}",
    "to": "{TO_NUMBER}",
    "answerUrl": "http://example.com/answer",
    "username": "{BANDWIDTH_USERNAME}",
    "password": "{BANDWIDTH:PASSWORD}",
    "machineDetection": {
        "mode": "async",
        "detectionTimeout": 15,
        "silenceTimeout": 10,
        "speechThreshold": 10,
        "speechEndThreshold": 5,
        "delayResult": false,
        "callbackUrl": "http://example.com/machineDetectionCallback",
    }
}

Find more developer insight into voicemail detection here.

Innovate with a global Voice API with award-winning support

Looking for reasons to switch to a Voice API that delivers on your business goals? We can give you a few to check out Bandwidth’s voice calling API:

  • You get competitive pricing, especially for high-volume cases for businesses relying on heavy calling needs
  • Developers know our Voice API to be easy to integrate with a more intuitive API structure.
  • You get to market faster with greater flexibility in terms of call routing, SIP trunking, and telephony features. If you need more customizable solutions. Bandwidth’s APIs offer a high level of configurability, allowing for more tailored use cases.
  • You have greater control over your carrier integrations and call data. Bandwidth owns and operates its own network, reaching 65+ countries. Our direct-to-PSTN connection in 38 countries brings you better call quality and reliability in key markets and cuts out the middlemen in pricing. 
  • Bandwidth’s universal platform combines dynamic communication APIs, the most direct-to-carrier integrations in the universe, and automation and analytics to manage it all in one place. 

Whether you’re looking to consolidate voice and messaging APIs with one provider, or making the initial shift from SIP to programmable voice, our experts are standing by to answer your questions.

Request a free trial Talk to an expert