Iterating results

It might happen, that you have rather a big number of items in a single batch or in all the batches. If the number of items exceeds 1000, you will not be able to get all the items due to the limits.

In this case, you will need to iterate through the items and get the results splitting them in smaller sets.

Tip

Getting the items from all the batches is similar to getting them from a single batch. In the scope of this tutorial, we will try and get the items from a single batch (specifying it by ID). You can either iterate through the batches, or remove the batch ID from the request query parameters to get the items from all the batches at once.

Step 1: Get first item

First, you need to get an item to start with. To do this, you must know the IDs of the project and batch containing the items. We also sort the returned items by their IDs and limit the request to exactly one item.

cURL
Toloka-Kit
curl -X GET 'https://toloka.dev/api/app/v0/app-projects/8lvN9kBN4wwsj47ZqNal/items?batch_id=7d0YYJ2Av5OhnLVwAygZ&sort=id&limit=1' \
-H 'Authorization: ApiKey PlaceYourRealApiKey_Here'

The response prints the details of the first item which include the item ID, input and output data.

{
"content": [
{
"app_project_id": "8lvN9kBN4wwsj47ZqNal",
"batch_id": "7d0YYJ2Av5OhnLVwAygZ",
"created_at": "2022-01-22T07:44:57.035",
"errors": [],
"id": "QPVvLYdlRPBH9pl7KzBV",
"input_data": {
"id": "1",
"text": "Kate likes dogs."
},
"output_data": {
"confidence": 0.9998542274,
"result": [
"OK"
],
"text": "Kate likes dogs."
},
"status": "NEW"
}
],
"has_more": true
}

In the requests above:

ParameterDescription
app_project_idThe ID of the project we want to get the items from.
batch_idThe ID of the batch we limit our requests to.
sortThe sorting direction (id) which allows us to get the ID of the first item.
limitThe number of items in the response. For the first request we limit it to 1.

Note the has_more attribute in the response to the cURL request. It means that there are more items in this project batch, we will learn how to get them at the next step.

Step 2: Get next items

Now that we have the ID of the first item in the selected batch, we can use it to iterate through the next items using the after_id query parameter in the request. We set this parameter equal to the ID of the first item and then the IDs of the consequent items to get the next ones.

In cURL requests, we must manually iterate through the items, while in Toloka-Kit we can cycle through them until we get the last result.

cURL
Toloka-Kit
curl -X GET 'https://toloka.dev/api/app/v0/app-projects/8lvN9kBN4wwsj47ZqNal/items?batch_id=7d0YYJ2Av5OhnLVwAygZ&sort=id&limit=1&after_id=QPVvLYdlRPBH9pl7KzBV' \
-H 'Authorization: ApiKey PlaceYourRealApiKey_Here'

The response prints the details of the second item which include the item ID, input and output data.

{
"content": [
{
"app_project_id": "8lvN9kBN4wwsj47ZqNal",
"batch_id": "7d0YYJ2Av5OhnLVwAygZ",
"created_at": "2022-01-22T07:44:57.035",
"errors": [],
"id": "Vnj0LMw4Rn5Hkev3Y4aD",
"input_data": {
"id": "4",
"text": "Pete likes cats."
},
"output_data": {
"confidence": 0.9993645425,
"result": [
"OK"
],
"text": "Pete likes cats."
},
"status": "NEW"
}
],
"has_more": true
}

In the requests above:

ParameterDescription
app_project_idThe ID of the project we want to get the items from.
batch_idThe ID of the batch we limit our requests to.
sortThe sorting direction (id) which allows us to get the item IDs in the descending alphanumerical order.
limitThe number of items in the response. For the second and subsequent requests we limit it to 1001000.
after_idThe ID of the item after which we start the next set of items.
Toloka-Kit complete code

See also

Contact support

Last updated: July 4, 2023