GlexirDocs
Dashboard

SDK

Pagination

List methods return results one page at a time. The SDK can fetch the next pages for you as you loop, or you can fetch pages yourself.

TypeScript
import Glexir from "@glexir/sdk";

const glexir = new Glexir({ apiKey: process.env.GLEXIR_API_KEY });

for await (const file of glexir.files.list({ purpose: "batch", limit: 50 })) {
  console.log(file.id, file.filename);
}

The loop requests the next page only when it reaches the end of the current one. If you break out early, no more pages are fetched.

List methods

MethodQuery parametersItems
glexir.files.list(query?)after, limit, order, purposeFileObject
glexir.batches.list(query?)after, limitBatch
glexir.chat.completions.list(query?)after, limit, metadata, model, orderChatCompletion
glexir.chat.completions.messages.list(completionId, query?)after, limit, orderChatCompletionStoreMessage
glexir.responses.inputItems.list(responseId, query?)after, limit, order, includeResponseItem
glexir.conversations.items.list(conversationId, query?)after, limit, order, includeConversationItem
glexir.models.list()noneModel

Each method also takes request options as its last argument. They apply to every page the SDK fetches, including signal, headers, timeout and maxRetries.

glexir.models.list() always returns a single page with every model. It has no cursor, so for await stops after that page.

Page size and order

  • Files and batches are listed by the gateway from Glexir's own records. limit defaults to 20 and is clamped to 1 to 100. Results are newest first. Files also accept order: "asc".
  • Stored chat completions default to order: "asc". metadata filters by key/value pairs, for example { metadata: { tenant: "acme" } }.
  • Conversation items default to order: "desc" (newest first).
  • Other lists pass limit and order through to the upstream API, which sets their defaults.

Page by page

Await a list call to get the first page as a CursorPage:

TypeScript
import Glexir from "@glexir/sdk";

const glexir = new Glexir({ apiKey: process.env.GLEXIR_API_KEY });

let page = await glexir.batches.list({ limit: 20 });
console.log(page.data.map((batch) => batch.status));

while (page.hasNextPage()) {
  page = await page.getNextPage();
  console.log(page.data.length);
}

To loop over pages instead of items, use iterPages():

TypeScript
import Glexir from "@glexir/sdk";

const glexir = new Glexir({ apiKey: process.env.GLEXIR_API_KEY });

const first = await glexir.chat.completions.list({ limit: 100 });
for await (const page of first.iterPages()) {
  console.log(`${page.data.length} completions, more: ${page.has_more}`);
}

Cursors

Pages are linked by an after cursor, which is an object ID. To resume a listing later, for example in a paginated UI or a job that runs in batches, save the cursor and pass it as after:

TypeScript
import Glexir from "@glexir/sdk";

const glexir = new Glexir({ apiKey: process.env.GLEXIR_API_KEY });

const page = await glexir.files.list({ limit: 10 });
const cursor = page.data.at(-1)?.id; // save this

if (page.has_more && cursor) {
  const next = await glexir.files.list({ limit: 10, after: cursor });
  console.log(next.data.map((file) => file.id));
}

The SDK picks the next cursor like this:

ListNext after
conversations.items.listThe page's last_id
All other listsThe id of the last item on the page

Keep the other query parameters the same when you pass a cursor. getNextPage() does this for you: it repeats the original query with the new after.

Reference

CursorPage

One page of results. It is also an async iterable over the items of this page and all later pages.

dataItem[]optional
The items on this page.
has_morebooleanoptional
Whether the API reported more results. false if the response did not include it.
first_idstring | null | undefinedoptional
The first item's ID, when the API returns it.
last_idstring | null | undefinedoptional
The last item's ID, when the API returns it.
hasNextPage()booleanoptional
true when has_more is true, the page is not empty, and a next cursor exists. Always false for models.list().
getNextPage()Promise<CursorPage<Item>>optional
Fetches the next page. Throws an Error if there is none, so check hasNextPage() first.
iterPages()AsyncGenerator<CursorPage<Item>>optional
Yields this page, then each following page.
[Symbol.asyncIterator]()AsyncGenerator<Item>optional
Yields every item on this page and the pages after it. This is what for await (const item of page) uses.

PagePromise

What a list method returns. It is an APIPromise for the first CursorPage<Item>, so you can await it or call withRawResponse() and asResponse(). It is also an async iterable over all items, which is why for await (const x of glexir.files.list()) works without an await first.

CursorPageParams

TypeScript
interface CursorPageParams {
  after?: string; // return items after this object ID
  limit?: number; // page size
}

The type is exported from @glexir/sdk. Each list method has its own params type with these two fields plus the extra filters in the table above: FileListParams, BatchListParams, ChatCompletionListParams, MessageListParams, InputItemListParams and ItemListParams.