Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

TaskRouter.js v1 Workspace: Managing resources in the browser


The SDK allows developers to interact with the entire TaskRouter REST API by a simple JS API.


Adding the SDK to your application

adding-the-sdk-to-your-application page anchor

Include the TaskRouter JS SDK in your JavaScript application as follows:


_10
<script src="https://sdk.twilio.com/js/taskrouter/v1.21/taskrouter.min.js" integrity="sha384-5fq+0qjayReAreRyHy38VpD3Gr9R2OYIzonwIkoGI4M9dhfKW6RWeRnZjfwSrpN8" crossorigin="anonymous"></script>

Creating a TaskRouter Workspace capability token

capability-token page anchor

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):

CapabilityAuthorization
AllowFetchSubresourcesA workspace can fetch any subresource
AllowUpdatesA workspace can update its properties
AllowUpdatesSubresourcesA workspace can update itself and any subresource
AllowDeleteA workspace can delete itself
AllowDeleteSubresourcesA 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:

Creating a TaskRouter Workspace capability token

creating-a-taskrouter-workspace-capability-token page anchor
Node.js
Python
C#
Java
PHP
Ruby

_67
// This snippets constructs the same policy list as seen here:
_67
// https://www.twilio.com/docs/api/taskrouter/constructing-jwts as a base
_67
_67
// Download the Node helper library from twilio.com/docs/node/install
_67
// These consts are your accountSid and authToken from https://www.twilio.com/console
_67
const taskrouter = require('twilio').jwt.taskrouter;
_67
const util = taskrouter.util;
_67
_67
const TaskRouterCapability = taskrouter.TaskRouterCapability;
_67
const Policy = TaskRouterCapability.Policy;
_67
_67
// To set up environmental variables, see http://twil.io/secure
_67
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_67
const authToken = process.env.TWILIO_AUTH_TOKEN;
_67
const workspaceSid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_67
const workerSid = 'WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
_67
_67
const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
_67
const version = 'v1';
_67
_67
const capability = new TaskRouterCapability({
_67
accountSid: accountSid,
_67
authToken: authToken,
_67
workspaceSid: workspaceSid,
_67
channelId: workspaceSid,
_67
});
_67
_67
// Helper function to create Policy
_67
function buildWorkspacePolicy(options) {
_67
options = options || {};
_67
const resources = options.resources || [];
_67
const urlComponents = [
_67
TASKROUTER_BASE_URL,
_67
version,
_67
'Workspaces',
_67
workspaceSid,
_67
];
_67
_67
return new Policy({
_67
url: urlComponents.concat(resources).join('/'),
_67
method: options.method || 'GET',
_67
allow: true,
_67
});
_67
}
_67
_67
// Event Bridge Policies
_67
const eventBridgePolicies = util.defaultEventBridgePolicies(
_67
accountSid,
_67
workspaceSid
_67
);
_67
_67
const workspacePolicies = [
_67
// Workspace Policy
_67
buildWorkspacePolicy(),
_67
// Workspace subresources fetch Policy
_67
buildWorkspacePolicy({ resources: ['**'] }),
_67
// Workspace resources update Policy
_67
buildWorkspacePolicy({ resources: ['**'], method: 'POST' }),
_67
// Workspace resources delete Policy
_67
buildWorkspacePolicy({ resources: ['**'], method: 'DELETE' }),
_67
];
_67
_67
eventBridgePolicies.concat(workspacePolicies).forEach(policy => {
_67
capability.addPolicy(policy);
_67
});
_67
_67
const 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:


_10
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);

The library will raise a 'ready' event once it has connected to TaskRouter:


_10
workspace.on("ready", function(workspace) {
_10
console.log(workspace.sid) // 'WSxxx'
_10
console.log(workspace.friendlyName) // 'Workspace 1'
_10
console.log(workspace.prioritizeQueueOrder) // 'FIFO'
_10
console.log(workspace.defaultActivityName) // 'Offline'
_10
});

See more about the methods and events exposed on this object below.


TaskRouter.js Workspace exposes the following API:


Twilio.TaskRouter.Workspace

taskrouterworkspace page anchor

Twilio.TaskRouter.Workspace is the top-level class you'll use for managing a workspace.

new Twilio.TaskRouter.Workspace(workspaceToken)

new-taskrouterworkspace page anchor

