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 type="text/javascript" src="//media.twiliocdn.com/taskrouter/js/v1.13/taskrouter.min.js"></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:

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
SDK Version:
  • 6.x
  • 7.x
SDK Version:
  • 2.x
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 5.x
  • 6.x
SDK Version:
  • 4.x
  • 5.x
// Download the Node helper library from twilio.com/docs/node/install
// These vars are your accountSid and authToken from twilio.com/user/account
var twilio = require('twilio');

var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var authToken = "your_auth_token";
var workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var workerSid = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

var capability = new twilio.TaskRouterWorkspaceCapability(accountSid, authToken, workspaceSid, workerSid);
capability.allowFetchSubresources();
capability.allowUpdatesSubresources();
capability.allowDeleteSubresources();
var token = capability.generate();

// By default, tokens are good for one hour.
// Override this default timeout by specifiying a new value (in seconds).
// For example, to generate a token good for 8 hours:

var token = capability.generate(28800);  // 60 * 60 * 8
// This snippets constructs the same policy list as seen here:
// https://www.twilio.com/docs/api/taskrouter/constructing-jwts as a base

// Download the Node helper library from twilio.com/docs/node/install
// These consts are your accountSid and authToken from https://www.twilio.com/console
const taskrouter = require('twilio').jwt.taskrouter;
const util = taskrouter.util;

const TaskRouterCapability = taskrouter.TaskRouterCapability;
const Policy = TaskRouterCapability.Policy;

const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const workspaceSid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const workerSid = 'WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
const version = 'v1';

const capability = new TaskRouterCapability({
  accountSid: accountSid,
  authToken: authToken,
  workspaceSid: workspaceSid,
  channelId: workspaceSid,
});

// Helper function to create Policy
function buildWorkspacePolicy(options) {
  options = options || {};
  const resources = options.resources || [];
  const urlComponents = [
    TASKROUTER_BASE_URL,
    version,
    'Workspaces',
    workspaceSid,
  ];

  return new Policy({
    url: urlComponents.concat(resources).join('/'),
    method: options.method || 'GET',
    allow: true,
  });
}

// Event Bridge Policies
const eventBridgePolicies = util.defaultEventBridgePolicies(
  accountSid,
  workspaceSid
);

const workspacePolicies = [
  // Workspace Policy
  buildWorkspacePolicy(),
  // Workspace subresources fetch Policy
  buildWorkspacePolicy({ resources: ['**'] }),
  // Workspace resources update Policy
  buildWorkspacePolicy({ resources: ['**'], method: 'POST' }),
  // Workspace resources delete Policy
  buildWorkspacePolicy({ resources: ['**'], method: 'DELETE' }),
];

eventBridgePolicies.concat(workspacePolicies).forEach(policy => {
  capability.addPolicy(policy);
});

const token = capability.toJwt();
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio.TaskRouter;

class Example
{
  static void Main(string[] args)
  {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    string AuthToken = "your_auth_token";
    string WorkspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    TaskRouterWorkspaceCapability capability = new TaskRouterWorkspaceCapability(AccountSid, AuthToken, WorkspaceSid);
    capability.AllowFetchSubresources();
    capability.AllowUpdatesSubresources();
    capability.AllowDeleteSubresources();
    var token = capability.GenerateToken();

    // By default, tokens are good for one hour.
    // Override this default timeout by specifiying a new value (in seconds).
    // For example, to generate a token good for 8 hours:

    token = capability.GenerateToken(28800);  // 60 * 60 * 8
  }
}
# Get twilio-ruby from twilio.com/docs/ruby/install
require 'twilio-ruby'

# Get your Account Sid and Auth Token from twilio.com/user/account
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
workspace_sid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

capability = Twilio::TaskRouter::WorkspaceCapability.new account_sid,
                                                         auth_token,
                                                         workspace_sid
capability.allow_fetch_subresources
capability.allow_updates_subresources
capability.allow_delete_subresources

# By default, tokens are good for one hour.
# Override this default timeout by specifiying a new value (in seconds).
# For example, to generate a token good for 8 hours:

