Augment Voice Calls with Twilio Conversation Intelligence Using Node.js

September 30, 2026
Written by

Augment Voice Calls with Twilio Conversation Intelligence Using Node.js

You know how to build a voice AI agent from scratch, one that supports speech recognition, text-to-speech, turn detection, and real-time audio streaming — all at low latency. But, what if you could also analyze customer conversations in real time and collect useful data insights for future calls?

With Twilio Conversation Intelligence and Conversation Orchestrator, backed by Conversation Memory, you can!

Specifically, in this tutorial you’re going to learn how to use these three technologies together to retrieve a short summary of each customer call, and an analysis of the caller’s sentiment. What’s more, you’ll also see whether your agent followed the guidelines you set for it. All of this information will then be persisted to a SQLite database, so that you can make use of it later.

Programming language support

This tutorial is geared toward Node.js developers. If you would like to build this project in a different programming language, see the following options:

Architecture

As this tutorial adds three new technologies to the previous application, here’s a quick overview of how the new functionality works.

You will add a new route which receives a POST (webhook) request from Twilio after customer calls end. The request body will be a JSON string that contains, among other things, a short summary of the call, an assessment of the caller’s sentiment, and how the agent adhered to a series of criteria. That information will be extracted from the request and then persisted to the application’s SQLite database.

You’re not going to do more with the received information. But, there are links at the end of the tutorial showing how you could continue building on the changes made in this tutorial, should you want to.

Prerequisites

To follow along with the tutorial, you will need the following:

Build the app

Step 1: Set up Conversation Orchestrator and Conversation Memory

Before you can set up Conversation Intelligence, which does most of the work, you need to create a Memory Store and Conversation Configuration.

To do that, sign in to the Twilio Console, and go to Products & Services > Conversation Orchestrator > Conversation configurations. There, click Create a Conversation configuration. On the Name Configuration step, enter a name and description, then click Next.

Twilio Console setup page for naming a new conversation configuration with fields for name and description.

On the Messaging and chat traffic step, click Next. On the Voice traffic step, scroll down and enable the Set up automatic capture checkbox. From the Voice phone numbers list, select your Twilio phone number, then click Next.

Now, on the Configure lifecycle step click Next. After that, on the Enable Conversation Memory step, create a memory store, by clicking Create new memory store, entering a name in the Memory store name field, and clicking Save.

Screenshot of the setup conversations and profiles interface with options to create a memory store.

From the Memory store list, select the memory store you just created, leave Turn on observations and summaries enabled, and click Next.

Finally, on the Summary step, review your settings and click Create Conversation configuration. Copy the conversation configuration ID for use later.

Step 2: Set up Conversation Intelligence

Before you can complete this step, you need to make the application publicly accessible on the internet, as you’ll need the ngrok URL later in this section. In a new terminal window, run the command below to create a connection to the app on port 8000.

ngrok http 8000
If your Owl Air agent listens on a port other than 3000, replace 8000 with that port number. Keep this terminal window open for the rest of the tutorial. If you restart ngrok, you may get a new URL, and you'll need to update the webhook URL you're about to set.

Next, go to Products & Services > Conversation Intelligence > Intelligence configurations. There, click Create Intelligence configuration. Add a name, description, and, in the Attach Conversation configurations section, select the name of the Conversation configuration that you created in the previous step, and click Submit.

With that done, in Intelligence configurations, click Create rule next to the Intelligence configuration which you just created. Then, in the Add language operators section, enable Sentiment, Summary, and Script-Adherence, and click Next.

Now, in the Script-Adherence section, at the bottom of the Set Parameters step, add the following text into the script field and click Next.

Category: introduction
- introduction: The agent should identify themselves by first name. Required Phrase: Thanks for calling Owl Air! I'm Hoot.
Category: assistance
- offer_assistance: I can help with flight status, baggage policy, loyalty points, or booking changes. Which of those can I help you with?
Interface displaying script adherence rule setup with script, version, and navigation options.

