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:
_10<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:
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:
_10var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);
The library will raise a 'ready' event once it has connected to TaskRouter:
_10workspace.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 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. |
_10var workspace = new Twilio.TaskRouter.Workspace(WORKSPACE_TOKEN);
Turning off debugging:
_10var 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. |
_10workspace.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});
_10var props = {"EventCallbackUrl", "http://requestb.in/1kmw9im1", "TimeoutActivitySid":"WAxxx"};_10workspace.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});
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. |
_10workspace.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});
Updates the TaskRouter capability token for the Workspace.
Name | Type | Description |
---|---|---|
workspaceToken | String | A valid TaskRouter capability token. |
_10var token = refreshJWT(); // your method to retrieve a new capability token_10workspace.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. |
_14workspace.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);
_10workspace.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);
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. |
_12var params = {"FriendlyName":"Activity1", "Available":"true"};_12workspace.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:
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. |
_23workspace.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);
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. |
_10workspace.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:
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. |
_21workspace.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);
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. |
_10workspace.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.
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. |
_14workspace.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);
_10workspace.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);
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. |
_13var workflowConfig = {"task_routing":{"default_filter":{"task_queue_sid":"WQxxx"}}};_13var params = {"FriendlyName":"Workflow1", "AssignmentCallbackUrl":"http://requestb.in/1kmw9im1", "Configuration":workflowConfig};_13workspace.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:
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. |
_23workspace.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);
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. |
_10workspace.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:
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. |
_22workspace.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);
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. |
_10workspace.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.
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. |
_12var queryParams = {"Minutes":"240"};_12workflow.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:
_12var queryParams = {"Minutes":"240"};_12workflow.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:
_10workflow.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.
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. |
_14workspace.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);
_10workspace.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);
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. |
_12var params = {"FriendlyName":"TaskQueue1", "ReservationActivitySid":"WAxxx", "AssignmentActivitySid":"WAxxx", "TargetWorkers":"1==1"};_12workspace.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:
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. |
_23workspace.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);
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. |
_10workspace.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:
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. |
_22workspace.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);
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. |
_10workspace.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.
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:
_12var queryParams = {"Minutes":"240"};_12workspace.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:
_12var queryParams = {"Minutes":"240"};_12taskqueue.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:
_11taskqueue.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:
_13var queryParams = {"Minutes":"240"};_13taskqueue.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.
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. |
_14workspace.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);
_10workspace.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);
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. |
_12var params = {"FriendlyName":"Worker1"};_12workspace.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:
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. |
_23workspace.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);
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. |
_10workspace.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:
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. |
_22workspace.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);
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. |
_10workspace.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.
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:
_12var queryParams = {"Minutes":"240"};_12workspace.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:
_12var queryParams = {"Minutes":"240"};_12workspace.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:
_10workspace.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:
_12var queryParams = {"Minutes":"240"};_12worker.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.
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. |
_14workspace.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);
_10workspace.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);
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. |
_13var attributes = "{\"Ticket\":\"Gold\"}";_13var params = {"WorkflowSid":"WWxxx", "Attributes":attributes};_13workspace.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:
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. |
_24workspace.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);
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. |
_11var cancelAttrs = {"AssignmentStatus":"canceled", "Reason":"waiting"}_11workspace.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:
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. |
_22workspace.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);
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. |
_10workspace.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.
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. |
_12var queryParams = {"Minutes":"240"};_12workspace.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:
_12var queryParams = {"Minutes":"240"};_12workspace.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:
_12var queryParams = {};_12workspace.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);
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. |
_10workspace.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.
Name | Type | Description |
---|---|---|
workspace | Workspace | The Workspace object for the Workspace you've created. |
_10workspace.on("ready", function(workspace) {_10 console.log(workspace.friendlyName) // MyWorkspace_10});
The Workspace has established a connection to TaskRouter.
_10workspace.on("connected", function() {_10 console.log("Websocket has connected");_10});
The Workspace has disconnected from TaskRouter.
_10workspace.on("disconnected", function() {_10 console.log("Websocket has disconnected");_10});
Raised when the TaskRouter capability token used to create this Workspace expires.
_10workspace.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});