Register a new Twilio.TaskRouter.Workspace with the capabilities provided in workspaceToken.

NameTypeDescription
workspaceTokenStringA Twilio TaskRouter capability token. See Creating a TaskRouter capability token for more information.
debugBoolean(optional) Whether or not the JS SDK will print event messages to the console. Defaults to true.
regionString(optional) A Twilio region for websocket connections (ex. ie1-ix).
maxRetriesInteger(optional) The maximum of retries to attempt if a websocket request fails. Defaults to 0.

_10
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);

Turning off debugging:


_10
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN, false);

update([args...], [resultCallback])

update page anchor

Updates a single or list of properties on a workspace.

NameTypeDescription
args...String or JSONA single API parameter and value or a JSON object containing multiple values
resultCallbackFunction(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.
Single Attribute Example
single-attribute-example page anchor

_10
workspace.update("EventCallbackUrl", "http://requestb.in/1kmw9im1", function(error, workspace) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
} else {
_10
console.log(workspace.eventCallbackUrl); // "http://requestb.in/1kmw9im1"
_10
}
_10
});

Multiple Attribute Example
multiple-attribute-example page anchor

_10
var props = {"EventCallbackUrl", "http://requestb.in/1kmw9im1", "TimeoutActivitySid":"WAxxx"};
_10
workspace.update(props, function(error, workspace) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
} else {
_10
console.log(workspace.eventCallbackUrl); // "http://requestb.in/1kmw9im1"
_10
console.log(workspace.timeoutActivitySid); // "WAxxx"
_10
}
_10
});

delete([resultCallback])

delete page anchor

Deletes a workspace

NameTypeDescription
resultCallbackFunction(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.

_10
workspace.delete(function(error) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
} else {
_10
console.log("workspace deleted");
_10
}
_10
});

updateToken(workspaceToken)

updatetoken page anchor

Updates the TaskRouter capability token for the Workspace.

NameTypeDescription
workspaceTokenStringA valid TaskRouter capability token.

_10
var token = refreshJWT(); // your method to retrieve a new capability token
_10
workspace.updateToken(token);

Retrieves the object to retrieve the list of activities, create a new activity, fetch, update or delete a specific activity.

NameTypeDescription
sidString(optional) SID to fetch
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_14
workspace.activities.fetch(
_14
function(error, activityList) {
_14
if(error) {
_14
console.log(error.code);
_14
console.log(error.message);
_14
return;
_14
}
_14
console.log("Parsing response");
_14
var data = activityList.data;
_14
for(i=0; i<data.length; i++) {
_14
console.log(data[i].friendlyName);
_14
}
_14
}
_14
);

Fetching a specific Activity
fetching-a-specific-activity page anchor

_10
workspace.activities.fetch("WAxxx",
_10
function(error, activity) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log(activity.friendlyName);
_10
}
_10
);

NameTypeDescription
paramsJSONAn object containing multiple values
callbackFunctionA 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.

_12
var params = {"FriendlyName":"Activity1", "Available":"true"};
_12
workspace.activities.create(
_12
params,
_12
function(error, activity) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("FriendlyName: "+activity.friendlyName);
_12
}
_12
);

You can update an Activity resource in two manners:

  • Updating on an Instance Resource
  • Updating on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters page anchor
NameTypeDescription
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_23
workspace.activities.fetch(
_23
function(error, activityList) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
var data = activityList.data;
_23
for(i=0; i<data.length; i++) {
_23
var activity = data[i];
_23
activity.update("WAxxx", "FriendlyName", "NewFriendlyName",
_23
function(error, activity) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
console.log("FriendlyName: "+activity.friendlyName);
_23
}
_23
);
_23
}
_23
}
_23
);

List Resource Parameters

list-resource-parameters page anchor
NameTypeDescription
sidStringSID to update
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_10
workspace.activities.update("WAxxx", "FriendlyName", "NewFriendlyName",
_10
function(error, activity) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("FriendlyName: "+activity.friendlyName);
_10
}
_10
);

You can delete an Activity resource in two manners:

  • Deleting on an Instance Resource
  • Deleting on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-2 page anchor
