Objective
This article provides a simple internal code example in Node.js showing how to initiate an email task using the Twilio Interactions API.
Product
Twilio Flex
Procedure
Use the following example to create an email Interaction initiated by api. Hardcoded credentials are for internal testing only.
const twilio = require("twilio");
const accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const authToken = "your_auth_token";
const client = twilio(accountSid, authToken);
async function createInteraction() {
const interaction = await client.flexApi.v1.interaction.create({
channel: {
type: "email",
initiated_by: "api",
// Required when type=email AND initiated_by=api
properties: {
from: "support@example.com", // Contact center team email
from_name: "Support Team", // Display name
},
// Participants are OPTIONAL (required only for initiated_by=agent)
participants: [
{
level: "to", // email role: to | cc | bcc
name: "John Doe",
address: "customer@example.com",
},
],
},
routing: {
properties: {
workspace_sid: "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // required
workflow_sid: "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // optional
task_channel_unique_name: "email", // optional
attributes: {
customerName: "John Doe",
customerEmail: "customer@example.com",
source: "webform",
interactionType: "api-email",
},
},
},
});
console.log("Interaction created!");
console.log("Interaction SID:", interaction.sid);
}
createInteraction();
-
initiated_by: "api"creates an inbound task. - Email channels require
fromandfrom_namewheninitiated_byisapioragent. - Participants are optional unless
initiated_byisagent. - Routing requires at least
workspace_sid.
Additional Information