# Tasks

> Documentation index: https://ridewithgps.com/api/v1/doc/llms.txt

Route and trip uploads, and experience publishing, run in the background. They return a task: poll its `url` until `status` is `completed`, then read its `items` and `errors`.

OpenAPI specification for these endpoints: https://ridewithgps.com/api/v1/openapi/openapi_tasks.yaml

## Returns the specified task

`GET /api/v1/tasks/{id}.json`

Returns the current state of an asynchronous task owned by the authenticated user.

Uploads are processed in the background. The upload endpoints return a task, and its
`id` is polled here until `status` becomes `completed`.

A completed task reports its outcome through two arrays:

- `items` lists the assets that were created, each with an `item_type`, `item_id` and `item_url`
- `errors` lists anything that went wrong, each with a machine readable `code`

Both arrays are always present. An upload that succeeded entirely has an empty `errors`
array, and one that created nothing has an empty `items` array. When an archive is
uploaded, entries are processed independently, so a task can report both created assets
and errors for the entries that failed.

### Headers

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `x-rwgps-api-key` | string | yes | API key for identifying the client application |
| `x-rwgps-auth-token` | string | yes | Authentication token for the user |

### Parameters

| Name | Type | In | Required | Description |
| --- | --- | --- | --- | --- |
| `id` | integer | path | yes | Task ID |

### Response `200`

| Name | Type | Description |
| --- | --- | --- |
| `task` | object |  |
| `task.id` | integer |  |
| `task.user_id` | integer |  |
| `task.url` | string | API url of the task, to poll until it completes |
| `task.type` | string | What kind of work the task performs. `gps_file_upload` for uploads, `experience_package` for experience packages |
| `task.status` | string | `pending` until the task has run, then `completed` |
| `task.created_at` | date-time |  |
| `task.updated_at` | date-time |  |
| `task.items` | array of objects | Assets created by the task. Empty while pending, and empty if nothing was created. A file holding several tracks creates one asset per track |
| `task.items[].item_type` | string | Type of the created asset: `route` or `trip` for uploads, `experience_package` for experience packages |
| `task.items[].item_id` | integer |  |
| `task.items[].item_url` | string or null | API url of the created asset |
| `task.errors` | array of objects | Anything that went wrong, one entry per track that could not be imported. Empty when the task succeeded entirely |
| `task.errors[].code` | string | Machine readable reason. One of `duplicate`, `time_data_missing`, `no_tracks`, `empty_file`, `parse_failed`, `failed_to_process`, `weigh_in_failed`, `build_failed`, `error` |
| `task.errors[].message` | string | Human readable description, always in English |
| `task.errors[].item_type` | string or null | Set when the error refers to an existing asset, as for a duplicate |
| `task.errors[].item_id` | integer or null |  |
| `task.errors[].item_url` | string or null |  |

### Errors

- `401` Unauthorized
- `404` Task not found

### Example request

```bash
curl \
  -H "x-rwgps-api-key: $API_KEY" \
  -H "x-rwgps-auth-token: $AUTH_TOKEN" \
  https://ridewithgps.com/api/v1/tasks/123.json
```

### Example response

```json
{
  "task": {
    "id": 123,
    "user_id": 123,
    "url": "https://ridewithgps.com/api/v1/tasks/123.json",
    "type": "gps_file_upload",
    "status": "completed",
    "created_at": "2000-01-01T12:00:00Z",
    "updated_at": "2000-01-01T12:00:00Z",
    "items": [
      {
        "item_type": "route",
        "item_id": 123,
        "item_url": "https://ridewithgps.com/api/v1/routes/123.json"
      }
    ],
    "errors": [
      {
        "code": "duplicate",
        "message": "This route already exists",
        "item_type": "route",
        "item_id": 123,
        "item_url": "https://ridewithgps.com/api/v1/routes/123.json"
      }
    ]
  }
}
```