capability.generate_token 28_800 # 60 * 60 * 8
// Download the twilio-csharp library from
// https://www.twilio.com/docs/libraries/csharp#installation
using System;
using System.Collections.Generic;
using Twilio.Http;
using Twilio.Jwt.Taskrouter;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        const string workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        var urls = new PolicyUrlUtils(workspaceSid);

        var allowFetchSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Get);
        var allowUpdatesSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Post);
        var allowDeleteSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Delete);


        var policies = new List<Policy>
        {
            allowFetchSubresources,
            allowUpdatesSubresources,
            allowUpdatesSubresources
        };

        // By default, tokens are good for one hour.
        // Override this default timeout by specifiying a new value (in seconds).
        // For example, to generate a token good for 8 hours:
        var capability = new TaskRouterCapability(
            accountSid,
            authToken,
            workspaceSid,
            null,
            policies: policies,
            expiration: DateTime.UtcNow.AddSeconds(28800)); // 60 * 60 * 8

        Console.WriteLine(capability.ToJwt());
    }
}

class PolicyUrlUtils
{
    const string taskRouterBaseUrl = "https://taskrouter.twilio.com";
    const string taskRouterVersion = "v1";

    readonly string _workspaceSid;

    public PolicyUrlUtils(string workspaceSid)
    {
        _workspaceSid = workspaceSid;
    }

    public string Workspace =>
        $"{taskRouterBaseUrl}/{taskRouterVersion}/Workspaces/{_workspaceSid}";
}
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Jwt\TaskRouter\WorkspaceCapability;

// Your Account Sid and Auth Token from twilio.com/user/account
$accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$authToken = "your_auth_token";
$workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$capability = new WorkspaceCapability($accountSid, $authToken, $workspaceSid);
$capability->allowFetchSubresources();
$capability->allowUpdatesSubresources();
$capability->allowDeleteSubresources();
$token = $capability->generateToken();
// By default, tokens are good for one hour.
// Override this default timeout by specifiying a new value (in seconds).
// For example, to generate a token good for 8 hours:
$workerToken = $capability->generateToken(28800);  // 60 * 60 * 8
# Download the Python helper library from twilio.com/docs/python/install
from twilio import jwt
from twilio.task_router import TaskRouterWorkspaceCapability

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token  = "your_auth_token"
workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

capability = TaskRouterWorkspaceCapability(account_sid, auth_token, workspace_sid)
capability.allow_fetch_subresources()
capability.allow_updates_subresources()
capability.allow_delete_subresources()
token = capability.generate_token()

# By default, tokens are good for one hour.
# Override this default timeout by specifiying a new value (in seconds).
# For example, to generate a token good for 8 hours:

token = capability.generate_token(28800); # 60 * 60 * 8
# Get twilio-ruby from twilio.com/docs/ruby/install
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
workspace_sid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

capability = Twilio::JWT::TaskRouterCapability.new(
 (account_sid, auth_token), workspace_sid, workspace_sid
)

allow_fetch_subresources = Twilio::JWT::TaskRouterCapability::Policy.new(
  Twilio::JWT::TaskRouterCapability::TaskRouterUtils.all_workspaces,
  'GET',
  true
)
capability.add_policy(allow_fetch_subresources)

allow_updates_subresources = Twilio::JWT::TaskRouterCapability::Policy.new(
  Twilio::JWT::TaskRouterCapability::TaskRouterUtils.all_workspaces,
  'POST',
  true
)
capability.add_policy(allow_updates_subresources)

allow_delete_subresources = Twilio::JWT::TaskRouterCapability::Policy.new(
  Twilio::JWT::TaskRouterCapability::TaskRouterUtils.all_workspaces,
  'DELETE',
  true
)
capability.add_policy(allow_delete_subresources)

puts capability.to_s
// Install the Java helper library from twilio.com/docs/java/install
import com.twilio.sdk.taskrouter.TaskRouterWorkspaceCapability;
import com.twilio.sdk.CapabilityToken.DomainException;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  private static final String AUTH_TOKEN = "your_auth_token";
  private static final String WORKSPACE_SID = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

  public static void main(String[] args) throws DomainException {
    TaskRouterWorkspaceCapability capability = new TaskRouterWorkspaceCapability(ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID);
    capability.allowFetchSubresources();
    capability.allowUpdatesSubresources();
    capability.allowDeleteSubresources();
    String token = capability.generateToken();

    // By default, tokens are good for one hour.
    // Override this default timeout by specifiying a new value (in seconds).
    // For example, to generate a token good for 8 hours:

    token = capability.generateToken(28800);  // 60 * 60 * 8
  }
}
# Download the Python helper library from twilio.com/docs/python/install
from twilio.jwt.taskrouter.capabilities import WorkspaceCapabilityToken

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