NameTypeDescription
resultCallbackFunction(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.

_21
workspace.activities.fetch(
_21
function(error, activityList) {
_21
if(error) {
_21
console.log(error.code);
_21
console.log(error.message);
_21
return;
_21
}
_21
var data = activityList.data;
_21
for(i=0; i<data.length; i++) {
_21
var activity = data[i];
_21
activity.delete(function(error) {
_21
if(error) {
_21
console.log(error.code);
_21
console.log(error.message);
_21
return;
_21
}
_21
console.log("Activity deleted");
_21
});
_21
}
_21
}
_21
);

NameTypeDescription
sidStringSID to delete
resultCallbackFunction(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.

_10
workspace.activities.delete("WAxxx"
_10
function(error) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("Activity deleted");
_10
}
_10
);

Retrieves the object to retrieve the list of workflows, create a new workflow, fetch, update or delete a specific workflow.

NameTypeDescription
sidString(optional) SID to fetch
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_14
workspace.workflows.fetch(
_14
function(error, workflowList) {
_14
if(error) {
_14
console.log(error.code);
_14
console.log(error.message);
_14
return;
_14
}
_14
console.log("Parsing response");
_14
var data = workflowList.data;
_14
for(i=0; i<data.length; i++) {
_14
console.log(data[i].friendlyName);
_14
}
_14
}
_14
);

Fetching a specific Workflow
fetching-a-specific-workflow page anchor

_10
workspace.workflows.fetch("WWxxx",
_10
function(error, workflow) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log(workflow.friendlyName);
_10
}
_10
);

NameTypeDescription
paramsJSONAn object containing multiple values
callbackFunctionA 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.

_13
var workflowConfig = {"task_routing":{"default_filter":{"task_queue_sid":"WQxxx"}}};
_13
var params = {"FriendlyName":"Workflow1", "AssignmentCallbackUrl":"http://requestb.in/1kmw9im1", "Configuration":workflowConfig};
_13
workspace.workflows.create(
_13
params,
_13
function(error, workflow) {
_13
if(error) {
_13
console.log(error.code);
_13
console.log(error.message);
_13
return;
_13
}
_13
console.log("FriendlyName: "+workflow.friendlyName);
_13
}
_13
);

You can update a Workflow resource in two manners:

  • Updating on an Instance Resource
  • Updating on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-3 page anchor
NameTypeDescription
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_23
workspace.workflows.fetch(
_23
function(error, workflowList) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
var data = workflowList.data;
_23
for(i=0; i<data.length; i++) {
_23
var workflow = data[i];
_23
workflow.update("WWxxx", "TaskReservationTimeout", "300",
_23
function(error, workflow) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
console.log("TaskReservationTimeout: "+workflow.taskReservationTimeout);
_23
}
_23
);
_23
}
_23
}
_23
);

NameTypeDescription
sidStringSID to update
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_10
workspace.workflows.update("WWxxx", "TaskReservationTimeout", "300",
_10
function(error, workflow) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("TaskReservationTimeout: "+workflow.taskReservationTimeout);
_10
}
_10
);

You can delete a Workflow resource in two manners:

  • Deleting on an Instance Resource
  • Deleting on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-4 page anchor
NameTypeDescription
resultCallbackFunction(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.

_22
workspace.workflows.fetch(
_22
function(error, workflowList) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
var data = workflowList.data;
_22
for(i=0; i<data.length; i++) {
_22
var workflow = data[i];
_22
workflow.delete(function(error) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
console.log("Workflow deleted");
_22
}
_22
);
_22
}
_22
}
_22
);

NameTypeDescription
sidStringSID to delete
resultCallbackFunction(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.

_10
workspace.workflows.delete("WWxxx"
_10
function(error) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("Workflow deleted");
_10
}
_10
);

Retrieves the object to retrieve the statistics for a workflow.

NameTypeDescription
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_12
var queryParams = {"Minutes":"240"};
_12
workflow.statistics.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workflow statistics: "+JSON.stringify(statistics));
_12
}
_12
);

Cumulative Stats:


_12
var queryParams = {"Minutes":"240"};
_12
workflow.cumulativeStats.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workflow statistics: "+JSON.stringify(statistics));
_12
}
_12
);

RealTime Stats:


_10
workflow.realtimeStats.fetch(
_10
function(error, statistics) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("fetched workflow statistics: "+JSON.stringify(statistics));
_10
}
_10
);

Retrieves the object to retrieve the list of taskqueues, create a new taskqueue, fetch, update or delete a specific taskqueue.

