TaskRouter.js 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
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>
Creating a TaskRouter Workspace capability token
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:
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:
workspace.on("ready", function(workspace) {
console.log(workspace.sid) // 'WSxxx'
console.log(workspace.friendlyName) // 'Workspace 1'
console.log(workspace.prioritizeQueueOrder) // 'FIFO'
console.log(workspace.defaultActivityName) // 'Offline'
});
See more about the methods and events exposed on this object below.
Workspace API
TaskRouter.js Workspace exposes the following API:
Twilio.TaskRouter.Workspace
Twilio.TaskRouter.Workspace is the top-level class you'll use for managing a workspace.
new Twilio.TaskRouter.Workspace(workspaceToken)
Register a new Twilio.TaskRouter.Workspace with the capabilities provided in workspaceToken
.
Parameters
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. |
Sample usage
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);
Turning off debugging:
var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN, false);
Methods
update([args...], [resultCallback])
Updates a single or list of properties on a workspace.
Parameters
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. |
Single Attribute Example
workspace.update("EventCallbackUrl", "http://requestb.in/1kmw9im1", function(error, workspace) {
if(error) {
console.log(error.code);
console.log(error.message);
} else {
console.log(workspace.eventCallbackUrl); // "http://requestb.in/1kmw9im1"
}
});
Multiple Attribute Example
var props = {"EventCallbackUrl", "http://requestb.in/1kmw9im1", "TimeoutActivitySid":"WAxxx"};
workspace.update(props, function(error, workspace) {
if(error) {
console.log(error.code);
console.log(error.message);
} else {
console.log(workspace.eventCallbackUrl); // "http://requestb.in/1kmw9im1"
console.log(workspace.timeoutActivitySid); // "WAxxx"
}
});
delete([resultCallback])
Deletes a workspace
Parameters
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. |
Example
workspace.delete(function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
} else {
console.log("workspace deleted");
}
});
updateToken(workspaceToken)
Updates the TaskRouter capability token for the Workspace.
Parameters
Name | Type | Description |
---|---|---|
workspaceToken | String | A valid TaskRouter capability token. |
Example
var token = refreshJWT(); // your method to retrieve a new capability token
workspace.updateToken(token);
activities
Retrieves the object to retrieve the list of activities, create a new activity, fetch, update or delete a specific activity.
fetch
Parameters
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. |
workspace.activities.fetch(
function(error, activityList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Parsing response");
var data = activityList.data;
for(i=0; i<data.length; i++) {
console.log(data[i].friendlyName);
}
}
);
Fetching a specific Activity
workspace.activities.fetch("WAxxx",
function(error, activity) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log(activity.friendlyName);
}
);
create
Parameters
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. |
var params = {"FriendlyName":"Activity1", "Available":"true"};
workspace.activities.create(
params,
function(error, activity) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+activity.friendlyName);
}
);
update
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
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. |
workspace.activities.fetch(
function(error, activityList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = activityList.data;
for(i=0; i<data.length; i++) {
var activity = data[i];
activity.update("WAxxx", "FriendlyName", "NewFriendlyName",
function(error, activity) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+activity.friendlyName);
}
);
}
}
);
List Resource Parameters
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. |
workspace.activities.update("WAxxx", "FriendlyName", "NewFriendlyName",
function(error, activity) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+activity.friendlyName);
}
);
delete
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
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. |
workspace.activities.fetch(
function(error, activityList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = activityList.data;
for(i=0; i<data.length; i++) {
var activity = data[i];
activity.delete(function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Activity deleted");
});
}
}
);
List Resource Parameters
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. |
workspace.activities.delete("WAxxx"
function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Activity deleted");
}
);
workflows
Retrieves the object to retrieve the list of workflows, create a new workflow, fetch, update or delete a specific workflow.
fetch
Parameters
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. |
workspace.workflows.fetch(
function(error, workflowList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Parsing response");
var data = workflowList.data;
for(i=0; i<data.length; i++) {
console.log(data[i].friendlyName);
}
}
);
Fetching a specific Workflow
workspace.workflows.fetch("WWxxx",
function(error, workflow) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log(workflow.friendlyName);
}
);
create
Parameters
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. |
var workflowConfig = {"task_routing":{"default_filter":{"task_queue_sid":"WQxxx"}}};
var params = {"FriendlyName":"Workflow1", "AssignmentCallbackUrl":"http://requestb.in/1kmw9im1", "Configuration":workflowConfig};
workspace.workflows.create(
params,
function(error, workflow) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+workflow.friendlyName);
}
);
update
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
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. |
workspace.workflows.fetch(
function(error, workflowList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = workflowList.data;
for(i=0; i<data.length; i++) {
var workflow = data[i];
workflow.update("WWxxx", "TaskReservationTimeout", "300",
function(error, workflow) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("TaskReservationTimeout: "+workflow.taskReservationTimeout);
}
);
}
}
);
List Resource Parameters
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. |
workspace.workflows.update("WWxxx", "TaskReservationTimeout", "300",
function(error, workflow) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("TaskReservationTimeout: "+workflow.taskReservationTimeout);
}
);
delete
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
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. |
workspace.workflows.fetch(
function(error, workflowList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = workflowList.data;
for(i=0; i<data.length; i++) {
var workflow = data[i];
workflow.delete(function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Workflow deleted");
}
);
}
}
);
List Resource Parameters
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. |
workspace.workflows.delete("WWxxx"
function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Workflow deleted");
}
);
statistics
Retrieves the object to retrieve the statistics for a workflow.
fetch
Parameters
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. |
var queryParams = {"Minutes":"240"};
workflow.statistics.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workflow statistics: "+JSON.stringify(statistics));
}
);
Cumulative Stats:
var queryParams = {"Minutes":"240"};
workflow.cumulativeStats.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workflow statistics: "+JSON.stringify(statistics));
}
);
RealTime Stats:
workflow.realtimeStats.fetch(
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workflow statistics: "+JSON.stringify(statistics));
}
);
taskqueues
Retrieves the object to retrieve the list of taskqueues, create a new taskqueue, fetch, update or delete a specific taskqueue.
fetch
Parameters
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. |
workspace.taskqueues.fetch(
function(error, taskQueueList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Parsing response");
var data = taskQueueList.data;
for(i=0; i<data.length; i++) {
console.log(data[i].friendlyName);
}
}
);
Fetching a specific TaskQueue
workspace.taskqueues.fetch("WQxxx",
function(error, taskQueue) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log(taskQueue.friendlyName);
}
);
create
Parameters
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. |
var params = {"FriendlyName":"TaskQueue1", "ReservationActivitySid":"WAxxx", "AssignmentActivitySid":"WAxxx", "TargetWorkers":"1==1"};
workspace.taskqueues.create(
params,
function(error, taskQueue) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+taskQueue.friendlyName);
}
);
update
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
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. |
workspace.taskqueues.fetch(
function(error, taskQueueList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = taskQueueList.data;
for(i=0; i<data.length; i++) {
var taskqueue = data[i];
taskqueue.update("WWxxx", "MaxReservedWorkers", "20",
function(error, taskqueue) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("MaxReservedWorkers: "+taskqueue.maxReservedWorkers);
}
);
}
}
);
List Resource Parameters
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. |
workspace.taskqueues.update("WQxxx", "MaxReservedWorkers", "20",
function(error, taskqueue) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("MaxReservedWorkers: "+taskqueue.maxReservedWorkers);
}
);
delete
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
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. |
workspace.taskqueues.fetch(
function(error, taskQueueList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = taskQueueList.data;
for(i=0; i<data.length; i++) {
var taskqueue = data[i];
taskqueue.delete(function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("TaskQueue deleted");
}
);
}
}
);
List Resource Parameters
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. |
workspace.taskqueues.delete("WQxxx"
function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("TaskQueue deleted");
}
);
statistics
Retrieves the object to retrieve the statistics for a taskqueue.
fetch
Parameters
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:
var queryParams = {"Minutes":"240"};
workspace.taskqueues.statistics.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched taskqueues statistics: "+JSON.stringify(statistics));
}
);
Single TaskQueue:
var queryParams = {"Minutes":"240"};
taskqueue.statistics.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));
}
);
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:
taskqueue.cumulativeStats.fetch(
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));
console.log("avg task acceptance time: "+statistics.avgTaskAcceptanceTime;
}
);
RealTime Stats:
var queryParams = {"Minutes":"240"};
taskqueue.realtimeStats.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched taskqueue statistics: "+JSON.stringify(statistics));
console.log("total available workers: "+statistics.totalAvailableWorkers;
}
);
workers
Retrieves the object to retrieve the list of workers, create a new worker, fetch, update or delete a specific worker.
fetch
Parameters
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. |
workspace.workers.fetch(
function(error, workerList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Parsing response");
var data = workerList.data;
for(i=0; i<data.length; i++) {
console.log(data[i].friendlyName);
}
}
);
Fetching a specific Worker
workspace.workers.fetch("WKxxx",
function(error, worker) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log(worker.friendlyName);
}
);
create
Parameters
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. |
var params = {"FriendlyName":"Worker1"};
workspace.workers.create(
params,
function(error, worker) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+worker.friendlyName);
}
);
update
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
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. |
workspace.workers.fetch(
function(error, workerList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = workerList.data;
for(i=0; i<data.length; i++) {
var worker = data[i];
worker.update("WKxxx", "ActivitySid", "WAxxx",
function(error, worker) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+worker.friendlyName);
}
);
}
}
);
List Resource Parameters
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. |
workspace.workers.update("WKxxx", "ActivitySid", "WAxxx",
function(error, worker) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("FriendlyName: "+worker.friendlyName);
}
);
delete
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
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. |
workspace.workers.fetch(
function(error, workerList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = workerList.data;
for(i=0; i<data.length; i++) {
var worker = data[i];
worker.delete(function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Worker deleted");
}
);
}
}
);
List Resource Parameters
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. |
workspace.workers.delete("WKxxx"
function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Worker deleted");
}
);
statistics
Retrieves the object to retrieve the statistics for a worker.
fetch
Parameters
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:
var queryParams = {"Minutes":"240"};
workspace.workers.statistics.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workers statistics: "+JSON.stringify(statistics));
}
);
List of Workers Cumulative Stats:
var queryParams = {"Minutes":"240"};
workspace.workers.cumulativeStats.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workers statistics: "+JSON.stringify(statistics));
}
);
List of Workers RealTime Stats:
workspace.workers.realtimeStats.fetch(
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workers statistics: "+JSON.stringify(statistics));
}
);
Single Worker:
var queryParams = {"Minutes":"240"};
worker.statistics.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched worker statistics: "+JSON.stringify(statistics));
}
);
tasks
Retrieves the object to retrieve the list of tasks, create a new task, fetch, update or delete a specific task.
fetch
Parameters
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. |
workspace.tasks.fetch(
function(error, taskList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Parsing response");
var data = taskList.data;
for(i=0; i<data.length; i++) {
console.log(JSON.stringify(data[i].attributes));
}
}
);
Fetching a specific Task
workspace.tasks.fetch("WTxxx",
function(error, task) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log(JSON.stringify(task.attributes));
}
);
create
Parameters
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. |
var attributes = "{\"Ticket\":\"Gold\"}";
var params = {"WorkflowSid":"WWxxx", "Attributes":attributes};
workspace.tasks.create(
params,
function(error, task) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("TaskSid: "+task.sid);
}
);
update
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
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. |
workspace.tasks.fetch(
function(error, taskList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = taskList.data;
var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}
for(i=0; i<data.length; i++) {
var task = data[i];
task.update("WTxxx", cancelAttrs,
function(error, task) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Attributes: "+JSON.stringify(task.attributes));
}
);
}
}
);
List Resource Parameters
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. |
var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}
workspace.tasks.update("WTxxx", cancelAttrs,
function(error, task) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Attributes: "+JSON.stringify(task.attributes));
}
);
delete
You can delete a Task resource in two manners:
- Deleting on an Instance Resource
- Deleting on the List Resource passing a specific SID
Instance Resource Parameters
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. |
workspace.tasks.fetch(
function(error, taskList) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
var data = taskList.data;
for(i=0; i<data.length; i++) {
var task = data[i];
task.delete(function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("Task deleted");
}
);
}
}
);
List Resource Parameters
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. |
workspace.tasks.delete("WTxxx"
function(error) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("task deleted");
}
);
statistics
Retrieves the object to retrieve the statistics for a workspace.
fetch
Parameters
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. |
var queryParams = {"Minutes":"240"};
workspace.statistics.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workspace statistics: "+JSON.stringify(statistics));
}
);
Cumulative Stats:
var queryParams = {"Minutes":"240"};
workspace.cumulativeStats.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workspace statistics: "+JSON.stringify(statistics));
}
);
RealTime Stats:
var queryParams = {};
workspace.realtimeStats.fetch(
queryParams,
function(error, statistics) {
if(error) {
console.log(error.code);
console.log(error.message);
return;
}
console.log("fetched workspace statistics: "+JSON.stringify(statistics));
}
);
on(event, callback)
Attaches a listener to the specified event. See Events for the complete list of supported events.
Parameters
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. |
Example
workspace.on("ready", function(workspace) {
console.log(workspace.friendlyName) // MyWorkspace
});
Events
TaskRouter's JS library currently raises the following events to the registered Workspace object:
ready
The Workspace has established a connection to TaskRouter and has completed initialization.
Parameters
Name | Type | Description |
---|---|---|
workspace | Workspace | The Workspace object for the Workspace you've created. |
Example
workspace.on("ready", function(workspace) {
console.log(workspace.friendlyName) // MyWorkspace
});
connected
The Workspace has established a connection to TaskRouter.
Example
workspace.on("connected", function() {
console.log("Websocket has connected");
});
disconnected
The Workspace has disconnected from TaskRouter.
Example
workspace.on("disconnected", function() {
console.log("Websocket has disconnected");
});
token.expired
Raised when the TaskRouter capability token used to create this Workspace expires.
Example
workspace.on("token.expired", function() {
console.log("updating token");
var token = refreshJWT(); // your method to retrieve a new capability token
workspace.updateToken(token);
});
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.