capability = WorkspaceCapabilityToken(
    account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid
)

capability.allow_fetch_subresources()
capability.allow_update_subresources()
capability.allow_delete_subresources()

token = capability.to_jwt()

# By default, tokens are good for one hour.
# Override this default timeout by specifiying a new value (in seconds).
# For example, to generate a token good for 8 hours:

# 60 * 60 * 8 = 28800
token = capability.to_jwt(ttl=28800)

print(token)
// Install the Java helper library from twilio.com/docs/java/install
import java.util.List;

import com.google.common.collect.Lists;
import com.twilio.http.HttpMethod;
import com.twilio.jwt.taskrouter.Policy;
import com.twilio.jwt.taskrouter.TaskRouterCapability;
import com.twilio.jwt.taskrouter.UrlUtils;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  private static final String AUTH_TOKEN = "your_auth_token";
  private static final String WORKSPACE_SID = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

  public static void main(String[] args) {
    Policy allowFetchSubresources = new Policy.Builder()
        .url(UrlUtils.workspace(WORKSPACE_SID) + "/**")
        .build();

    Policy allowUpdatesSubresources = new Policy.Builder()
        .url(UrlUtils.workspace(WORKSPACE_SID) + "/**")
        .method(HttpMethod.POST)
        .build();

    Policy allowDeleteSubresources = new Policy.Builder()
        .url(UrlUtils.workspace(WORKSPACE_SID) + "/**")
        .method(HttpMethod.DELETE)
        .build();


    List<Policy> policies = Lists.newArrayList(allowFetchSubresources, allowUpdatesSubresources,
        allowDeleteSubresources);


    TaskRouterCapability.Builder capabilityBuilder =
        new TaskRouterCapability.Builder(ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID, WORKSPACE_SID)
            .policies(policies);

    String token = capabilityBuilder.build().toJwt();

    System.out.println(token);

    // By default, tokens are good for one hour.
    // Override this default timeout by specifiying a new value (in seconds).
    // For example, to generate a token good for 8 hours:
    token = capabilityBuilder.ttl(28800).build().toJwt();

    System.out.println(token);
  }
}
Creating a TaskRouter Workspace capability token

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.

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 browsing the Twilio tag on Stack Overflow.

Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
SDK Version:
  • 6.x
  • 7.x
SDK Version:
  • 2.x
  • 3.x
SDK Version:
  • 5.x
SDK Version:
  • 5.x
  • 6.x
SDK Version:
  • 4.x
  • 5.x
// Download the Node helper library from twilio.com/docs/node/install
// These vars are your accountSid and authToken from twilio.com/user/account
var twilio = require('twilio');

var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var authToken = "your_auth_token";
var workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var workerSid = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

var capability = new twilio.TaskRouterWorkspaceCapability(accountSid, authToken, workspaceSid, workerSid);
capability.allowFetchSubresources();
capability.allowUpdatesSubresources();
capability.allowDeleteSubresources();
var token = capability.generate();

// By default, tokens are good for one hour.
// Override this default timeout by specifiying a new value (in seconds).
// For example, to generate a token good for 8 hours:

var token = capability.generate(28800);  // 60 * 60 * 8
// This snippets constructs the same policy list as seen here:
// https://www.twilio.com/docs/api/taskrouter/constructing-jwts as a base

// Download the Node helper library from twilio.com/docs/node/install
// These consts are your accountSid and authToken from https://www.twilio.com/console
const taskrouter = require('twilio').jwt.taskrouter;
const util = taskrouter.util;

const TaskRouterCapability = taskrouter.TaskRouterCapability;
const Policy = TaskRouterCapability.Policy;

const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'your_auth_token';
const workspaceSid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const workerSid = 'WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
const version = 'v1';

const capability = new TaskRouterCapability({
  accountSid: accountSid,
  authToken: authToken,
  workspaceSid: workspaceSid,
  channelId: workspaceSid,
});

// Helper function to create Policy
function buildWorkspacePolicy(options) {
  options = options || {};
  const resources = options.resources || [];
  const urlComponents = [
    TASKROUTER_BASE_URL,
    version,
    'Workspaces',
    workspaceSid,
  ];

  return new Policy({
    url: urlComponents.concat(resources).join('/'),
    method: options.method || 'GET',
    allow: true,
  });
}

// Event Bridge Policies
const eventBridgePolicies = util.defaultEventBridgePolicies(
  accountSid,
  workspaceSid
);

