Indexes, videos, and tasks

When listing your indexes, videos, or video indexing tasks, the platform supports the following parameters that allow you to control sorting behavior:

  • Sorting criteria: Use the sort_by parameter to specify the field you want to sort on. The following options are available:
    • updated_at: Sorts by the date and time when the item was updated.
    • created_at: Sorts by the date and time when the item was created.
  • Sorting direction: Use the sort_option parameter to specify the sorting direction. The following options are available:
    • asc: For ascending order.
    • desc: For descending order. This is the default value.

The examples in this section are specific to using the /tasks endpoint. However, the principles demonstrated are similar when using the /indexes and /indexes/videos endpoints.

You can interact with the platform using one of the available SDKs or an HTTP client like requests or axios. This guide demonstrates how to use the SDKs, the recommended approach for most scenarios. If you need to make direct HTTP requests, refer to the corresponding pages in API Reference section for details.

Prerequisites

Examples

Sort by creation date

To sort items by their creation date, invoke the list method on the task object, specifying "created_at" for the sort_by parameter.

from twelvelabs import TwelveLabs

client = TwelveLabs(api_key="<YOUR_API_KEY>")

tasks = client.task.list(sort_by="created_at")
for task in tasks:
    print(f"id={task.id} status={task.status} created_at={task.created_at}")
import { TwelveLabs } from 'twelvelabs-js';

const client = new TwelveLabs({ apiKey: '<YOUR_API_KEY>'});

const tasks = await client.task.list({ indexId: indexId, sortBy: 'created_at' });
tasks.forEach((task) => {
  console.log(`id=${task.id} status=${task.status} created_at=${task.createdAt}`);
});

The following output was truncated for brevity:

id=65e86608bb29f13bdd6f3c2e status=pending created_at=2024-03-06T12:48:08.685Z
id=65e865b7bb29f13bdd6f3c2b status=pending created_at=2024-03-06T12:46:47.75Z
id=65e86582bb29f13bdd6f3c28 status=pending created_at=2024-03-06T12:45:54.256Z

Specify the sort order

The example code below demonstrates how to sort items by their creation date in ascending order. Specify "created_at" for the sort_by parameter and "asc" for the sort_option parameter to reverse the default sorting order:

from twelvelabs import TwelveLabs

client = TwelveLabs(api_key="<YOUR_API_KEY>")

tasks = client.task.list(
    sort_by="created_at",
    sort_option="asc"
    )
for task in tasks:
    print(f"id={task.id} status={task.status} created_at={task.created_at}")
import { TwelveLabs } from 'twelvelabs-js';

const client = new TwelveLabs({ apiKey: '<YOUR_API_KEY>'});

const tasks = await client.task.list({
  indexId: indexId,
  sortBy: 'created_at',
  sortOption: 'asc',
});
tasks.forEach((task) => {
  console.log(`id=${task.id} status=${task.status} created_at=${task.createdAt}`);
});

The following output was truncated for brevity:

id=65b9f6b79e82682070384431 status=ready created_at=2024-01-31T07:28:55.406Z
id=65b9f6b89e82682070384432 status=ready created_at=2024-01-31T07:28:56.474Z
id=65b9f6b89e82682070384433 status=ready created_at=2024-01-31T07:28:56.522Z

Combine sorting and pagination

The following example code sorts items by their creation date and specifies that each page should contain 5 items:

from twelvelabs import TwelveLabs

client = TwelveLabs(api_key="<YOUR_API_KEY>")

tasks = client.task.list_pagination(
  sort_by="created_at",
  page_limit=5
)

# Utility function to print a specific page
def print_page(page):
  for task in page:
    print(f"id={task.id} status={task.status} created_at={task.created_at}")

print_page(tasks.data)

while True:
    try:
        print_page(next(tasks))
    except StopIteration:
        break
import { TwelveLab, Task } from 'twelvelabs-js';

const client = new TwelveLabs({ apiKey: '<YOUR_API_KEY>'});

const tasks = await client.task.listPagination({ sortBy: 'created_at', pageLimit: 5 });
tasks.data.forEach((task: Task) => {
  console.log(`id=${task.id} status=${task.status}`);
});
while (true) {
  const nextPageData = await tasks.next();
  if (!nextPageData) {
    break;
  }
  nextPageData.forEach((task: Task) => {
    console.log(`id=${task.id} status=${task.status} created_at=${task.createdAt}`);
  });
}

The following output was truncated for brevity:

id=65e86608bb29f13bdd6f3c2e status=pending created_at=2024-03-06T12:48:08.685Z
id=65e865b7bb29f13bdd6f3c2b status=pending created_at=2024-03-06T12:46:47.75Z
id=65e86582bb29f13bdd6f3c28 status=pending created_at=2024-03-06T12:45:54.256Z