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

> \[!NOTE]
>
> If you'd like to view the events happening in your Workspace at an Event Callback URL, please obtain a [free endpoint URL](https://requestbin.com/r) then set the Event Callback URL in your Workspace to point there.

![Twilio console showing event callback URL and selected callback events like Task Created and Task Completed.](https://docs-resources.prod.twilio.com/b808a9d74dec3e1eab9eade106c12b94844e2ef46a35f65d5a3df6219c136e2a.jpg)

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](https://console.twilio.com/?frameUrl=%2Fconsole%2Ftaskrouter%2Fworkspaces), click 'Workers' then click to edit Alice and set her Activity to 'Offline'. Your Workers should look like this:

![Twilio console showing Alice offline for 20 seconds, Bob idle for 2 weeks 3 days.](https://docs-resources.prod.twilio.com/fecf462eda2f80f8081a23f295fde6046c7d043990c8d5a88ba28620b87bd58b.jpg)

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

Download and extract [Twilio's Python SDK](https://github.com/twilio/twilio-python) into your working directory. This is required as we're utilizing the SDK to create a Task and will continue to use the library in subsequent steps. See the [twilio-python documentation](https://github.com/twilio/twilio-python).

## 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(username=account_sid, password=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(sid=workspace_sid).tasks.create(
        workflow_sid=workflow_sid,
        attributes='{"selected_language":"es"}'
    )
    print(task.attributes)
    resp = Response({}, status=200, mimetype='application/json')
    return resp

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

Next, reset your Workflow's Assignment Callback URL as shown below to point to your new, running Flask server's path.

![Workflow settings for Incoming Customer Care Requests with assignment callback URL.](https://docs-resources.prod.twilio.com/154c3e62d7467fe947ef8d7658511aa06833ea3852d9d6ffe63cb5a0159a23ee.png)

To generate a Task, visit the /create\_task route we have just defined.

Alternatively, we can also create a Task using the command line utility curl, which should exist on any Mac or Linux workstation.
Execute the following command at your terminal, making sure to replace the \{} with your ngrok forwarding URL:

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

*If you don't have curl, you can run this request using an HTTP test tool 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 assignment status is pending for Customer Care Requests in Spanish.](https://docs-resources.prod.twilio.com/224ecfb8d420f5fae7d93f038b0b25c500b8362870d57bd388ea2d7f8919a455.jpg)

## Make an Eligible Worker Available

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 an eligible Worker online.

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

![Twilio console showing Alice set to Idle in TaskRouter worker settings.](https://docs-resources.prod.twilio.com/ab8fefc3590b6dec67345d739cb7940542c316ec79bd98270b420d5bf0cb2bd3.jpg)

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 showing POST and GET requests to /assignment\_callback with 200 OK status.](https://docs-resources.prod.twilio.com/6eecbbdb33e891e9bfb31a5edd222e77516c9d72e41bda6826e0b9af0fda63d7.jpg)

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 »](/docs/taskrouter/quickstart/python/reservations-accept-rest)