const workspacePolicies = [
  // Workspace Policy
  buildWorkspacePolicy(),
  // Workspace subresources fetch Policy
  buildWorkspacePolicy({ resources: ['**'] }),
  // Workspace resources update Policy
  buildWorkspacePolicy({ resources: ['**'], method: 'POST' }),
  // Workspace resources delete Policy
  buildWorkspacePolicy({ resources: ['**'], method: 'DELETE' }),
];

eventBridgePolicies.concat(workspacePolicies).forEach(policy => {
  capability.addPolicy(policy);
});

const token = capability.toJwt();
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio.TaskRouter;

class Example
{
  static void Main(string[] args)
  {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    string AuthToken = "your_auth_token";
    string WorkspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    TaskRouterWorkspaceCapability capability = new TaskRouterWorkspaceCapability(AccountSid, AuthToken, WorkspaceSid);
    capability.AllowFetchSubresources();
    capability.AllowUpdatesSubresources();
    capability.AllowDeleteSubresources();
    var token = capability.GenerateToken();

    // By default, tokens are good for one hour.
    // Override this default timeout by specifiying a new value (in seconds).
    // For example, to generate a token good for 8 hours:

    token = capability.GenerateToken(28800);  // 60 * 60 * 8
  }
}
# Get twilio-ruby from twilio.com/docs/ruby/install
require 'twilio-ruby'

# Get your Account Sid and Auth Token from twilio.com/user/account
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
workspace_sid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

capability = Twilio::TaskRouter::WorkspaceCapability.new account_sid,
                                                         auth_token,
                                                         workspace_sid
capability.allow_fetch_subresources
capability.allow_updates_subresources
capability.allow_delete_subresources

# By default, tokens are good for one hour.
# Override this default timeout by specifiying a new value (in seconds).
# For example, to generate a token good for 8 hours:

capability.generate_token 28_800 # 60 * 60 * 8
// Download the twilio-csharp library from
// https://www.twilio.com/docs/libraries/csharp#installation
using System;
using System.Collections.Generic;
using Twilio.Http;
using Twilio.Jwt.Taskrouter;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        const string workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        var urls = new PolicyUrlUtils(workspaceSid);

        var allowFetchSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Get);
        var allowUpdatesSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Post);
        var allowDeleteSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Delete);


        var policies = new List<Policy>
        {
            allowFetchSubresources,
            allowUpdatesSubresources,
            allowUpdatesSubresources
        };

        // By default, tokens are good for one hour.
        // Override this default timeout by specifiying a new value (in seconds).
        // For example, to generate a token good for 8 hours:
        var capability = new TaskRouterCapability(
            accountSid,
            authToken,
            workspaceSid,
            null,
            policies: policies,
            expiration: DateTime.UtcNow.AddSeconds(28800)); // 60 * 60 * 8

        Console.WriteLine(capability.ToJwt());
    }
}

class PolicyUrlUtils
{
    const string taskRouterBaseUrl = "https://taskrouter.twilio.com";
    const string taskRouterVersion = "v1";

    readonly string _workspaceSid;

    public PolicyUrlUtils(string workspaceSid)
    {
        _workspaceSid = workspaceSid;
    }

    public string Workspace =>
        $"{taskRouterBaseUrl}/{taskRouterVersion}/Workspaces/{_workspaceSid}";
}
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Jwt\TaskRouter\WorkspaceCapability;

// Your Account Sid and Auth Token from twilio.com/user/account
$accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$authToken = "your_auth_token";
$workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$capability = new WorkspaceCapability($accountSid, $authToken, $workspaceSid);
$capability->allowFetchSubresources();
$capability->allowUpdatesSubresources();
$capability->allowDeleteSubresources();
$token = $capability->generateToken();
// By default, tokens are good for one hour.
// Override this default timeout by specifiying a new value (in seconds).
// For example, to generate a token good for 8 hours:
$workerToken = $capability->generateToken(28800);  // 60 * 60 * 8
# Download the Python helper library from twilio.com/docs/python/install
from twilio import jwt
from twilio.task_router import TaskRouterWorkspaceCapability

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token  = "your_auth_token"
workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

capability = TaskRouterWorkspaceCapability(account_sid, auth_token, workspace_sid)
capability.allow_fetch_subresources()
capability.allow_updates_subresources()
capability.allow_delete_subresources()
token = capability.generate_token()

# By default, tokens are good for one hour.
# Override this default timeout by specifiying a new value (in seconds).
# For example, to generate a token good for 8 hours:

