Skip to contentSkip to navigationSkip to topbar
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 web portal(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:

Ensure 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 Program.cs to create a task with our web server. Replace the with your Twilio AccountSid, Twilio AuthToken, WorkspaceSid, and WorkflowSid.

Download and extract Twilio's C# helper library(link takes you to an external page). Install the latest jar with dependencies into your web application library directory (for Tomcat this is taskrouter/WEB-INF/lib).


Program.cs

programcs page anchor
1
using System;
2
using System.Net;
3
using SimpleWebServer;
4
using Twilio;
5
using Twilio.Rest.Taskrouter.V1.Workspace.Task;
6
7
namespace taskroutercsharp
8
{
9
class MainClass
10
{
11
// Find your Account Sid and Auth Token at twilio.com/user/account
12
const string AccountSid = "{{ account_sid }}";
13
const string AuthToken = "{{ auth_token }}";
14
const string WorkspaceSid = "{{ workspace_sid }}";
15
const string WorkflowSid = "{{ workflow_sid }}";
16
17
public static void Main (string[] args)
18
{
19
TwilioClient.Init(AccountSid, AuthToken);
20
21
WebServer ws = new WebServer (SendResponse, "http://localhost:8080/");
22
ws.Run ();
23
Console.WriteLine ("A simple webserver. Press a key to quit.");
24
Console.ReadKey ();
25
ws.Stop ();
26
}
27
28
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
29
{
30
HttpListenerRequest request = ctx.Request;
31
HttpListenerResponse response = ctx.Response;
32
33
String endpoint = request.RawUrl;
34
35
if (endpoint.EndsWith("assignment_callback")) {
36
response.StatusCode = (int) HttpStatusCode.OK;
37
response.ContentType = "application/json";
38
response.StatusDescription = "{}";
39
return response;
40
} else if (endpoint.EndsWith ("create_task")) {
41
response.StatusCode = (int)HttpStatusCode.OK;
42
response.ContentType = "application/json";
43
var task = TaskResource.Create(
44
WorkspaceSid, attributes: "{\"selected_language\":\"es\"}", workflowSid: WorkflowSid);
45
response.StatusDescription = task.Attributes;
46
return response;
47
}
48
response.StatusCode = (int) HttpStatusCode.OK;
49
return response;
50
}
51
}
52
}

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:

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

You can find your Twilio AccountSid and AuthToken on the TaskRouter Getting Started page by clicking 'show API credentials'.

If you don't have curl, you can run this request using an HTTP test tool like Insomnia(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:

Task in the TaskRouter web portal.

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 »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.