# 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. \*\*

> \[!NOTE]
>
> 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](/docs/taskrouter/api/task) 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 run.py to accept a reservation with our web server. Remember to substitute your own account details in place of the curly braces.

## run.py

```python
from flask import Flask, request, Response
from twilio.rest import Client

app = Flask(__name__)

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "{{ account_sid }}"
auth_token  = "{{ auth_token }}"
workspace_sid = "{{ workspace_sid }}"
workflow_sid = "{{ workflow_sid }}"

client = Client(account_sid, auth_token)

@app.route("/assignment_callback", methods=['GET', 'POST'])
def assignment_callback():
    """Respond to assignment callbacks with empty 200 response"""

    resp = Response({}, status=200, mimetype='application/json')
    return resp

@app.route("/create_task", methods=['GET', 'POST'])
def create_task():
    """Creating a Task"""
    task = client.taskrouter.workspaces(workspace_sid).tasks.create(
        workflow_sid=workflow_sid,
        attributes='{"selected_language":"es"}'
    )
    print(task.attributes)
    resp = Response({}, status=200, mimetype='application/json')
    return resp

@app.route("/accept_reservation", methods=['GET', 'POST'])
def accept_reservation():
    """Accepting a Reservation"""
    task_sid = request.args.get('task_sid')
    reservation_sid = request.args.get('reservation_sid')

    reservation = client.taskrouter.workspaces(workspace_sid) \
                                   .tasks(task_sid) \
                                   .reservations(reservation_sid) \
                                   .update(reservation_status='accepted')
    print(reservation.reservation_status)
    print(reservation.worker_name)

    resp = Response({}, status=200, mimetype='application/json')
    return resp

if __name__ == "__main__":
    app.run(debug=True)
```

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

```shell
curl -X POST https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks/{TaskSid}/Reservations/{ReservationSid} 
-d ReservationStatus=accepted
-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 our Worker Alice:

```json
{... "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](https://console.twilio.com/?frameUrl=%2Fconsole%2Ftaskrouter%2Fworkspaces), 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's activity status set to Busy in Twilio TaskRouter console.](https://docs-resources.prod.twilio.com/c5c7f33b5a09ffb1f0d0a5e715b2c144ba8edfe1e09481f568425dc7c99d056a.jpg)

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 acception and rejection workflows.

[Next: Accept a Reservation using Assignment Instructions »](/docs/taskrouter/quickstart/python/reservations-accept-callback)

\*\* *If you're not using ngrok or a similar tool, you can modify run.py to print the value of ReservationSid to your web server log. Or, you can use the [Tasks REST API](/docs/taskrouter/api/task) instance resource to look up the ReservationSid based on the TaskSid.*
