Flex Conversations requires Flex UI 2.0.x. If you are on Flex UI 1.x.x, refer to the Messaging in Flex pages.
The Interactions resource lets you create inbound and outbound interactions for different channel types. To create an interaction, you must supply Channel and Routing attributes. The Channel attributes are used to either create or to bind to an underlying media channel such as a conversation. The Routing attributes are used to create a task which is then routed according to your specified workspace and workflow. A successful interaction results in a task reservation being offered to an agent which references the associated media channel.
Creating an interaction is an asynchronous request for which a 200
response only indicates that the server has accepted the request. You need to use the getter methods to check if the interaction channel was set up successfully. Additionally, the example JSON responses on this page are not representative of the actual API response. See example response on the API overview page.
In this release, an interaction supports a single channel. Refer to the Invites subresource to see how you can create multiple tasks for the same interaction channel to include multiple agents.
POST /Interactions
Remember to handle certain terminal TaskRouter events according to your application's needs, as conversations may stay orphaned with an open
state.
See Conversations Best Practices for details.
The unique string created by Twilio to identify an Interaction resource, prefixed with KD.
^KD[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
A JSON object that defines the Interaction’s communication channel and includes details about the channel. See the Outbound SMS and inbound (API-initiated) Channel object examples.
A JSON Object representing the routing rules for the Interaction Channel. See Outbound SMS Example for an example Routing object. The Interactions resource uses TaskRouter for all routing functionality. All attributes in the Routing object on your Interaction request body are added “as is” to the task. For a list of known attributes consumed by the Flex UI and/or Flex Insights, see Known Task Attributes.
^HQ[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
Parameter | Sub-fields | Description |
---|---|---|
Channel [required] | A JSON object that defines the interaction's communication channel and includes details about the channel. See the Outbound Email, Outbound SMS, and inbound (customer-initiated) Channel object examples. | |
sid [optional] | The SID of the Interaction Channel if already generated. This is a unique identifier for the channel. | |
type [required] | The media channel type. Allowed values: sms , whatsapp , web , chat , email , messenger , and gbm Note: These can be different from the task channel type specified in the Routing attributes. Task channel type corresponds to channel capacity while this channel type is the actual media type. | |
initiated_by [required] | The actor that created this Interaction. Allowed values: customer , agent , or api customer: Indicates that this interaction was initiated by a customer and will always trigger an inbound task. The media_channel_sid , defined in the properties object, is required and the customer participant must already be in that given media_channel_sid .agent: initiated by an agent's click. Will always trigger an outbound Task for the same agent. This will also create a new Conversation and add the agent to that Conversation. api: Triggered or initiated by a secondary type of interaction e.g. webform, automation, etc. This will create an inbound Task. If a media_channel_sid is not given, a new one will be created and the given participants in the participants object will be added. | |
properties [optional] | The media channel properties. It is optional if sid is passed. In this release only Twilio Conversations is supported as a media channel.Depending on the value used in initiated_by and Channel type , there are required values in some cases as follows:media_channel_sid is required for all channels, when initiated_by is set to the value customer. Note, this must be set to a Twilio Conversation SID which is prefixed by CH. See the SID property on the Conversation Resource.For email channel with initiated_by set to agent or api, the following are required:from: email address of contact center team (e.g. support@yourcompany.com). This is set as the projected_address parameter on the Conversation Participant resource.from_name: name of contact center team (e.g. Twilio Support). | |
participants [ ] [optional] | An array of participants. Required for outbound interactions where initiated_by is set to agent. Otherwise, it's optional. The specified participants will be added to the media channel and to the interaction channel. The attributes required to specify the participant address depends on the type of address, as follows: SMS & Whatsapp: proxy_address: This is the business number which must be a Twilio-verified number e.g +192555512345, whatsapp:+19251235555 address: Actual phone number of the customer Chat & Web: identity: The chat identity of the user in Twilio conversations. We recommend following the standard URI specification and avoid the following reserved characters ! * ' ( ) ; : @ & = + $ , / ? % # [ ] for values such as identity and friendly name. Email: level: the recipient field. Could be to, cc, or bcc name: customer display name in your Flex application address: customer email address For more information, refer to the Twilio Conversations Participant resource. | |
Routing [optional] | A JSON Object representing the routing rules for the Interaction Channel. See Outbound SMS Example for an example Routing object. The Interactions resource uses TaskRouter for all routing functionality. Routing is optional if sid is provided in Channel object, else Routing is required. | |
properties [required] | Fields needed to create a task: workspace_sid [required] The TaskRouter Workspace SID which starts with WS. workflow_sid [optional] The Workflow SID prefixed by WW. Optional if there's only one workflow defined otherwise it's mandatory queue_sid [required for agent-initiated], the Task router Queue SID prefixed by WQ. worker_sid [required for agent-initiated] task_channel_sid [optional] task_channel_unique_name [optional] attributes [optional] : The task attributes. priority [optional]: The priority of the task in the queue. timeout [optional]: The task/reservation timeout interval in seconds. All attributes in the Routing object on your Interaction request body are added "as is" to the task. For a list of known attributes consumed by the Flex UI and/or Flex Insights, see Known Task Attributes. |
POST https://flex-api.twilio.com/v1/Interactions
application/x-www-form-urlencoded
The Interaction context sid is used for adding a context lookup sid
^HQ[0-9a-fA-F]{32}$
Min length: 34
Max length: 34
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createInteraction() {11const interaction = await client.flexApi.v1.interaction.create({12channel: {},13});1415console.log(interaction.sid);16}1718createInteraction();
1{2"sid": "KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"channel": {4"type": "sms",5"sid": "UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"6},7"routing": {8"reservation": null,9"properties": {10"date_updated": 1634845217,11"task_queue_entered_date": 1634845217,12"workflow_name": "Default Fifo Workflow",13"age_in_queue": 0,14"task_channel_unique_name": "default",15"assignment_status": "pending",16"queue_name": "Sample Queue",17"assignmentCounter": 0,18"priority": 0,19"sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"workflow_sid": "WWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"routing_target": "WKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",22"reason": "",23"attributes": "{\"customerAddress\":\"customer phone address\",\"flexChannelInviteSid\":\"KGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"conversationSid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"channelType\":\"sms\",\"customers\":{\"phone\":\"customer phone address\",\"name\":\"customer name\"},\"conversations\":{\"initiated_by\":\"customer\",\"conversation_id\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"media\":[{\"type\":\"ChatTranscript\",\"sid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}]},\"customerName\":\"customer name\",\"flexInteractionChannelSid\":\"UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"initiatedBy\":\"customer\",\"flexInteractionSid\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"direction\":\"inbound\"}",24"task_channel_sid": "TCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",25"age": 0,26"workspace_sid": "WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",27"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",28"timeout": 86400,29"date_created": 1634845217,30"addons": "{}",31"queue_sid": "WQaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"32}33},34"interaction_context_sid": null,35"url": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",36"links": {37"channels": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels"38}39}
For customer-initiated contacts, you must have an existing Conversation SID for the required media_channel_sid
.
In this example, we have configured the SMS address in the Console with a webhook
integration. As a result, our endpoint will be called when a new conversation is created and a new message (SMS) is added. At this time, we would create the Interaction and the Channel and have it routed to an agent. The diagram illustrates the flow.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createInteraction() {11const interaction = await client.flexApi.v1.interaction.create({12channel: {13type: "sms",14initiated_by: "customer",15},16});1718console.log(interaction.sid);19}2021createInteraction();
1{2"sid": "KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"channel": {4"type": "sms",5"sid": "UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"6},7"routing": {8"reservation": null,9"properties": {10"date_updated": 1634845217,11"task_queue_entered_date": 1634845217,12"workflow_name": "Default Fifo Workflow",13"age_in_queue": 0,14"task_channel_unique_name": "default",15"assignment_status": "pending",16"queue_name": "Sample Queue",17"assignmentCounter": 0,18"priority": 0,19"sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"workflow_sid": "WWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"routing_target": "WKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",22"reason": "",23"attributes": "{\"customerAddress\":\"customer phone address\",\"flexChannelInviteSid\":\"KGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"conversationSid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"channelType\":\"sms\",\"customers\":{\"phone\":\"customer phone address\",\"name\":\"customer name\"},\"conversations\":{\"initiated_by\":\"customer\",\"conversation_id\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"media\":[{\"type\":\"ChatTranscript\",\"sid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}]},\"customerName\":\"customer name\",\"flexInteractionChannelSid\":\"UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"initiatedBy\":\"customer\",\"flexInteractionSid\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"direction\":\"inbound\"}",24"task_channel_sid": "TCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",25"age": 0,26"workspace_sid": "WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",27"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",28"timeout": 86400,29"date_created": 1634845217,30"addons": "{}",31"queue_sid": "WQaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"32}33},34"interaction_context_sid": null,35"url": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",36"links": {37"channels": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels"38}39}
This example shows how to create an outbound Flex interaction from your backend application. Use the /Interactions endpoint to implement additional opportunities for agents to send an outbound SMS, WhatsApp, Chat, Email (private beta), and Facebook Messenger (public beta).
Some practical examples include a "click-to-sms" or "click-to-email" functionality in your CRM integration.
The API response will contain a newly created task SID, which you can use to update the task attributes, retrieve the conversation SID, or do some additional programming as needed. The agent initiating the interaction must be available in the Flex application to start the conversation in the Flex UI.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createInteraction() {11const interaction = await client.flexApi.v1.interaction.create({12channel: {13type: "sms",14initiated_by: "agent",15participants: [16{17address: "+13115552368",18proxy_address: "+192555512345",19},20],21},22routing: {23properties: {24workspace_sid: "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",25workflow_sid: "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",26queue_sid: "WQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",27worker_sid: "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",28task_channel_unique_name: "sms",29attributes: {30customerName: "silly name",31customerAddress: "+13115552368",32},33},34},35});3637console.log(interaction.routing);38}3940createInteraction();
1{2"sid": "KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"channel": {4"type": "sms",5"sid": "UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"6},7"routing": {8"reservation": null,9"properties": {10"date_updated": 1634845217,11"task_queue_entered_date": 1634845217,12"workflow_name": "Default Fifo Workflow",13"age_in_queue": 0,14"task_channel_unique_name": "default",15"assignment_status": "pending",16"queue_name": "Sample Queue",17"assignmentCounter": 0,18"priority": 0,19"sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"workflow_sid": "WWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"routing_target": "WKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",22"reason": "",23"attributes": "{\"customerAddress\":\"customer phone address\",\"flexChannelInviteSid\":\"KGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"conversationSid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"channelType\":\"sms\",\"customers\":{\"phone\":\"customer phone address\",\"name\":\"customer name\"},\"conversations\":{\"initiated_by\":\"customer\",\"conversation_id\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"media\":[{\"type\":\"ChatTranscript\",\"sid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}]},\"customerName\":\"customer name\",\"flexInteractionChannelSid\":\"UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"initiatedBy\":\"customer\",\"flexInteractionSid\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"direction\":\"inbound\"}",24"task_channel_sid": "TCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",25"age": 0,26"workspace_sid": "WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",27"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",28"timeout": 86400,29"date_created": 1634845217,30"addons": "{}",31"queue_sid": "WQaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"32}33},34"interaction_context_sid": null,35"url": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",36"links": {37"channels": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels"38}39}
You can use the API to prompt an agent through a workflow to message your customer.
These tasks can be prompted by a customer requesting assistance (for example, an "ask for help" form) or through automation (for example, analysis of reviews). Accordingly, you must indicate that the interaction was initiated by the API. An example flow can go like this:
In the diagram above, step 2 might look something like this:
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createInteraction() {11const interaction = await client.flexApi.v1.interaction.create({12channel: {13type: "sms",14initiated_by: "api",15participants: [16{17address: "+13115552368",18proxy_address: "+192555512345",19},20],21},22routing: {23properties: {24workspace_sid: "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",25workflow_sid: "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",26task_channel_unique_name: "sms",27attributes: {28from: "testname",29customerName: "testcustomer",30customerAddress: "+13115552368",31},32},33},34});3536console.log(interaction.sid);37}3839createInteraction();
1{2"sid": "KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"channel": {4"type": "sms",5"sid": "UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"6},7"routing": {8"reservation": null,9"properties": {10"date_updated": 1634845217,11"task_queue_entered_date": 1634845217,12"workflow_name": "Default Fifo Workflow",13"age_in_queue": 0,14"task_channel_unique_name": "default",15"assignment_status": "pending",16"queue_name": "Sample Queue",17"assignmentCounter": 0,18"priority": 0,19"sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"workflow_sid": "WWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"routing_target": "WKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",22"reason": "",23"attributes": "{\"customerAddress\":\"customer phone address\",\"flexChannelInviteSid\":\"KGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"conversationSid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"channelType\":\"sms\",\"customers\":{\"phone\":\"customer phone address\",\"name\":\"customer name\"},\"conversations\":{\"initiated_by\":\"customer\",\"conversation_id\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"media\":[{\"type\":\"ChatTranscript\",\"sid\":\"CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}]},\"customerName\":\"customer name\",\"flexInteractionChannelSid\":\"UOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"initiatedBy\":\"customer\",\"flexInteractionSid\":\"KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"direction\":\"inbound\"}",24"task_channel_sid": "TCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",25"age": 0,26"workspace_sid": "WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",27"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",28"timeout": 86400,29"date_created": 1634845217,30"addons": "{}",31"queue_sid": "WQaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"32}33},34"interaction_context_sid": null,35"url": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",36"links": {37"channels": "https://flex-api.twilio.com/v1/Interactions/KDaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels"38}39}