Skip to contentSkip to navigationSkip to topbar
On this page

Interactions Resource


(information)

Info

Flex Conversations requires Flex UI 2.0.x. If you are on Flex UI 1.x.x, refer to the Messaging in Flex pages.


Interactions Resource

interactions-resource page anchor

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.

(information)

Info

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
(warning)

Warning

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.


Property nameTypeRequiredDescriptionChild properties
sidSID<KD>

Optional

Not PII

The unique string created by Twilio to identify an Interaction resource, prefixed with KD.

Pattern: ^KD[0-9a-fA-F]{32}$Min length: 34Max length: 34

channelobject

Optional

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.


routingobject

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. 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.


urlstring<uri>

Optional


linksobject<uri-map>

Optional


interaction_context_sidSID<HQ>

Optional

Pattern: ^HQ[0-9a-fA-F]{32}$Min length: 34Max length: 34

ParameterSub-fieldsDescription
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.

Create an Interaction resource

create-an-interaction-resource page anchor
POST https://flex-api.twilio.com/v1/Interactions

Request body parameters

request-body-parameters page anchor
Encoding type:application/x-www-form-urlencoded
SchemaExample
Property nameTypeRequiredDescriptionChild properties
Channelobjectrequired

The Interaction's channel.


Routingobject

Optional

The Interaction's routing logic.


InteractionContextSidSID<HQ>

Optional

The Interaction context sid is used for adding a context lookup sid

Pattern: ^HQ[0-9a-fA-F]{32}$Min length: 34Max length: 34
Create an InteractionLink to code sample: Create an Interaction
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createInteraction() {
11
const interaction = await client.flexApi.v1.interaction.create({
12
channel: {},
13
});
14
15
console.log(interaction.sid);
16
}
17
18
createInteraction();

Output

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
}

Customer-initiated SMS Contact

customer-initiated-sms-contact page anchor
(information)

Info

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.

customer-initiated sms contact diagram.
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createInteraction() {
11
const interaction = await client.flexApi.v1.interaction.create({
12
channel: {
13
type: "sms",
14
initiated_by: "customer",
15
},
16
});
17
18
console.log(interaction.sid);
19
}
20
21
createInteraction();

Output

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
}

Agent-initiated Outbound Interactions

agent-initiated-outbound-interactions page anchor

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.

outbound agent-initiated interaction diagram.

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/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createInteraction() {
11
const interaction = await client.flexApi.v1.interaction.create({
12
channel: {
13
type: "sms",
14
initiated_by: "agent",
15
participants: [
16
{
17
address: "+13115552368",
18
proxy_address: "+192555512345",
19
},
20
],
21
},
22
routing: {
23
properties: {
24
workspace_sid: "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
25
workflow_sid: "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
26
queue_sid: "WQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
27
worker_sid: "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
28
task_channel_unique_name: "sms",
29
attributes: {
30
customerName: "silly name",
31
customerAddress: "+13115552368",
32
},
33
},
34
},
35
});
36
37
console.log(interaction.routing);
38
}
39
40
createInteraction();

Output

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:

api-initiated contact diagram.
  1. Your customer fills out a web form on your site or application.
  2. Your application logic creates an interaction. The content from the form is used to populate the task attributes, and the Interactions resource creates an inbound task.
  3. The Interactions endpoint will take care of creating the conversation and adding the participant
  4. An agent is assigned the task, accepts the reservation, and writes to the customer to solve their issue.

In the diagram above, step 2 might look something like this:

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createInteraction() {
11
const interaction = await client.flexApi.v1.interaction.create({
12
channel: {
13
type: "sms",
14
initiated_by: "api",
15
participants: [
16
{
17
address: "+13115552368",
18
proxy_address: "+192555512345",
19
},
20
],
21
},
22
routing: {
23
properties: {
24
workspace_sid: "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
25
workflow_sid: "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
26
task_channel_unique_name: "sms",
27
attributes: {
28
from: "testname",
29
customerName: "testcustomer",
30
customerAddress: "+13115552368",
31
},
32
},
33
},
34
});
35
36
console.log(interaction.sid);
37
}
38
39
createInteraction();

Output

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
}

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.