NameTypeDescription
sidString(optional) SID to fetch
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_14
workspace.taskqueues.fetch(
_14
function(error, taskQueueList) {
_14
if(error) {
_14
console.log(error.code);
_14
console.log(error.message);
_14
return;
_14
}
_14
console.log("Parsing response");
_14
var data = taskQueueList.data;
_14
for(i=0; i<data.length; i++) {
_14
console.log(data[i].friendlyName);
_14
}
_14
}
_14
);

Fetching a specific TaskQueue
fetching-a-specific-taskqueue page anchor

_10
workspace.taskqueues.fetch("WQxxx",
_10
function(error, taskQueue) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log(taskQueue.friendlyName);
_10
}
_10
);

NameTypeDescription
paramsJSONAn object containing multiple values
callbackFunctionA 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.

_12
var params = {"FriendlyName":"TaskQueue1", "ReservationActivitySid":"WAxxx", "AssignmentActivitySid":"WAxxx", "TargetWorkers":"1==1"};
_12
workspace.taskqueues.create(
_12
params,
_12
function(error, taskQueue) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("FriendlyName: "+taskQueue.friendlyName);
_12
}
_12
);

You can update a TaskQueue resource in two manners:

  • Updating on an Instance Resource
  • Updating on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-5 page anchor
NameTypeDescription
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_23
workspace.taskqueues.fetch(
_23
function(error, taskQueueList) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
var data = taskQueueList.data;
_23
for(i=0; i<data.length; i++) {
_23
var taskqueue = data[i];
_23
taskqueue.update("WWxxx", "MaxReservedWorkers", "20",
_23
function(error, taskqueue) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
console.log("MaxReservedWorkers: "+taskqueue.maxReservedWorkers);
_23
}
_23
);
_23
}
_23
}
_23
);

NameTypeDescription
sidStringSID to update
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_10
workspace.taskqueues.update("WQxxx", "MaxReservedWorkers", "20",
_10
function(error, taskqueue) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("MaxReservedWorkers: "+taskqueue.maxReservedWorkers);
_10
}
_10
);

You can delete a TaskQueue resource in two manners:

  • Deleting on an Instance Resource
  • Deleting on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-6 page anchor
NameTypeDescription
resultCallbackFunction(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.

_22
workspace.taskqueues.fetch(
_22
function(error, taskQueueList) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
var data = taskQueueList.data;
_22
for(i=0; i<data.length; i++) {
_22
var taskqueue = data[i];
_22
taskqueue.delete(function(error) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
console.log("TaskQueue deleted");
_22
}
_22
);
_22
}
_22
}
_22
);

NameTypeDescription
sidStringSID to delete
resultCallbackFunction(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.

_10
workspace.taskqueues.delete("WQxxx"
_10
function(error) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("TaskQueue deleted");
_10
}
_10
);

Retrieves the object to retrieve the statistics for a taskqueue.

NameTypeDescription
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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:


_12
var queryParams = {"Minutes":"240"};
_12
workspace.taskqueues.statistics.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched taskqueues statistics: "+JSON.stringify(statistics));
_12
}
_12
);

Single TaskQueue:


_12
var queryParams = {"Minutes":"240"};
_12
taskqueue.statistics.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));
_12
}
_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:


_11
taskqueue.cumulativeStats.fetch(
_11
function(error, statistics) {
_11
if(error) {
_11
console.log(error.code);
_11
console.log(error.message);
_11
return;
_11
}
_11
console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));
_11
console.log("avg task acceptance time: "+statistics.avgTaskAcceptanceTime;
_11
}
_11
);

RealTime Stats:


_13
var queryParams = {"Minutes":"240"};
_13
taskqueue.realtimeStats.fetch(
_13
queryParams,
_13
function(error, statistics) {
_13
if(error) {
_13
console.log(error.code);
_13
console.log(error.message);
_13
return;
_13
}
_13
console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));
_13
console.log("total available workers: "+statistics.totalAvailableWorkers;
_13
}
_13
);

Retrieves the object to retrieve the list of workers, create a new worker, fetch, update or delete a specific worker.

NameTypeDescription
sidString(optional) SID to fetch
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_14
workspace.workers.fetch(
_14
function(error, workerList) {
_14
if(error) {
_14
console.log(error.code);
_14
console.log(error.message);
_14
return;
_14
}
_14
console.log("Parsing response");
_14
var data = workerList.data;
_14
for(i=0; i<data.length; i++) {
_14
console.log(data[i].friendlyName);
_14
}
_14
}
_14
);

