Query objects with the server API, SDK, CLI and UI
Automating operations with CVAT is often related to requesting and processing object collections, such as tasks, jobs, or organization members. CVAT provides a number of options to query collections and filter the results via the server API. This guide describes how to query objects in the server API, the Python SDK, the CLI and the web interface.
How querying works
List endpoints, such as GET /api/tasks, GET /api/jobs and GET /api/projects, accept query
parameters for filtering, pagination, and ordering:
| Parameter | Description |
|---|---|
A field name, such as project_id or name |
A simple equality check on one field. |
filter |
A JSON Logic expression for complex conditions. |
search |
A search term, matched against the fields the endpoint lists as searchable. |
sort |
The field used to order the results. |
org, org_id, X-Organization |
The organization to return objects from: its slug in the org query parameter or the X-Organization header, or its ID in org_id. An empty value selects the personal workspace. If omitted, objects from all the organizations available to the user are returned. |
page |
The page number, starting from 1. |
page_size |
The number of objects per page. The default is 10, values above 500 are reduced to 500. |
A simple parameter can be useful for a simple equality check, for example
GET /api/tasks?project_id=2. In more complex cases use the filter parameter, which supports
several conditions, lists of values, and different comparisons. All these parameters can be
combined.
The responses are paginated. The maximum allowed page size is 500 items. Collecting all the items
requires iteration over all the pages. It can be done by estimating the number of pages from the
count field in the response, or by following the next URL in the response.
JSON Logic filters
The filter parameter is a JSON Logic expression.
A field is referenced with {"var": "<field name>"} and compared with an operator.
Conditions are combined with and and or.
For example, this filter matches completed tasks that belong to project 1:
{"and": [{"==": [{"var": "status"}, "completed"]}, {"==": [{"var": "project_id"}, 1]}]}
Supported filter fields
Each endpoint accepts its own list of fields.
The list is provided in the description of the filter parameter in the API reference,
and it can change between versions. The table below lists the fields of some endpoints.
| Endpoint | Filter fields |
|---|---|
| Projects | assignee, id, name, owner, status, updated_date |
| Tasks | assignee, created_date, dimension, id, media_type, mode, name, owner, project_id, project_name, status, subset, tracker_link, updated_date, validation_mode |
| Jobs | assignee, dimension, id, media_type, mode, parent_job_id, project_id, project_name, stage, state, task_id, task_name, type, updated_date |
| Cloud storages | credentials_type, description, id, name, owner, provider_type, resource |
A field that is not in the list for an endpoint cannot be used in its filter.
Query with the server API
The filter expression is sent in the filter query parameter.
The expression must be URL encoded. The --data-urlencode option of curl performs the encoding.
Requests are authenticated with a personal access token, as described on the Access tokens page.
curl --get "https://app.cvat.ai/api/tasks" \
--data-urlencode 'filter={"and":[{"==":[{"var":"status"},"completed"]},{"==":[{"var":"project_id"},1]}]}' \
--data-urlencode 'page_size=100' \
-H "Accept: application/vnd.cvat+json" \
-H "Authorization: Bearer $CVAT_ACCESS_TOKEN"
The CVAT_ACCESS_TOKEN variable must contain the token value.
Filter examples
Tasks owned by a specific user:
{"and": [{"==": [{"var": "owner"}, "<username>"]}]}
Tasks from projects 1, 2 and 3:
{"and": [{"in": [{"var": "project_id"}, [1, 2, 3]]}]}
Completed tasks from project 1:
{"and": [{"==": [{"var": "status"}, "completed"]}, {"==": [{"var": "project_id"}, 1]}]}
Tasks with “demo” in the name:
{"in": ["demo", {"var": "name"}]}
Tasks that have no assignee:
{"!": {"var": "assignee"}}
Jobs in the validation stage of task 12 (GET /api/jobs):
{"and": [{"==": [{"var": "stage"}, "validation"]}, {"==": [{"var": "task_id"}, 12]}]}
Common mistakes
Passing a plain key and value. A filter such as {"project_id": 2} is not a JSON Logic
expression. The server reads project_id as the name of an operation and returns 400 Bad Request
with a message like this:
filter: operation 'project_id' with arguments '2' is not supported
The correct forms are GET /api/tasks?project_id=2 or the JSON Logic condition
{"==": [{"var": "project_id"}, 2]}.
Missing URL encoding. Braces, quotes and colons in the expression must be encoded in the query
string. The encoding is best left to the HTTP client, as curl --get --data-urlencode does above.
When the URL is built manually in Python, the value can be encoded with
urllib.parse.quote_plus.
The SDK encodes the filter automatically.
Using a field the endpoint does not list. The supported fields are listed in the table above
and in the filter description of each endpoint.
Requesting related objects one by one. When many related objects are needed, for example the projects of a set of tasks, requesting the project of each task separately produces one request per task. It is typically more efficient to request the tasks first, collect the unique project IDs, and then request the projects by pages with a single filter:
{"in": [{"var": "id"}, [1, 2, 3]]}
If the list of IDs is long, it can be split into several requests to keep the request URL short. When the IDs are close to each other, it can be better to request a range of values instead of listing each ID. The range can include objects that are not needed, which can be skipped on the client side:
{"<=": [10, {"var": "id"}, 20]}
Query with the Python SDK
The SDK builds filters from Python expressions, so JSON Logic does not need to be written by hand.
The F object is available in SDK releases from 2.70.0. Conditions are combined with & (and), |
(or) and ~ (not). The list() method requests all the pages and returns the objects from all of
them. Other keyword arguments, such as sort or page_size, are passed to the endpoint as query
parameters.
from cvat_sdk import make_client
from cvat_sdk.core.filters import F
with make_client("https://app.cvat.ai", access_token="<access token>") as client:
for task in client.tasks.list(filter=(F.status == "completed") & F.project_id.one_of([1, 2, 3])):
print(task.id, task.name)
The full list of operators and keyword lookups is available in High level API.
The related objects advice above looks like this in the SDK:
tasks = client.tasks.list(filter=F.status == "completed", page_size=500)
project_ids = sorted({task.project_id for task in tasks if task.project_id is not None})
projects = client.projects.list(filter=F.id.one_of(project_ids), page_size=500)
A range of IDs is requested with between():
projects = client.projects.list(filter=F.id.between(project_ids[0], project_ids[-1]), page_size=500)
The SDK turns each expression into a JSON Logic filter and sends it in the filter parameter.
The table below shows the result, which helps to move a filter between the SDK and the server API.
| SDK expression | JSON Logic sent to the server |
|---|---|
F.owner == "alice" |
{"==": [{"var": "owner"}, "alice"]} |
F.id >= 10 |
{">=": [{"var": "id"}, 10]} |
F.project_id.one_of([1, 2, 3]) |
{"in": [{"var": "project_id"}, [1, 2, 3]]} |
F.name.contains("demo") |
{"in": ["demo", {"var": "name"}]} |
F.id.between(10, 20) |
{"<=": [10, {"var": "id"}, 20]} |
~F.assignee.is_set() |
{"!": {"var": "assignee"}} |
F.status != "annotation" |
{"!": {"==": [{"var": "status"}, "annotation"]}} |
(F.status == "completed") & F.project_id.one_of([1, 2, 3]) |
{"and": [{"==": [{"var": "status"}, "completed"]}, {"in": [{"var": "project_id"}, [1, 2, 3]]}]} |
Low level API
The low level API accepts the JSON Logic string as is and encodes it automatically. It returns one page per call.
(page, response) = client.tasks.api.list(
filter='{"in": [{"var": "project_id"}, [1, 2, 3]]}'
)
print(page.count)
The cvat_sdk.core.helpers.get_paginated_collection() function collects the objects from all the
pages of a low level API list endpoint. It fetches the collection based on the query parameters
specified, and returns either a list of Python SDK objects or, with return_json=True, a list of
plain Python structures parsed from JSON.
from cvat_sdk.core.helpers import get_paginated_collection
tasks = get_paginated_collection(
client.tasks.api.list_endpoint,
filter='{"in": [{"var": "project_id"}, [1, 2, 3]]}',
page_size=500,
)
More details are available in the Paginated responses section of the low level API guide.
Query with the CLI
The CLI lists projects and tasks with the project ls and
task ls commands. The commands return all the objects available to the user and have no filtering
options. The --json option prints the full objects in the JSON format instead of only their IDs:
cvat-cli task ls --json
The output can be filtered on the client side, for example, with jq. This command prints the IDs and names of the completed tasks from project 1:
cvat-cli task ls --json | jq '.[] | select(.status == "completed" and .project_id == 1) | {id, name}'
The CLI requests all the objects from the server, which can take a long time for large collections. Filtering on the server side requires the server API or the SDK.
Build a filter in the web interface
The Projects, Tasks, Jobs and Cloud storages pages have sort and filter controls. The pages use the same API as other clients, so a filter built on a page is a quick way to find the right expression.
-
Open the page, for example Tasks.
-
Open the filter window, add the rules and apply them.
-
Check the page address, which contains the filter. For example, a rule that matches task names containing “balcony” produces an address like this:
https://app.cvat.ai/tasks?filter=%7B%22and%22%3A%5B%7B%22in%22%3A%5B%22balcony%22%2C%7B%22var%22%3A%22name%22%7D%5D%7D%5D%7D&page=1&pageSize=10 -
Copy only the value of
filterand decode it. The example above decodes to this JSON Logic expression:{"and": [{"in": ["balcony", {"var": "name"}]}]}This expression can be used as the
filterparameter in an API request. ThepageandpageSizeparameters only control the web page and are not needed. The API parameter for page length ispage_size.
Choosing a query method
| Task | Method |
|---|---|
| Check one field for one value | A simple parameter, such as ?project_id=2 |
| Run a one-off query from a terminal | The server API with filter |
| Query objects inside a Python application | The SDK with F |
| List all projects or tasks from a terminal | The CLI with project ls or task ls |
| Find the right filter expression by example | The web interface, then copy the filter |
| Get many related objects, such as the projects of a set of tasks | One filtered request by IDs instead of a request per object |