Skip to contentSkip to navigationSkip to topbar
On this page

Creating Tasks and Accepting Reservations: Accept a Reservation using the REST API


To indicate that a Worker has accepted or rejected a Task, you make an HTTP POST request to a Reservation instance resource. To do that, we need the TaskSid, which is available via the web portal, and the ReservationSid. The ReservationSid was passed to our Assignment Callback URL when a Worker was reserved for our Task. Using the ngrok inspector page at http://localhost:4040, we can easily find the request parameters sent from TaskRouter and copy the ReservationSid to our clipboard. **

(information)

Info

The Reservation API resource is ephemeral and exists only within the context of a Task. As such, it doesn't have its own primary API resource and you'll find it documented in the Tasks resource section of the reference documentation.

With our trusty TaskSid and ReservationSid in hand, let's make another REST API request to accept our Task Reservation. We'll add on to our TwilioTaskRouterServlet to accept a reservation with our web server. Remember to substitute your own account details in place of the curly braces.


TwilioTaskRouterServlet.java

twiliotaskrouterservletjava page anchor
1
import java.io.IOException;
2
3
import javax.servlet.http.HttpServlet;
4
import javax.servlet.http.HttpServletRequest;
5
import javax.servlet.http.HttpServletResponse;
6
7
import com.twilio.Twilio;
8
import com.twilio.rest.taskrouter.v1.workspace.Task;
9
import com.twilio.rest.taskrouter.v1.workspace.task.Reservation;
10
11
public class TwilioTaskRouterServlet extends HttpServlet {
12
13
private String accountSid;
14
private String authToken;
15
private String workspaceSid;
16
private String workflowSid;
17
18
@Override
19
public void init() {
20
accountSid = this.getServletConfig().getInitParameter("AccountSid");
21
authToken = this.getServletConfig().getInitParameter("AuthToken");
22
workspaceSid = this.getServletConfig().getInitParameter("WorkspaceSid");
23
workflowSid = this.getServletConfig().getInitParameter("WorkflowSid");
24
25
Twilio.init(accountSid, authToken);
26
}
27
28
// service() responds to both GET and POST requests.
29
// You can also use doGet() or doPost()
30
@Override
31
public void service(final HttpServletRequest request, final HttpServletResponse response)
32
throws IOException {
33
if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) {
34
return;
35
}
36
37
if (request.getPathInfo().equals("/assignment_callback")) {
38
response.setContentType("application/json");
39
response.getWriter().print("{}");
40
} else if (request.getPathInfo().equals("/create_task")) {
41
response.setContentType("application/json");
42
response.getWriter().print(createTask());
43
} else if (request.getPathInfo().equals("/accept_reservation")) {
44
response.setContentType("application/json");
45
final String taskSid = request.getParameter("TaskSid");
46
final String reservationSid = request.getParameter("ReservationSid");
47
response.getWriter().print(acceptReservation(taskSid, reservationSid));
48
}
49
}
50
51
public String createTask() {
52
String attributes = "{\"selected_language\":\"es\"}";
53
54
Task task = Task.creator(workspaceSid, attributes, workflowSid).create();
55
56
return "{\"task_sid\":\"" + task.getSid() + "\"}";
57
}
58
59
public String acceptReservation(final String taskSid, final String reservationSid) {
60
Reservation reservation = Reservation.updater(workspaceSid, taskSid, reservationSid)
61
.setReservationStatus(Reservation.Status.ACCEPTED)
62
.update();
63
64
return "{\"worker_name\":\"" + reservation.getWorkerName() + "\"}";
65
}
66
}

If you'd like to use curl instead, put the following into your terminal:

1
curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks/{TaskSid}/Reservations/{ReservationSid} \
2
-d ReservationStatus=accepted \
3
-u {AccountSid}:{AuthToken}

Examining the response from TaskRouter, we see that the Task Reservation has been accepted, and the Task has been assigned to the Worker Alice:

{... "worker_name": "Alice", "reservation_status": "accepted", ...}

If you don't see this, it's possible that your Reservation has timed out. If this is the case, set your Worker back to an available Activity state and create another Task. To prevent this occurring, you can increase the 'Task Reservation Timeout' value in your Workflow configuration.

With your Workspace open in the TaskRouter web portal(link takes you to an external page), click 'Workers' and you'll see that Alice has been transitioned to the 'Assignment Activity' of the TaskQueue that assigned the Task. In this case, "Busy":

Alice has been transitioned to the Busy Activity.

Hurrah! We've made it to the end of the Task lifecycle:

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

In the next steps, we'll examine more ways to perform common Task acceptance and rejection workflows.

Next: Accept a Reservation using Assignment Instructions »

** If you're not using ngrok or a similar tool, you can modify assignment.php to print the value of ReservationSid to your web server log. Or, you can use the Tasks REST API instance resource to look up the ReservationSid based on the TaskSid.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.