Fetching a specific Worker
fetching-a-specific-worker page anchor

_10
workspace.workers.fetch("WKxxx",
_10
function(error, worker) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log(worker.friendlyName);
_10
}
_10
);

NameTypeDescription
paramsJSONAn object containing multiple values
callbackFunctionA 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.

_12
var params = {"FriendlyName":"Worker1"};
_12
workspace.workers.create(
_12
params,
_12
function(error, worker) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("FriendlyName: "+worker.friendlyName);
_12
}
_12
);

You can update a Worker resource in two manners:

  • Updating on an Instance Resource
  • Updating on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-7 page anchor
NameTypeDescription
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_23
workspace.workers.fetch(
_23
function(error, workerList) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
var data = workerList.data;
_23
for(i=0; i<data.length; i++) {
_23
var worker = data[i];
_23
worker.update("WKxxx", "ActivitySid", "WAxxx",
_23
function(error, worker) {
_23
if(error) {
_23
console.log(error.code);
_23
console.log(error.message);
_23
return;
_23
}
_23
console.log("FriendlyName: "+worker.friendlyName);
_23
}
_23
);
_23
}
_23
}
_23
);

NameTypeDescription
sidStringSID to update
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_10
workspace.workers.update("WKxxx", "ActivitySid", "WAxxx",
_10
function(error, worker) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("FriendlyName: "+worker.friendlyName);
_10
}
_10
);

You can delete a Worker resource in two manners:

  • Deleting on an Instance Resource
  • Deleting on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-8 page anchor
NameTypeDescription
resultCallbackFunction(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.

_22
workspace.workers.fetch(
_22
function(error, workerList) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
var data = workerList.data;
_22
for(i=0; i<data.length; i++) {
_22
var worker = data[i];
_22
worker.delete(function(error) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
console.log("Worker deleted");
_22
}
_22
);
_22
}
_22
}
_22
);

NameTypeDescription
sidStringSID to delete
resultCallbackFunction(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.

_10
workspace.workers.delete("WKxxx"
_10
function(error) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("Worker deleted");
_10
}
_10
);

Retrieves the object to retrieve the statistics for a worker.

NameTypeDescription
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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:


_12
var queryParams = {"Minutes":"240"};
_12
workspace.workers.statistics.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workers statistics: "+JSON.stringify(statistics));
_12
}
_12
);

List of Workers Cumulative Stats:


_12
var queryParams = {"Minutes":"240"};
_12
workspace.workers.cumulativeStats.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workers statistics: "+JSON.stringify(statistics));
_12
}
_12
);

List of Workers RealTime Stats:


_10
workspace.workers.realtimeStats.fetch(
_10
function(error, statistics) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("fetched workers statistics: "+JSON.stringify(statistics));
_10
}
_10
);

Single Worker:


_12
var queryParams = {"Minutes":"240"};
_12
worker.statistics.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched worker statistics: "+JSON.stringify(statistics));
_12
}
_12
);

Retrieves the object to retrieve the list of tasks, create a new task, fetch, update or delete a specific task.

NameTypeDescription
sidString(optional) SID to fetch
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_14
workspace.tasks.fetch(
_14
function(error, taskList) {
_14
if(error) {
_14
console.log(error.code);
_14
console.log(error.message);
_14
return;
_14
}
_14
console.log("Parsing response");
_14
var data = taskList.data;
_14
for(i=0; i<data.length; i++) {
_14
console.log(JSON.stringify(data[i].attributes));
_14
}
_14
}
_14
);

Fetching a specific Task
fetching-a-specific-task page anchor

_10
workspace.tasks.fetch("WTxxx",
_10
function(error, task) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log(JSON.stringify(task.attributes));
_10
}
_10
);

NameTypeDescription
paramsJSONAn object containing multiple values
callbackFunctionA 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.

_13
var attributes = "{\"Ticket\":\"Gold\"}";
_13
var params = {"WorkflowSid":"WWxxx", "Attributes":attributes};
_13
workspace.tasks.create(
_13
params,
_13
function(error, task) {
_13
if(error) {
_13
console.log(error.code);
_13
console.log(error.message);
_13
return;
_13
}
_13
console.log("TaskSid: "+task.sid);
_13
}
_13
);

