How it works
At a high level, the Twilio Media Stream integration with GenerativeAgent works by streaming audio and managing conversations through your Twilio Functions:- Stream the audio to GenerativeAgent through Twilio Media Stream.
- GenerativeAgent handles the conversation using the audio stream and responds to the caller.
Since calls remain within your Twilio infrastructure throughout the interaction, you maintain full control over call handling, including error scenarios and transfers.
- Return control back to your Twilio flow when:
- The conversation is successfully completed
- The caller requests a human agent
- An error occurs
- Retrieve the call transfer state after the conversation ends when GenerativeAgent ends the call.
If the caller hangs up, there is no call transfer state to retrieve.
Detailed Integration Flow
Detailed Integration Flow
Obtain a Twilio Media Stream URL
Initiate Bidirectional Media Stream to Media Gateway
asapp_callTransferId
- the call transfer IDasapp_externalConversationId
- the external conversation IDcustomerId
- the customer ID for the conversationtaskName
- used to enter a specific task- Additional values are passed as inputVariables for the GenerativeAgent conversation.
Retrieve Call Transfer Result
asapp_callTransferId
from the previous step:- Status: Indicates if the call was completed successfully (ACTIVE, ONGOING, COMPLETED, or EXPIRED)
- outputContext: Contains the conversation results and transfer variables
- transferType: Either
AGENT
(transfer to human agent) orSYSTEM
(system transfer) - referenceVariables: Context information about the customer and conversation
- transferVariables: Data to pass to the next system or agent
- transferType: Either
Before you Begin
Before using the GenerativeAgent integration with Twilio, you need:- Get your API Key and Secret
- Ensure your API key has been configured to access GenerativeAgent APIs. Reach out to your ASAPP team if you need access enabled.
- Have a Twilio account with:
- Access to Twilio Functions and Assets
- A phone number for testing
- Administrator permissions to configure phone numbers and functions
Your Infrastructure, Your Control
This integration keeps calls within your Twilio infrastructure throughout the interaction. You maintain full control over call handling, including error scenarios and transfers. You may need to implement your own logic to handle the call transfer state and output context. This integration walks you through setting up Twilio Functions and provides the code you need to get started. You can adapt, modify, or replace any part of this implementation to match your organization’s specific requirements and business logic.Step 1: Create and Configure Your Twilio Function
You need to create a Twilio Function that will handle the integration with GenerativeAgent. This function will authenticate with ASAPP, obtain a media stream URL, and configure the audio streaming.Select or create a service for the Function
Create a new service
Create a new service
Navigate to Functions and Assets
Functions and Assets
tool in your Twilio Account Dashboard.
From Twilio Account Home, navigate to your Account Dashboard sidebar > Explore products > Developer Tools.Add the Functions and Assets to your sidebar
Functions and Assets
tool and click on the “pin to sidebar” icon.
Create a new service
Create Service
button.Name the service
GenerativeAgentService
, and click Next
.
Create a function
Create your Function
button or click on Add
> Add Function
button. Pick a name for your function path, such as /engage
.
Add axios dependency

Create the GenerativeAgent engagement function
/engage
function. This function establishes the connection between Twilio and GenerativeAgent.This function performs the following:- Obtains a Twilio Media Stream URL with the Get Twilio Media Stream URL API
- Configures the media stream with input parameters
asapp_callTransferId
is passed as the id of the call transfer.asapp_externalConversationId
is passed as the external conversation id of the conversation.customerId
is passed as the customer id for the conversation.taskName
is used to enter a specific task- All other values are used as passed as inputVariables used for the GenerativeAgent conversation.
- Specifies a TwiML connect action to trigger call completion function when call ends
- Returns the TwiML response to Twilio
Create the call transfer state function
/call-complete
function. This function retrieves the call transfer state after the conversation ends with the Get Call Transfer API.This example code just logs what transfer type occurred, you will need to implement your own business logic to handle what happens next.Configure environment variables
Key | Value |
---|---|
ASAPP_API_HOST | The API host provided by ASAPP, e.g.: https://api.sandbox.asapp.com (sandbox) or https://api.asapp.com (production) |
ASAPP_API_ID | The API Key provided by ASAPP |
ASAPP_API_SECRET | The API Secret provided by ASAPP |
DOMAIN_NAME | Your Twilio Function domain within the service (e.g., your-service-1234.twil.io) |
Step 2: Configure Direct Phone Number Integration
This approach directly connects a phone number to your GenerativeAgent function, making it the simplest setup for testing or simple use cases.Select or Create a Phone Number
Configure the Phone Number
Configure
tab of your selected phone number, set these settings:Setting | Value |
---|---|
Configure With | ”Webhook, TwiML Bin, Function, Studio Flow, Proxy Service” |
A call comes in | ”Function” |
Service | The name of your service, e.g.: “GenerativeAgentService” |
Environment | ”ui” |
Function Path | The name of your engagement function, e.g.: “/engage” |
Save Configuration
when done.Step 3: Validate and Deploy Your Integration
Before going live, thoroughly test your integration to ensure it works as expected and provides a good caller experience.Test Your Integration
Testing is essential to ensure the integration works as expected and provides a good caller experience. Test the integration thoroughly:- Make test calls through the phone number configured in Step 2.
Test various scenarios including normal conversations or requests for human agents.
- Verify audio streaming quality and reliability
- Test conversation handling
- Ensure GenerativeAgent understands and responds appropriately
- Test different caller accents and speech patterns
- Verify handling of background noise and interruptions
- Check error scenarios
- Verify error handling paths in your flow
Twilio Flow Integration
If you are using Twilio Studio Flows, you can modify the above integration to work with Flows.Flow Integration Approach
When using flows, the integration is very similar but instead of relying on action callbacks to trigger the call transfer state function, you’ll call the call transfer state function directly from your flow:- Flow calls engage function via TwiML Redirect - This connects GenerativeAgent to the conversation
- Flow calls call transfer state function - Immediately after the call returns, call the function to get transfer state and output context
- Flow processes results - Using the returned transfer data, you can implement your own logic for routing and other business objectives.
Setting Up Flow Integration
Modify the Engagement Function for Flow Use
Modify the Call Transfer State Function to Return Values
Add Widgets for Engagement and Call Transfer State
- Add a Redirect widget where you want to engage GenerativeAgent.
- The URL needs to be the URL of your engagement function, with a query parameter of
FlowEvent=return
e.g.:https://your-service-1234.twil.io/engage?FlowEvent=return
.
- The URL needs to be the URL of your engagement function, with a query parameter of
- Add a Run Function widget, specifying the function to fetch the call transfer state.
Handle Transfer State Results