The SDK allows developers to interact with the entire TaskRouter REST API by a simple JS API.
Include the TaskRouter JS SDK in your JavaScript application as follows:
<script src="https://sdk.twilio.com/js/taskrouter/v1.21/taskrouter.min.js" integrity="sha384-5fq+0qjayReAreRyHy38VpD3Gr9R2OYIzonwIkoGI4M9dhfKW6RWeRnZjfwSrpN8" crossorigin="anonymous"></script>
TaskRouter uses Twilio capability tokens to delegate scoped access to TaskRouter resources to your JavaScript application. Twilio capability tokens conform to the JSON Web Token (JWT) standard, which allow for limited-time use of credentials by a third party. Your web server needs to generate a Twilio capability token and provide it to your JavaScript application in order to register a TaskRouter Workspace.
We provide five helper methods to provide access capabilities (Note: These are not currently available in the Node.js library):
Capability | Authorization |
---|---|
AllowFetchSubresources | A workspace can fetch any subresource |
AllowUpdates | A workspace can update its properties |
AllowUpdatesSubresources | A workspace can update itself and any subresource |
AllowDelete | A workspace can delete itself |
AllowDeleteSubresources | A workspace can delete itself and any subresource |
Additionally, you can utilize more granular access to particular resources beyond these capabilities. These can viewed under Constructing JWTs.
You can generate a TaskRouter capability token using any of Twilio's Helper Libraries. You'll need to provide your Twilio AccountSid and AuthToken, along with the WorkspaceSid for the Workspace you would like to register. For example, using our PHP helper library you can create a token and add capabilities as follows:
1// This snippets constructs the same policy list as seen here:2// https://www.twilio.com/docs/api/taskrouter/constructing-jwts as a base34// Download the Node helper library from twilio.com/docs/node/install5// These consts are your accountSid and authToken from https://www.twilio.com/console6const taskrouter = require('twilio').jwt.taskrouter;7const util = taskrouter.util;89const TaskRouterCapability = taskrouter.TaskRouterCapability;10const Policy = TaskRouterCapability.Policy;1112// To set up environmental variables, see http://twil.io/secure13const accountSid = process.env.TWILIO_ACCOUNT_SID;14const authToken = process.env.TWILIO_AUTH_TOKEN;15const workspaceSid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';16const workerSid = 'WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';1718const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';19const version = 'v1';2021const capability = new TaskRouterCapability({22accountSid: accountSid,23authToken: authToken,24workspaceSid: workspaceSid,25channelId: workspaceSid,26});2728// Helper function to create Policy29function buildWorkspacePolicy(options) {30options = options || {};31const resources = options.resources || [];32const urlComponents = [33TASKROUTER_BASE_URL,34version,35'Workspaces',36workspaceSid,37];3839return new Policy({40url: urlComponents.concat(resources).join('/'),41method: options.method || 'GET',42allow: true,43});44}4546// Event Bridge Policies47const eventBridgePolicies = util.defaultEventBridgePolicies(48accountSid,49workspaceSid50);5152const workspacePolicies = [53// Workspace Policy54buildWorkspacePolicy(),55// Workspace subresources fetch Policy56buildWorkspacePolicy({ resources: ['**'] }),57// Workspace resources update Policy58buildWorkspacePolicy({ resources: ['**'], method: 'POST' }),59// Workspace resources delete Policy60buildWorkspacePolicy({ resources: ['**'], method: 'DELETE' }),61];6263eventBridgePolicies.concat(workspacePolicies).forEach(policy => {64capability.addPolicy(policy);65});6667const token = capability.toJwt();
Once you have generated a TaskRouter capability token, you can pass this to your front-end web application and initialize the JavaScript library as follows:
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);
The library will raise a 'ready' event once it has connected to TaskRouter:
1workspace.on("ready", function(workspace) {2console.log(workspace.sid) // 'WSxxx'3console.log(workspace.friendlyName) // 'Workspace 1'4console.log(workspace.prioritizeQueueOrder) // 'FIFO'5console.log(workspace.defaultActivityName) // 'Offline'6});
See more about the methods and events exposed on this object below.
TaskRouter.js Workspace exposes the following API:
Twilio.TaskRouter.Workspace is the top-level class you'll use for managing a workspace.
Register a new Twilio.TaskRouter.Workspace with the capabilities provided in workspaceToken
.
Name | Type | Description |
---|---|---|
workspaceToken | String | A Twilio TaskRouter capability token. See Creating a TaskRouter capability token for more information. |
debug | Boolean | (optional) Whether or not the JS SDK will print event messages to the console. Defaults to true. |
region | String | (optional) A Twilio region for websocket connections (ex. ie1-ix ). |
maxRetries | Integer | (optional) The maximum of retries to attempt if a websocket request fails. Defaults to 0. |
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);
Turning off debugging:
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN, false);
Updates a single or list of properties on a workspace.
Name | Type | Description |
---|---|---|
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the update. If an error occurs, the first argument passed to this function will be an Error. If the update is successful, the first argument will be null and the second argument will contain the updated Workspace object. |
1workspace.update("EventCallbackUrl", "http://requestb.in/1kmw9im1", function(error, workspace) {2if(error) {3console.log(error.code);4console.log(error.message);5} else {6console.log(workspace.eventCallbackUrl); // "http://requestb.in/1kmw9im1"7}8});
1var props = {"EventCallbackUrl", "http://requestb.in/1kmw9im1", "TimeoutActivitySid":"WAxxx"};2workspace.update(props, function(error, workspace) {3if(error) {4console.log(error.code);5console.log(error.message);6} else {7console.log(workspace.eventCallbackUrl); // "http://requestb.in/1kmw9im1"8console.log(workspace.timeoutActivitySid); // "WAxxx"9}10});
Deletes a workspace
Name | Type | Description |
---|---|---|
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.delete(function(error) {2if(error) {3console.log(error.code);4console.log(error.message);5} else {6console.log("workspace deleted");7}8});
Updates the TaskRouter capability token for the Workspace.
Name | Type | Description |
---|---|---|
workspaceToken | String | A valid TaskRouter capability token. |
1var token = refreshJWT(); // your method to retrieve a new capability token2workspace.updateToken(token);
Retrieves the object to retrieve the list of activities, create a new activity, fetch, update or delete a specific activity.
Name | Type | Description |
---|---|---|
sid | String | (optional) SID to fetch |
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the Activity list is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain an activity list. |
1workspace.activities.fetch(2function(error, activityList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Parsing response");9var data = activityList.data;10for(i=0; i<data.length; i++) {11console.log(data[i].friendlyName);12}13}14);
1workspace.activities.fetch("WAxxx",2function(error, activity) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log(activity.friendlyName);9}10);
Name | Type | Description |
---|---|---|
params | JSON | An object containing multiple values |
callback | Function | A function that will be called when the created instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the create is successful, the first parameter will be null and the second parameter will contain the created instance. |
1var params = {"FriendlyName":"Activity1", "Available":"true"};2workspace.activities.create(3params,4function(error, activity) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("FriendlyName: "+activity.friendlyName);11}12);
You can update an Activity resource in two manners:
Name | Type | Description |
---|---|---|
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.activities.fetch(2function(error, activityList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = activityList.data;9for(i=0; i<data.length; i++) {10var activity = data[i];11activity.update("WAxxx", "FriendlyName", "NewFriendlyName",12function(error, activity) {13if(error) {14console.log(error.code);15console.log(error.message);16return;17}18console.log("FriendlyName: "+activity.friendlyName);19}20);21}22}23);
Name | Type | Description |
---|---|---|
sid | String | SID to update |
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.activities.update("WAxxx", "FriendlyName", "NewFriendlyName",2function(error, activity) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("FriendlyName: "+activity.friendlyName);9}10);
You can delete an Activity resource in two manners:
Name | Type | Description |
---|---|---|
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.activities.fetch(2function(error, activityList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = activityList.data;9for(i=0; i<data.length; i++) {10var activity = data[i];11activity.delete(function(error) {12if(error) {13console.log(error.code);14console.log(error.message);15return;16}17console.log("Activity deleted");18});19}20}21);
Name | Type | Description |
---|---|---|
sid | String | SID to delete |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.activities.delete("WAxxx"2function(error) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Activity deleted");9}10);
Retrieves the object to retrieve the list of workflows, create a new workflow, fetch, update or delete a specific workflow.
Name | Type | Description |
---|---|---|
sid | String | (optional) SID to fetch |
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the workflow list is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain a workflow list. |
1workspace.workflows.fetch(2function(error, workflowList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Parsing response");9var data = workflowList.data;10for(i=0; i<data.length; i++) {11console.log(data[i].friendlyName);12}13}14);
1workspace.workflows.fetch("WWxxx",2function(error, workflow) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log(workflow.friendlyName);9}10);
Name | Type | Description |
---|---|---|
params | JSON | An object containing multiple values |
callback | Function | A function that will be called when the created instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the create is successful, the first parameter will be null and the second parameter will contain the created instance. |
1var workflowConfig = {"task_routing":{"default_filter":{"task_queue_sid":"WQxxx"}}};2var params = {"FriendlyName":"Workflow1", "AssignmentCallbackUrl":"http://requestb.in/1kmw9im1", "Configuration":workflowConfig};3workspace.workflows.create(4params,5function(error, workflow) {6if(error) {7console.log(error.code);8console.log(error.message);9return;10}11console.log("FriendlyName: "+workflow.friendlyName);12}13);
You can update a Workflow resource in two manners:
Name | Type | Description |
---|---|---|
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.workflows.fetch(2function(error, workflowList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = workflowList.data;9for(i=0; i<data.length; i++) {10var workflow = data[i];11workflow.update("WWxxx", "TaskReservationTimeout", "300",12function(error, workflow) {13if(error) {14console.log(error.code);15console.log(error.message);16return;17}18console.log("TaskReservationTimeout: "+workflow.taskReservationTimeout);19}20);21}22}23);
Name | Type | Description |
---|---|---|
sid | String | SID to update |
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.workflows.update("WWxxx", "TaskReservationTimeout", "300",2function(error, workflow) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("TaskReservationTimeout: "+workflow.taskReservationTimeout);9}10);
You can delete a Workflow resource in two manners:
Name | Type | Description |
---|---|---|
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.workflows.fetch(2function(error, workflowList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = workflowList.data;9for(i=0; i<data.length; i++) {10var workflow = data[i];11workflow.delete(function(error) {12if(error) {13console.log(error.code);14console.log(error.message);15return;16}17console.log("Workflow deleted");18}19);20}21}22);
Name | Type | Description |
---|---|---|
sid | String | SID to delete |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.workflows.delete("WWxxx"2function(error) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Workflow deleted");9}10);
Retrieves the object to retrieve the statistics for a workflow.
Name | Type | Description |
---|---|---|
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the statistics object is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain a statistics object. |
1var queryParams = {"Minutes":"240"};2workflow.statistics.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workflow statistics: "+JSON.stringify(statistics));11}12);
Cumulative Stats:
1var queryParams = {"Minutes":"240"};2workflow.cumulativeStats.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workflow statistics: "+JSON.stringify(statistics));11}12);
RealTime Stats:
1workflow.realtimeStats.fetch(2function(error, statistics) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("fetched workflow statistics: "+JSON.stringify(statistics));9}10);
Retrieves the object to retrieve the list of taskqueues, create a new taskqueue, fetch, update or delete a specific taskqueue.
Name | Type | Description |
---|---|---|
sid | String | (optional) SID to fetch |
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the taskqueue list is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain a taskqueue list. |
1workspace.taskqueues.fetch(2function(error, taskQueueList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Parsing response");9var data = taskQueueList.data;10for(i=0; i<data.length; i++) {11console.log(data[i].friendlyName);12}13}14);
1workspace.taskqueues.fetch("WQxxx",2function(error, taskQueue) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log(taskQueue.friendlyName);9}10);
Name | Type | Description |
---|---|---|
params | JSON | An object containing multiple values |
callback | Function | A function that will be called when the created instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the create is successful, the first parameter will be null and the second parameter will contain the created instance. |
1var params = {"FriendlyName":"TaskQueue1", "ReservationActivitySid":"WAxxx", "AssignmentActivitySid":"WAxxx", "TargetWorkers":"1==1"};2workspace.taskqueues.create(3params,4function(error, taskQueue) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("FriendlyName: "+taskQueue.friendlyName);11}12);
You can update a TaskQueue resource in two manners:
Name | Type | Description |
---|---|---|
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.taskqueues.fetch(2function(error, taskQueueList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = taskQueueList.data;9for(i=0; i<data.length; i++) {10var taskqueue = data[i];11taskqueue.update("WWxxx", "MaxReservedWorkers", "20",12function(error, taskqueue) {13if(error) {14console.log(error.code);15console.log(error.message);16return;17}18console.log("MaxReservedWorkers: "+taskqueue.maxReservedWorkers);19}20);21}22}23);
Name | Type | Description |
---|---|---|
sid | String | SID to update |
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.taskqueues.update("WQxxx", "MaxReservedWorkers", "20",2function(error, taskqueue) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("MaxReservedWorkers: "+taskqueue.maxReservedWorkers);9}10);
You can delete a TaskQueue resource in two manners:
Name | Type | Description |
---|---|---|
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.taskqueues.fetch(2function(error, taskQueueList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = taskQueueList.data;9for(i=0; i<data.length; i++) {10var taskqueue = data[i];11taskqueue.delete(function(error) {12if(error) {13console.log(error.code);14console.log(error.message);15return;16}17console.log("TaskQueue deleted");18}19);20}21}22);
Name | Type | Description |
---|---|---|
sid | String | SID to delete |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.taskqueues.delete("WQxxx"2function(error) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("TaskQueue deleted");9}10);
Retrieves the object to retrieve the statistics for a taskqueue.
Name | Type | Description |
---|---|---|
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the statistics object is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain a statistics object. |
List of TaskQueues:
1var queryParams = {"Minutes":"240"};2workspace.taskqueues.statistics.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched taskqueues statistics: "+JSON.stringify(statistics));11}12);
Single TaskQueue:
1var queryParams = {"Minutes":"240"};2taskqueue.statistics.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));11}12);
Note: Replace statistics
with cumulativestats
or realtimestats
if you only care about cumulative or real time stats for faster response times and a smaller payload.
Cumulative Stats:
1taskqueue.cumulativeStats.fetch(2function(error, statistics) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));9console.log("avg task acceptance time: "+statistics.avgTaskAcceptanceTime;10}11);
RealTime Stats:
1var queryParams = {"Minutes":"240"};2taskqueue.realtimeStats.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));11console.log("total available workers: "+statistics.totalAvailableWorkers;12}13);
Retrieves the object to retrieve the list of workers, create a new worker, fetch, update or delete a specific worker.
Name | Type | Description |
---|---|---|
sid | String | (optional) SID to fetch |
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the worker list is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain an worker list. |
1workspace.workers.fetch(2function(error, workerList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Parsing response");9var data = workerList.data;10for(i=0; i<data.length; i++) {11console.log(data[i].friendlyName);12}13}14);
1workspace.workers.fetch("WKxxx",2function(error, worker) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log(worker.friendlyName);9}10);
Name | Type | Description |
---|---|---|
params | JSON | An object containing multiple values |
callback | Function | A function that will be called when the created instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the create is successful, the first parameter will be null and the second parameter will contain the created instance. |
1var params = {"FriendlyName":"Worker1"};2workspace.workers.create(3params,4function(error, worker) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("FriendlyName: "+worker.friendlyName);11}12);
You can update a Worker resource in two manners:
Name | Type | Description |
---|---|---|
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.workers.fetch(2function(error, workerList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = workerList.data;9for(i=0; i<data.length; i++) {10var worker = data[i];11worker.update("WKxxx", "ActivitySid", "WAxxx",12function(error, worker) {13if(error) {14console.log(error.code);15console.log(error.message);16return;17}18console.log("FriendlyName: "+worker.friendlyName);19}20);21}22}23);
Name | Type | Description |
---|---|---|
sid | String | SID to update |
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.workers.update("WKxxx", "ActivitySid", "WAxxx",2function(error, worker) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("FriendlyName: "+worker.friendlyName);9}10);
You can delete a Worker resource in two manners:
Name | Type | Description |
---|---|---|
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.workers.fetch(2function(error, workerList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = workerList.data;9for(i=0; i<data.length; i++) {10var worker = data[i];11worker.delete(function(error) {12if(error) {13console.log(error.code);14console.log(error.message);15return;16}17console.log("Worker deleted");18}19);20}21}22);
Name | Type | Description |
---|---|---|
sid | String | SID to delete |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.workers.delete("WKxxx"2function(error) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Worker deleted");9}10);
Retrieves the object to retrieve the statistics for a worker.
Name | Type | Description |
---|---|---|
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the statistics object is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain a statistics object. |
List of Workers:
1var queryParams = {"Minutes":"240"};2workspace.workers.statistics.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workers statistics: "+JSON.stringify(statistics));11}12);
List of Workers Cumulative Stats:
1var queryParams = {"Minutes":"240"};2workspace.workers.cumulativeStats.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workers statistics: "+JSON.stringify(statistics));11}12);
List of Workers RealTime Stats:
1workspace.workers.realtimeStats.fetch(2function(error, statistics) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("fetched workers statistics: "+JSON.stringify(statistics));9}10);
Single Worker:
1var queryParams = {"Minutes":"240"};2worker.statistics.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched worker statistics: "+JSON.stringify(statistics));11}12);
Retrieves the object to retrieve the list of tasks, create a new task, fetch, update or delete a specific task.
Name | Type | Description |
---|---|---|
sid | String | (optional) SID to fetch |
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the task list is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain an task list. |
1workspace.tasks.fetch(2function(error, taskList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Parsing response");9var data = taskList.data;10for(i=0; i<data.length; i++) {11console.log(JSON.stringify(data[i].attributes));12}13}14);
1workspace.tasks.fetch("WTxxx",2function(error, task) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log(JSON.stringify(task.attributes));9}10);
Name | Type | Description |
---|---|---|
params | JSON | An object containing multiple values |
callback | Function | A function that will be called when the created instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the create is successful, the first parameter will be null and the second parameter will contain the created instance. |
1var attributes = "{\"Ticket\":\"Gold\"}";2var params = {"WorkflowSid":"WWxxx", "Attributes":attributes};3workspace.tasks.create(4params,5function(error, task) {6if(error) {7console.log(error.code);8console.log(error.message);9return;10}11console.log("TaskSid: "+task.sid);12}13);
You can update a Task resource in two manners:
Name | Type | Description |
---|---|---|
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1workspace.tasks.fetch(2function(error, taskList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = taskList.data;9var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}10for(i=0; i<data.length; i++) {11var task = data[i];12task.update("WTxxx", cancelAttrs,13function(error, task) {14if(error) {15console.log(error.code);16console.log(error.message);17return;18}19console.log("Attributes: "+JSON.stringify(task.attributes));20}21);22}23}24);
Name | Type | Description |
---|---|---|
sid | String | SID to update |
args... | String or JSON | A single API parameter and value or a JSON object containing multiple values |
callback | Function | A function that will be called when the updated instance is returned. If an error occurs when updating the instance, the first parameter passed to this function will contain the Error object. If the update is successful, the first parameter will be null and the second parameter will contain the updated instance. |
1var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}2workspace.tasks.update("WTxxx", cancelAttrs,3function(error, task) {4if(error) {5console.log(error.code);6console.log(error.message);7return;8}9console.log("Attributes: "+JSON.stringify(task.attributes));10}11);
You can delete a Task resource in two manners:
Name | Type | Description |
---|---|---|
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.tasks.fetch(2function(error, taskList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8var data = taskList.data;9for(i=0; i<data.length; i++) {10var task = data[i];11task.delete(function(error) {12if(error) {13console.log(error.code);14console.log(error.message);15return;16}17console.log("Task deleted");18}19);20}21}22);
Name | Type | Description |
---|---|---|
sid | String | SID to delete |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the delete. If an error occurs, the first argument passed to this function will be an Error. If the delete is successful, the first argument will be null. |
1workspace.tasks.delete("WTxxx"2function(error) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("task deleted");9}10);
Retrieves the object to retrieve the statistics for a workspace.
Name | Type | Description |
---|---|---|
params | JSON | (optional) A JSON object of query parameters |
callback | Function | A function that will be called when the statistics object is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain a statistics object. |
1var queryParams = {"Minutes":"240"};2workspace.statistics.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workspace statistics: "+JSON.stringify(statistics));11}12);
Cumulative Stats:
1var queryParams = {"Minutes":"240"};2workspace.cumulativeStats.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workspace statistics: "+JSON.stringify(statistics));11}12);
RealTime Stats:
1var queryParams = {};2workspace.realtimeStats.fetch(3queryParams,4function(error, statistics) {5if(error) {6console.log(error.code);7console.log(error.message);8return;9}10console.log("fetched workspace statistics: "+JSON.stringify(statistics));11}12);
Attaches a listener to the specified event. See Events for the complete list of supported events.
Name | Type | Description |
---|---|---|
event | String | An event name. See Events for the complete list of supported events. |
callback | Function | A function that will be called when the specified Event is raised. |
1workspace.on("ready", function(workspace) {2console.log(workspace.friendlyName) // MyWorkspace3});
TaskRouter's JS library currently raises the following events to the registered Workspace object:
The Workspace has established a connection to TaskRouter and has completed initialization.
Name | Type | Description |
---|---|---|
workspace | Workspace | The Workspace object for the Workspace you've created. |
1workspace.on("ready", function(workspace) {2console.log(workspace.friendlyName) // MyWorkspace3});
The Workspace has established a connection to TaskRouter.
1workspace.on("connected", function() {2console.log("Websocket has connected");3});
The Workspace has disconnected from TaskRouter.
1workspace.on("disconnected", function() {2console.log("Websocket has disconnected");3});
Raised when the TaskRouter capability token used to create this Workspace expires.
1workspace.on("token.expired", function() {2console.log("updating token");3var token = refreshJWT(); // your method to retrieve a new capability token4workspace.updateToken(token);5});