Indexes, videos, and tasks

The endpoints that return lists of items support pagination. You can use the following parameters to control pagination behavior:

  • page: Page number to retrieve. Defaults to 1.
  • page_limit: Maximum items per page. Default to 10. The maximum value is 50.

The examples on this page are specific to listing your indexes. The same principles apply to other similar endpoints.

Prerequisites

Examples

The SDKs provide two distinct methods for listing items, each suited to different use cases and requirements. Selecting the most efficient method is important for optimizing the performance of your application:

  • Direct pagination: Choose this method for retrieving a specific page, mainly when the total number of items is manageable, or you must fetch a single page of results.
  • Iterative pagination: Choose this method for iterative navigation through lists, mainly when your application must retrieve a large number of items.

Direct pagination

Retrieve the first page of results

The following example code retrieves the first page of results using the default value for the page and page_limit parameters by invoking the list method of the index object with no parameters:

from twelvelabs import TwelveLabs

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

indexes = client.index.list()
for index in indexes:
    print(
        f"id={index.id} name={index.name} models={index.models} created_at={index.created_at}"
    )
import { TwelveLabs } from 'twelvelabs-js';

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

const indexes = await client.index.list();
indexes.forEach((index) => {
  console.log(
    `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
  );
});

The following output was truncated for brevity:

id=65e5ac69bb29f13bdd6f37c4 name=2024-03-04T11:11:36.583Z models=[Model(name='pegasus1', options=['visual', 'audio'], addons=None)] created_at=2024-03-04T11:11:37.393Z
id=65e59606bb29f13bdd6f3794 name=2024-03-04T09:36:06.079Z models=[Model(name='pegasus1', options=['visual', 'audio'], addons=None)] created_at=2024-03-04T09:36:06.791Z
id=65e5945abb29f13bdd6f378c name=2024-03-04T09:28:57.739Z models=[Model(name='pegasus1', options=['visual', 'audio'], addons=None)] created_at=2024-03-04T09:28:58.473Z

Retrieve a specific page

The following example code uses the page parameter to retrieve a specific page (2):

from twelvelabs import TwelveLabs

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

indexes = client.index.list(page=2)
for index in indexes:
    print(
        f"id={index.id} name={index.name} models={index.models} created_at={index.created_at}"
    )
import { TwelveLabs } from 'twelvelabs-js';

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

const indexes = await client.index.list({ page: 2 });
indexes.forEach((index) => {
  console.log(
    `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
  );
});

If the number of items on a page is less than the maximum number of items the platform should return on that page, you've reached the end of the dataset.

Limits

The following example code uses the page_limit parameter to specify a limit of 2 items on each page:

from twelvelabs import TwelveLabs

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

indexes = client.index.list(page_limit=2)
for index in indexes:
    print(
        f"id={index.id} name={index.name} models={index.models} created_at={index.created_at}"
    )
import { TwelveLabs } from 'twelvelabs-js';

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

const indexes = await client.index.list({ pageLimit: 2 });
indexes.forEach((index) => {
  console.log(
    `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
  );
});

Iterative pagination

To retrieve the first page of results, invoke the list_pagination method of the index object. To retrieve subsequent pages of results, use the iterator protocol in Python or the async iterator protocol in JavaScript.

Retrieve the first page of results

The following example code retrieves the first page of results using the default value for the page and page_limit parameters by invoking the list_pagination method of the index object with no parameters:

from twelvelabs import TwelveLabs

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

indexes = client.index.list_pagination()

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

print_page(indexes.data)
import { TwelveLabs, Index } from 'twelvelabs-js';

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

const indexes = await client.index.listPagination();
indexes.data.forEach((index: Index) => {
  console.log(
    `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
  );

The following example output was truncated for brevity:

id=65e71802bb29f13bdd6f38d8 name=2024-03-05T13:02:57.938Z models=[Model(name='pegasus1', options=['visual', 'audio'], addons=None), Model(name='marengo2.7', options=['visual', 'audio'], addons=None)] created_at=2024-03-05T13:02:58.531Z
id=65e5ac69bb29f13bdd6f37c4 name=2024-03-04T11:11:36.583Z models=[Model(name='pegasus1', options=['visual', 'audio'], addons=None)] created_at=2024-03-04T11:11:37.393Z
id=65e59606bb29f13bdd6f3794 name=2024-03-04T09:36:06.079Z models=[Model(name='pegasus1', options=['visual', 'audio'], addons=None)] created_at=2024-03-04T09:36:06.791Z

Iterate over the results

The following example code iterates over results using the default value for the page and page_limit parameters:

from twelvelabs import TwelveLabs

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

indexes = client.index.list_pagination()

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

print_page(indexes.data)

while True:
    try:
        print_page(next(indexes))
    except StopIteration:
        break
  import { TwelveLabs, Index } from 'twelvelabs-js';

  const client = new TwelveLabs({ apiKey: '<YOUR_API_KEY>'});
  
  const indexes = await client.index.listPagination();
  indexes.data.forEach((index: Index) => {
    console.log(
      `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
    );
  });
  while (true) {
    const nextPageData = await indexes.next();
    if (!nextPageData) {
      break;
    }
    nextPageData.forEach((index: Index) => {
      console.log(
        `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
      );
    });
  }

Limits

The following example code iterates over results and specifies a limit of 5 items on each page:

from twelvelabs import TwelveLabs

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

indexes = client.index.list_pagination(page_limit=5)

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

print_page(indexes.data)

while True:
    try:
        print_page(next(indexes))
    except StopIteration:
        break
import { TwelveLabs, Index } from 'twelvelabs-js';

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

const indexes = await client.index.listPagination({pageLimit:5});
indexes.data.forEach((index: Index) => {
  console.log(
    `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
  );
});
while (true) {
  const nextPageData = await indexes.next();
  if (!nextPageData) {
    break;
  }
  nextPageData.forEach((index: Index) => {
    console.log(
      `id=${index.id} name=${index.name} models=${JSON.stringify(index.models)} created_at=${index.createdAt}`,
    );
  });
}