SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site, support.twilio.com, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to help.twilio.com, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

How to Create an Email Task With the Interactions API in Node.js

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 from and from_name when initiated_by is api or agent.
  • Participants are optional unless initiated_by is agent.
  • Routing requires at least workspace_sid.

 

Additional Information 

 

 

 

Have more questions? Submit a request
Powered by Zendesk