How to Build a Simple IVR with Speech and Touch-Tone Input

An Interactive Voice Response (IVR) allows you to automate phone interactions by providing callers with a menu of options. While traditional IVRs rely on keypad (DTMF) input, modern systems often include Speech-to-Text (ASR) for a more natural user experience. You can refer to the Advanced IVR guide for more details.

For an overview of voice automation concepts and a comparison of all three implementation approaches, see Understanding Voice Automation.

In this guide, you will build a Node.js application that answers a call and prompts the user to either press a key or speak. The application will then repeat that input back to the caller.

Prerequisites

Initialize Your Project Folder

Before configuring your Vonage resources, create a home for your code. This ensures you have a destination for your security keys later.

mkdir simple-ivr && cd simple-ivr npm init -y npm install express body-parser

Expose Your Local Server

Vonage needs to send webhooks to your local machine. Use ngrok to expose your server:

ngrok http 3000

Note: Keep this terminal open and copy your ngrok URL. You'll need it in the next steps.

Provision Your Vonage Resources

Configure your environment using the Vonage API Dashboard.

Create a Voice Application

  1. Navigate to Applications > Create a new application.
  2. Give your application a name (e.g., Simple-IVR-Speech-DTMF).
  3. Click Generate public and private key.
  4. Move the downloaded private.key file into your simple-ivr project folder.
  5. Under Capabilities, enable Voice.
  6. Set the Answer URL to your ngrok URL with /webhooks/answer appended. Example: https://{random-id}.ngrok.app/webhooks/answer. Set the method to
    GET
    .
  7. Set the Event URL to your ngrok URL with /webhooks/events appended. Example: https://{random-id}.ngrok.app/webhooks/events. Set the method to
    POST
    .
  8. Click Generate new application at the bottom.
  1. Navigate to Phone Numbers > Buy Numbers and rent a number with Voice capabilities.
  2. Go back to Applications, select your IVR application, and link the new number to it.

Handle the Inbound Call

When a user calls your number, Vonage requests an NCCO (Call Control Object) from your Answer URL. Create a file named index.js and add the following code:

Process Speech and DTMF Input

Add the input handler to your index.js to process the payload Vonage sends back once the user interacts with the menu.

Test the IVR

  1. Start your server: node index.js.
  2. Call your Vonage number:
  • Keypad: Press 1. The IVR should say, You pressed 1.
  • Speech: Say Hello. The IVR should say, You said: Hello.

Next Steps

  1. Call Transfer: Add a connect action to connect the caller to the relevant department based on user input via phone number, SIP endpoint, or your web application using the Client SDK.
  2. Call Recording: Record and transcribe the call with record action.
  3. Voice AI: Pass user input to your AI agent to provide the caller with valuable information.
  4. Customization: Change text-to-speech voice or use the stream action in your NCCO to play pre-recorded MP3 files instead of text-to-speech.