Now, on the Trigger and action step, choose At conversation end in the Trigger section. Then, in the Action section, paste your ngrok Forwarding URL plus “/intelligence-results” in the Webhook action field (for example, https://1234abcd.ngrok.app/intelligence-results). Click Next.

Screenshot of rule creation screen with options to add language operators, set parameters, and enable conversation memory.

In the Add context step, scroll down to the Conversation Memory section and enable Enable Conversation Memory for this rule and click Next. In the Summary step, click Create rule.

Step 3: Update the existing project structure

Now, it’s time to start augmenting the Node.js code. But, before you can do that, you have to add a few new directories. In your terminal, change into the root directory of your Owl Air project (the directory containing package.json), and run the following command.

mkdir -p data/database handlers services types/operator-results
If you're using Microsoft Windows, the -p option is not required.

The data/database directory will store the application’s SQLite database and a SQL file defining the database’s schema. The handlers directory will contain the code that handles the webhook request. The services directory will contain helper code for talking to the database and to Twilio. The types/operator-results directory will contain a series of plain JavaScript classes which will store and model the information extracted from the webhook received from Twilio; you’ll create those classes shortly.

Step 4: Set up the application’s database

Create a new file named dump.sql in the data/database directory and paste the following SQL into that file.

-- Enable SQLite's WAL mode
PRAGMA journal_mode=WAL;
-- Enable foreign key support
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS intelligence_results
(
    'conversation_id' TEXT NOT NULL PRIMARY KEY,
    'call_started' TEXT NOT NULL,
    'call_ended' TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS operators
(
    'operator_id' INTEGER PRIMARY KEY,
    'operator_type' TEXT NOT NULL,
    'operator_value' TEXT NOT NULL,
    'conversation_id' TEXT NOT NULL,
     FOREIGN KEY(conversation_id) REFERENCES intelligence_results(conversation_id)
);
CREATE TABLE IF NOT EXISTS operator_script_adherence_categories
(
    'operator_id' INTEGER NOT NULL,
    'category_name' TEXT NOT NULL,
    'category_met' INTEGER NOT NULL DEFAULT false,
     FOREIGN KEY(operator_id) REFERENCES operators(operator_id)
);

The instructions:

  • Enable SQLite’s WAL (Write-Ahead Logging) mode (which, among other benefits, significantly improves performance)
  • Enable foreign key support
  • Define three tables:
  • intelligence_results: stores the core information about the conversation
  • operators: stores the information extracted by Conversation Intelligence, such as the call summary and sentiment. The operator_id column gives each record a unique, auto-incrementing ID
  • operator_script_adherence_categories: stores the script adherence information, linking it to the relevant record in operators through its operator_id

It’s not the most sophisticated schema, but it can store the information in a maintainable way.

Now, use SQLite’s Command-Line Shell (or your preferred database management tool) to provision the database with the following command.

sqlite3 data/database/database.sqlite3 < data/database/dump.sql

The command prints wal, confirming that WAL mode is enabled, and creates the database file data/database/database.sqlite3.

The database file will contain details of your customers' calls. Add *data/database/database.sqlite3** to your project's *.gitignore* file so that you don't commit it to version control.

Step 5: Create the Conversation Intelligence route

Add the required packages

The application needs two extra packages: better-sqlite3, to simplify interacting with the application’s SQLite database, and the Twilio Node.js Helper Library, to query Twilio’s Conversations API. It also uses dotenv to load your credentials from a .env file. To install them, run the following command in your terminal, from the root directory of your project.

npm install better-sqlite3 twilio dotenv

If your project already has twilio or dotenv installed, npm updates them to the latest version.

Create a route and handler for processing the webhook

Next, create the handler which processes the webhook request. In the handlers directory, create a new file named intelligence-results-handler.js. You’ll add the code to this file in two parts.

First, paste the code below into the file.

const { Summary, Sentiment, Category, ScriptAdherence } = require('../types/operator-results');
const importOperator = (operatorName, conversationId, data = {}) => {
  switch (operatorName) {
    case 'Sentiment':
      return new Sentiment(data.result?.label ?? '', conversationId);
    case 'Script-Adherence': {
      const categories = (data.result?.categories ?? []).map(
        (category) =>
          new Category(
            category.category_key ?? '',
            category.criteria?.criteria_met === 'Succeeded'
          )
      );
      return new ScriptAdherence(data.parameters?.script ?? '', conversationId, categories);
    }
    case 'Summary':
      return new Summary(data.result?.text ?? '', conversationId);
    default:
      return null;
  }
};

The importOperator() function converts one operator result from the webhook into an object. It instantiates a Sentiment object from the result.label element, a Summary object from the result.text element, and a ScriptAdherence object from the result.categories element. Each category is marked as met when its criteria_met value is Succeeded. If Conversation Intelligence sends an operator that the app doesn’t know about, the function returns null so the operator can be skipped.

The ?. (optional chaining) and ?? (nullish coalescing) operators protect the code from missing data. For example, data.result?.label ?? '' returns an empty string instead of throwing an error if result or label is missing.

Now, paste the code below at the bottom of the same file, after the importOperator() function.

const createIntelligenceResultsHandler = ({ twilioClient, dbService }) => {
  return async (req, res) => {
    const data = req.body ?? {};
    const conversationId = data.conversationId ?? '';
    const operatorResults = data.operatorResults ?? [];
    const operators = operatorResults
      .map((operatorData) =>
        importOperator(operatorData.operator?.displayName, conversationId, operatorData)
      )
      .filter((operator) => operator !== null);
    try {
      const conversation = await twilioClient.conversations.v2
        .conversations(conversationId)
        .fetch();
      dbService.recordCall(
        conversationId,
        conversation.createdAt,
        conversation.updatedAt,
        operators
      );
    } catch (err) {
      console.error('Failed to record intelligence results', {
        conversationId,
        error: err.message,
      });
    }
    res.json('Log Data Received');
  };
};
module.exports = { createIntelligenceResultsHandler };

The createIntelligenceResultsHandler() function is the central focus of the file. It receives the Twilio client and the database service as arguments, and returns an Express route handler that uses them. Passing dependencies in this way, instead of creating them inside the handler, keeps the handler small and makes it easy to swap in test versions later.

The returned handler is called when the “/intelligence-results” route is requested. It starts off by reading the JSON request body, which Express has already parsed into a JavaScript object, before progressively extracting the essential information from it. This is the conversation ID (the conversation’s unique identifier), and the details that Conversation Intelligence determined about the call, contained in the operatorResults element, using the importOperator() function.

With the relevant information collected, the handler makes a call to the Conversations (v2) API to get the start and end time of the call (details which aren’t available in the received webhook data). The Twilio Node.js Helper Library returns these as JavaScript Date objects in the createdAt and updatedAt properties. Then, using the DatabaseService’s recordCall() function, the collated information is persisted to the SQLite database. If anything goes wrong, the error is logged to the terminal.

Finally, the handler responds to Twilio with a JSON response, confirming that the webhook was received.

Create the plain JavaScript classes

Now, it’s time to create the classes to store the Conversation Intelligence data. Start off by creating a file named summary.js in types/operator-results, and paste the code, below, into the file.

class Summary {
  constructor(summary, conversationId) {
    this.summary = summary;
    this.conversationId = conversationId;
  }
  getValue() {
    return this.summary;
  }
}
module.exports = { Summary };

Then, create a file named sentiment.js in types/operator-results, and paste the code, below, into the file.

class Sentiment {
  constructor(sentiment, conversationId) {
    this.sentiment = sentiment;
    this.conversationId = conversationId;
  }
  getValue() {
    return this.sentiment;
  }
}
module.exports = { Sentiment };

Create another file, this time named script-adherence.js in types/operator-results, and paste the code, below, into the file.

class ScriptAdherence {
  constructor(summary, conversationId, categories = []) {
    this.summary = summary;
    this.conversationId = conversationId;
    this.categories = categories;
  }
  getValue() {
    return this.summary;
  }
}
module.exports = { ScriptAdherence };

The categories property holds a list of Category objects, one for each category in the script. Create a file named category.js in types/operator-results, and paste the code, below, into the file.

class Category {
  constructor(name, met) {
    this.name = name;
    this.met = met;
  }
}
module.exports = { Category };

Finally, create a file named index.js in types/operator-results, and paste the code, below, into the file.

const { Summary } = require('./summary');
const { Sentiment } = require('./sentiment');
const { ScriptAdherence } = require('./script-adherence');
const { Category } = require('./category');
module.exports = { Summary, Sentiment, ScriptAdherence, Category };

This file gathers all four classes in one place. When other files call require('../types/operator-results'), Node.js loads this index.js file automatically, so they can import every class with a single line.

JavaScript doesn’t have interfaces, so there’s no equivalent of a shared interface that the classes must implement. Instead, Summary, Sentiment, and ScriptAdherence each provide a getValue() method. This makes it simpler to work with them in DatabaseService, which you’ll create shortly, as it can call getValue() on any of them without checking which class it is.

Update the application’s routing table

Next, you need to register the new route with Express. To do that, open your project’s index.js file in the root directory.

If the file doesn’t already load environment variables with dotenv, add the following line as the very first line of the file.

require('dotenv').config();

Then, add the following require statements near the top of the file, below your existing require statements.

const path = require('path');
const { createIntelligenceResultsHandler } = require('./handlers/intelligence-results-handler');
const { DatabaseService } = require('./services/database-service');
const { createTwilioClient } = require('./services/twilio-client');

If your index.js file already has a const path = require('path'); line, don't add it a second time.

You’ll create the database-service.js and twilio-client.js files in the next two sections.

Now, add the code below after the line where you create your Express app (const app = express();), and above the app.listen() call.

const twilioClient = createTwilioClient();
const dbService = new DatabaseService(
  path.join(__dirname, 'data', 'database', 'database.sqlite3')
);
app.post(
  '/intelligence-results',
  express.json(),
  createIntelligenceResultsHandler({ twilioClient, dbService })
);

This code creates the Twilio client and the database service once, when the app starts, and passes them to the handler. The path.join() function builds the full path to the SQLite database file, so it’s found no matter which directory you start the app from.

The new route accepts only POST requests to the “/intelligence-results” endpoint, passing requests through the express.json() middleware (which parses JSON request bodies into req.body) and then to the handler. Because express.json() is added to this route only, it doesn’t change how your existing routes, such as the one that returns TwiML, process their requests.

Create the database service

It’s time to create the database service which the handler uses to simplify persisting the retrieved webhook data into the application’s database. In services, create a file named database-service.js, and paste the code below into the file.

const Database = require('better-sqlite3');
const { ScriptAdherence, Sentiment, Summary } = require('../types/operator-results');
const formatDate = (date) => date.toISOString().slice(0, 19).replace('T', ' ');
const getOperatorType = (operator) => {
  if (operator instanceof ScriptAdherence) return 'script-adherence';
  if (operator instanceof Summary) return 'summary';
  if (operator instanceof Sentiment) return 'sentiment';
  throw new Error(`Unsupported operator: ${operator.constructor.name}`);
};
class DatabaseService {
  constructor(databasePath) {
    this.db = new Database(databasePath);
    this.db.pragma('foreign_keys = ON');
    this.insertIntelligenceResult = this.db.prepare(`
      INSERT INTO intelligence_results (conversation_id, call_started, call_ended)
      VALUES (@conversationId, @callStarted, @callEnded)
    `);
    this.insertOperator = this.db.prepare(`
      INSERT INTO operators (conversation_id, operator_value, operator_type)
      VALUES (@conversationId, @operatorValue, @operatorType)
    `);
    this.insertCategory = this.db.prepare(`
      INSERT INTO operator_script_adherence_categories (operator_id, category_name, category_met)
      VALUES (@operatorId, @categoryName, @categoryMet)
    `);
  }
  recordCall(conversationId, startedAt, endedAt, operators) {
    const record = this.db.transaction(() => {
      this.insertIntelligenceResult.run({
        conversationId,
        callStarted: formatDate(startedAt),
        callEnded: formatDate(endedAt),
      });
      for (const operator of operators) {
        const { lastInsertRowid: operatorId } = this.insertOperator.run({
          conversationId,
          operatorValue: operator.getValue(),
          operatorType: getOperatorType(operator),
        });
        if (operator instanceof ScriptAdherence) {
          for (const category of operator.categories) {
            this.insertCategory.run({
              operatorId,
              categoryName: category.name,
              categoryMet: category.met ? 1 : 0,
            });
          }
        }
      }
    });
    record();
  }
}
module.exports = { DatabaseService };

Here’s what the code does:

  • The formatDate() helper function converts a JavaScript Date into a string such as 2026-09-18 01:10:41, which is how the call start and end times are stored.
  • The getOperatorType() helper function returns the value stored in the operator_type column, based on which class the operator is an instance of.
  • The constructor opens the SQLite database, turns on foreign key support for the connection, and creates three prepared statements, one for each table. A prepared statement is an SQL query with named placeholders, such as @conversationId, which better-sqlite3 fills in with values when you call run(). Using placeholders, instead of building SQL strings by hand, protects the database from SQL injection.
  • The recordCall() function inserts one record into intelligence_results, then one record into operators for each operator. For the ScriptAdherence operator, it uses the ID of the newly inserted operator record (lastInsertRowid) to link each category to it in operator_script_adherence_categories.
SQLite has no boolean type, and better-sqlite3 can't store JavaScript true and false values directly. That's why category.met ? 1 : 0 converts each category's met value into 1 or 0 before it's saved.

All of the inserts are wrapped in a transaction, using this.db.transaction(). This means that either every record for the call is saved, or, if something goes wrong part way through, none of them are. That way, your database never contains half of a call’s results.

You may notice that recordCall() isn’t an async function. better-sqlite3 runs its queries synchronously, which keeps the code short. For a local SQLite database, the queries finish quickly enough that this won’t slow down your voice agent.

Create a Twilio REST client

Now, you need to create a Twilio REST client, as the application will need it to query the Conversations (v2) API to retrieve a conversation’s start and end time in the handler. To do that, in services, create a file named twilio-client.js, and in that file paste the code below.

const twilio = require('twilio');
const createTwilioClient = () => {
  return twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
};
module.exports = { createTwilioClient };

The createTwilioClient() function creates a new Twilio client with your Account SID and Auth Token, which are read from environment variables. You’ll add these values to your .env file in the next step.

Step 6: Start the application

With the code now complete, you need to make sure that the application has all of the credentials it needs. Open the .env file in the root directory of your project (or create it, if it doesn’t exist), and make sure it contains the following four environment variables.

TWILIO_ACCOUNT_SID=XXXXXX
TWILIO_AUTH_TOKEN=XXXXXX
DOMAIN=XXXXXX
OPENAI_API_KEY=XXXXXX

Replace each XXXXXX placeholder with its respective value:

  • TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN: your Twilio Account SID and Auth Token, which you can find in the Account Info section of the Twilio Console
  • DOMAIN: your ngrok Forwarding URL, which your Owl Air agent uses to build its WebSocket URL
  • OPENAI_API_KEY: your OpenAI API key, from the OpenAI dashboard

If your Owl Air agent already uses different names for any of these values, keep the names that the rest of your code expects.

Save the file. Then, start the application by running the command below in your terminal, from the root directory of your project.

node index.js

You will see output similar to the example below, after the application starts. The exact message depends on the console.log() call in your existing app.listen() code.

Server listening on port 3000

Test that the app works as expected

With the application running, call your Twilio phone number. You should hear Hoot’s greeting within a second or two of the call connecting:

“Thanks for calling Owl Air! I’m Hoot. I can help with flight status, baggage policy, loyalty points, or booking changes. Which of those can I help you with?”

Then, like when you tested the first version of the app, try a few test questions to verify the full flow is working:

  • “What’s the baggage policy?”: Hoot should describe carry-on and checked bag rules in natural spoken language.
  • “How do loyalty points work?”: Hoot should explain the earn and redemption rates.
  • “Can I change my flight?”: Hoot should give the change fee policy, with amounts spelled out in words.
If you hear an error message on the call or see an error message in your terminal logs, check the error code against the Conversation Relay error code reference in the Twilio documentation.

After the call finishes, using your database tool of choice, have a look at the records in the application's database. There, you should see a summary of the conversation, along with the related operator information.

Conclusion

You’ve now learned how to use Conversation Intelligence, Conversation Orchestrator, and Conversation Memory, as well as the Conversations API (V2), to retrieve a short summary of each call and an analysis of the caller’s sentiment, and persist the information to a SQLite database, so that you can make use of it later. What’s more, you also know whether your agent followed the guidelines you set for it.

But don’t stop there! Now that the application can store conversation information, why not add a route for viewing a summary of all stored conversations, and one for viewing individual conversation details?

Then, I strongly encourage you to learn more about Conversation Intelligence, Conversation Orchestrator, and Conversation Memory, as well as the Conversations API (V2).

Dhruv Patel is a Developer on Twilio’s Developer Voices team. You can find Dhruv working in a coffee shop with a glass of cold brew or he can be reached at dhrpatel [at] twilio.com.