You can update a Task resource in two manners:

  • Updating on an Instance Resource
  • Updating on the List Resource passing a specific SID

Instance Resource Parameters

instance-resource-parameters-9 page anchor
NameTypeDescription
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_24
workspace.tasks.fetch(
_24
function(error, taskList) {
_24
if(error) {
_24
console.log(error.code);
_24
console.log(error.message);
_24
return;
_24
}
_24
var data = taskList.data;
_24
var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}
_24
for(i=0; i<data.length; i++) {
_24
var task = data[i];
_24
task.update("WTxxx", cancelAttrs,
_24
function(error, task) {
_24
if(error) {
_24
console.log(error.code);
_24
console.log(error.message);
_24
return;
_24
}
_24
console.log("Attributes: "+JSON.stringify(task.attributes));
_24
}
_24
);
_24
}
_24
}
_24
);

NameTypeDescription
sidStringSID to update
args...String or JSONA single API parameter and value or a JSON object containing multiple values
callbackFunctionA 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.

_11
var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}
_11
workspace.tasks.update("WTxxx", cancelAttrs,
_11
function(error, task) {
_11
if(error) {
_11
console.log(error.code);
_11
console.log(error.message);
_11
return;
_11
}
_11
console.log("Attributes: "+JSON.stringify(task.attributes));
_11
}
_11
);

You can delete a Task resource in two manners:

  • Deleting on an Instance Resource
  • Deleting on the List Resource passing a specific SID
NameTypeDescription
resultCallbackFunction(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.

_22
workspace.tasks.fetch(
_22
function(error, taskList) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
var data = taskList.data;
_22
for(i=0; i<data.length; i++) {
_22
var task = data[i];
_22
task.delete(function(error) {
_22
if(error) {
_22
console.log(error.code);
_22
console.log(error.message);
_22
return;
_22
}
_22
console.log("Task deleted");
_22
}
_22
);
_22
}
_22
}
_22
);

NameTypeDescription
sidStringSID to delete
resultCallbackFunction(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.

_10
workspace.tasks.delete("WTxxx"
_10
function(error) {
_10
if(error) {
_10
console.log(error.code);
_10
console.log(error.message);
_10
return;
_10
}
_10
console.log("task deleted");
_10
}
_10
);

Retrieves the object to retrieve the statistics for a workspace.

NameTypeDescription
paramsJSON(optional) A JSON object of query parameters
callbackFunctionA 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.

_12
var queryParams = {"Minutes":"240"};
_12
workspace.statistics.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workspace statistics: "+JSON.stringify(statistics));
_12
}
_12
);

Cumulative Stats:


_12
var queryParams = {"Minutes":"240"};
_12
workspace.cumulativeStats.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workspace statistics: "+JSON.stringify(statistics));
_12
}
_12
);

RealTime Stats:


_12
var queryParams = {};
_12
workspace.realtimeStats.fetch(
_12
queryParams,
_12
function(error, statistics) {
_12
if(error) {
_12
console.log(error.code);
_12
console.log(error.message);
_12
return;
_12
}
_12
console.log("fetched workspace statistics: "+JSON.stringify(statistics));
_12
}
_12
);

on(event, callback)

workspace-on page anchor

Attaches a listener to the specified event. See Events for the complete list of supported events.

NameTypeDescription
eventStringAn event name. See Events for the complete list of supported events.
callbackFunctionA function that will be called when the specified Event is raised.

_10
workspace.on("ready", function(workspace) {
_10
console.log(workspace.friendlyName) // MyWorkspace
_10
});


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.

NameTypeDescription
workspaceWorkspaceThe Workspace object for the Workspace you've created.

_10
workspace.on("ready", function(workspace) {
_10
console.log(workspace.friendlyName) // MyWorkspace
_10
});

The Workspace has established a connection to TaskRouter.


_10
workspace.on("connected", function() {
_10
console.log("Websocket has connected");
_10
});

The Workspace has disconnected from TaskRouter.


_10
workspace.on("disconnected", function() {
_10
console.log("Websocket has disconnected");
_10
});

Raised when the TaskRouter capability token used to create this Workspace expires.


_10
workspace.on("token.expired", function() {
_10
console.log("updating token");
_10
var token = refreshJWT(); // your method to retrieve a new capability token
_10
workspace.updateToken(token);
_10
});


Rate this page: