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

Creating Tasks and Accepting Reservations: Create a Task using the REST API


Recall the TaskRouter Task lifecycle:

Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.

Before we create our first Task, make sure that our Worker Alice is in a non-available Activity state. Bob's Activity state won't matter right now, as we will create a Spanish language Task that he is not eligible to handle.

With your Workspace open in the TaskRouter console(link takes you to an external page), click 'Workers' then click to edit Alice and set her Activity to 'Offline'. Your Workers should look like this:

Worker status — Alice is offline.

To simulate reality, we'll create a Task using the REST API rather than the web portal. We'll add on to our server.rb to create a task with our web server. Replace the {} with your Twilio AccountSid, Twilio AuthToken, WorkspaceSid, and WorkflowSid.

Download and extract Twilio's Ruby helper library(link takes you to an external page) into the same directory that you are working in. This is required as we're utilizing the helper library to create a task and will continue to use the library in subsequent steps.

server.rb


_32
require 'rubygems'
_32
require 'twilio-ruby'
_32
require 'sinatra'
_32
require 'json'
_32
set :port, 8080
_32
_32
# Get your Account Sid and Auth Token from twilio.com/user/account
_32
account_sid = '{{ account_sid }}'
_32
auth_token = '{{ auth_token }}'
_32
workspace_sid = '{{ workspace_sid }}'
_32
workflow_sid = '{{ workflow_sid }}'
_32
_32
client = Twilio::REST::Client.new(account_sid, auth_token)
_32
_32
post '/assignment_callback' do
_32
# Respond to assignment callbacks with empty 200 response
_32
content_type :json
_32
{}.to_json
_32
end
_32
_32
get '/create-task' do
_32
# Create a task
_32
task = client.taskrouter.workspaces(workspace_sid)
_32
.tasks
_32
.create(
_32
attributes: {
_32
'selected_language' => 'es'
_32
}.to_json,
_32
workflow_sid: workflow_sid
_32
)
_32
task.attributes
_32
end

Alternatively, we can also create a Task using the command line utility curl, which should exist on any Mac or Linux workstation. Again, remember to replace the {} with your matching credentials and Sids, then execute the following command at your terminal:


_10
curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks \
_10
--data-urlencode Attributes='{"selected_language": "es"}' \
_10
-d WorkflowSid={WorkflowSid} \
_10
-u {AccountSid}:{AuthToken}

You can find your Twilio AccountSid and AuthToken on the TaskRouter Getting Started page(link takes you to an external page) by clicking 'show API credentials'.

If you don't have curl, you can run this request using an HTTP test tool like Postman(link takes you to an external page) or using the Task creation dialog in the TaskRouter web portal: with your Workspace open, click 'Tasks' then 'Create Task'.

To see our newly created Task in the TaskRouter web portal, with your Workspace open, click 'Tasks' in the main navigation. Notice that the Task has been added to the "Customer Care Requests - Spanish" Task Queue based on the Attributes we provided in the curl request. The Assignment Status is 'pending' because there is no available Worker that matches the Task Queue:

New task list — pending task.

Make an Eligible Worker Available

make-an-eligible-worker-available page anchor

Look again at the TaskRouter Task lifecycle:

Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.

The first stage - 'Task Created' - is complete. To trigger an automatic Task Reservation, the next step is to bring en eligible Worker (in this case Alice) online.

With your Workspace open in the TaskRouter web portal, click 'Workers', then click to edit Alice and set her Activity to 'Idle':

Set Alice to Idle.

When you hit save, Twilio will create a Reservation between Alice and our Task and you will receive a Webhook request at the Assignment Callback URL that we set up in the previous step. If you're using ngrok, open http://localhost:4040 in your web browser to see a detailed log of the request that Twilio made to your server, including all the parameters that your server might use to determine whether to accept a Reservation:

Ngrok request log.

We're now one step further along the Task Reservation lifecycle:

Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.

Time to accept the Reservation.

Next: Accept a Reservation with the REST API »


Rate this page: