Objective
This article will guide you on how to create an outbound voice Flex tasks using the TaskRouter API.
Warning: When using TaskRouter standalone (without Flex), this API request will not automatically create a Voice Conference.
Product
Twilio Flex
Procedure
Prerequisites
A Flex-enabled Twilio account.
A Workspace (WS…), Workflow (WW…), Task Queue (WQ…), and Voice TaskChannel in TaskRouter (Flex creates these by default).
A ready/available Worker (WK…) signed into Flex, or a TaskQueue SID (WQ...).
An owned/verified Twilio caller ID (the
fromnumber).
The cURL request
curl --location 'https://taskrouter.twilio.com/v1/Workspaces/WSXXXXX/Tasks' \
-u ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:your_auth_token \
--data-urlencode 'RoutingTarget=WKXXXXXX' \
--data-urlencode 'Attributes={"outbound_to":"+1XXXXXXXX", "direction":"outbound", "from":"+1XXXXXXXX"}' \
--data-urlencode 'TaskQueueSid=WQXXXX' \
--data-urlencode 'WorkflowSid=WWXXXX' \
--data-urlencode 'TaskChannel=voice' What each field does:
RoutingTarget=WK…
Directs the Task to a specific Worker or Task Queue.-
Attributes (JSON)
direction:"outbound"– lets Flex know this is an outbound call.outbound_to:"+1…"– the destination phone number.from:"+1…"– the caller ID / Twilio number to dial from.
TaskQueueSid=WQ…
Queue used for the task (can be a general outbound queue).WorkflowSid=WW…
Workflow that will create a Reservation for the targeted Worker.TaskChannel=voice
Ensures the Task is created for the Voice channel.
Minimal REST example (Node.js)
const client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
(async () => {
const task = await client.taskrouter.v1
.workspaces('WSXXXXX')
.tasks
.create({
routingTarget: 'WKXXXXXX',
attributes: JSON.stringify({
outbound_to: '+1XXXXXXXX',
direction: 'outbound',
from: '+1XXXXXXXX'
}),
taskQueueSid: 'WQXXXX',
workflowSid: 'WWXXXX',
taskChannel: 'voice'
});
console.log('Created Task:', task.sid);
})();