token = capability.generate_token(28800); # 60 * 60 * 8
# Get twilio-ruby from twilio.com/docs/ruby/install
require 'twilio-ruby'

# Get your Account SID and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
workspace_sid = 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

capability = Twilio::JWT::TaskRouterCapability.new(
 (account_sid, auth_token), workspace_sid, workspace_sid
)

allow_fetch_subresources = Twilio::JWT::TaskRouterCapability::Policy.new(
  Twilio::JWT::TaskRouterCapability::TaskRouterUtils.all_workspaces,
  'GET',
  true
)
capability.add_policy(allow_fetch_subresources)

allow_updates_subresources = Twilio::JWT::TaskRouterCapability::Policy.new(
  Twilio::JWT::TaskRouterCapability::TaskRouterUtils.all_workspaces,
  'POST',
  true
)
capability.add_policy(allow_updates_subresources)

allow_delete_subresources = Twilio::JWT::TaskRouterCapability::Policy.new(
  Twilio::JWT::TaskRouterCapability::TaskRouterUtils.all_workspaces,
  'DELETE',
  true
)
capability.add_policy(allow_delete_subresources)

puts capability.to_s
// Install the Java helper library from twilio.com/docs/java/install
import com.twilio.sdk.taskrouter.TaskRouterWorkspaceCapability;
import com.twilio.sdk.CapabilityToken.DomainException;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  private static final String AUTH_TOKEN = "your_auth_token";
  private static final String WORKSPACE_SID = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

  public static void main(String[] args) throws DomainException {
    TaskRouterWorkspaceCapability capability = new TaskRouterWorkspaceCapability(ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID);
    capability.allowFetchSubresources();
    capability.allowUpdatesSubresources();
    capability.allowDeleteSubresources();
    String token = capability.generateToken();

    // By default, tokens are good for one hour.
    // Override this default timeout by specifiying a new value (in seconds).
    // For example, to generate a token good for 8 hours:

    token = capability.generateToken(28800);  // 60 * 60 * 8
  }
}
# Download the Python helper library from twilio.com/docs/python/install
from twilio.jwt.taskrouter.capabilities import WorkspaceCapabilityToken

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

capability = WorkspaceCapabilityToken(
    account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid
)

capability.allow_fetch_subresources()
capability.allow_update_subresources()
capability.allow_delete_subresources()

token = capability.to_jwt()

# By default, tokens are good for one hour.
# Override this default timeout by specifiying a new value (in seconds).
# For example, to generate a token good for 8 hours:

# 60 * 60 * 8 = 28800
token = capability.to_jwt(ttl=28800)

print(token)
// Install the Java helper library from twilio.com/docs/java/install
import java.util.List;

import com.google.common.collect.Lists;
import com.twilio.http.HttpMethod;
import com.twilio.jwt.taskrouter.Policy;
import com.twilio.jwt.taskrouter.TaskRouterCapability;
import com.twilio.jwt.taskrouter.UrlUtils;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  private static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  private static final String AUTH_TOKEN = "your_auth_token";
  private static final String WORKSPACE_SID = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

  public static void main(String[] args) {
    Policy allowFetchSubresources = new Policy.Builder()
        .url(UrlUtils.workspace(WORKSPACE_SID) + "/**")
        .build();

    Policy allowUpdatesSubresources = new Policy.Builder()
        .url(UrlUtils.workspace(WORKSPACE_SID) + "/**")
        .method(HttpMethod.POST)
        .build();

    Policy allowDeleteSubresources = new Policy.Builder()
        .url(UrlUtils.workspace(WORKSPACE_SID) + "/**")
        .method(HttpMethod.DELETE)
        .build();


    List<Policy> policies = Lists.newArrayList(allowFetchSubresources, allowUpdatesSubresources,
        allowDeleteSubresources);


    TaskRouterCapability.Builder capabilityBuilder =
        new TaskRouterCapability.Builder(ACCOUNT_SID, AUTH_TOKEN, WORKSPACE_SID, WORKSPACE_SID)
            .policies(policies);

    String token = capabilityBuilder.build().toJwt();

    System.out.println(token);

    // By default, tokens are good for one hour.
    // Override this default timeout by specifiying a new value (in seconds).
    // For example, to generate a token good for 8 hours:
    token = capabilityBuilder.ttl(28800).build().toJwt();

    System.out.println(token);
  }
}