Documentation menu

Pagination

Walk through long lists with a cursor.

Every list is a page with an opaque cursor, the same shape everywhere:

{
  "data": [ … ],
  "next_cursor": "eyJ0IjoiMjAyNi0xMS0yNVQxMDowMDowMC4wMDBaIiwiaWQiOiJ1c2VfLi4uIn0"
}

Pass next_cursor back as cursor, with the same filters, for the next page. next_cursor is null on the last page.

curl "https://api.hixel.dev/v1/usage?customer=org_42&limit=100&cursor=eyJ0Ijoi…" \
  -H "Authorization: Bearer $HIXEL_API_KEY"
  • limit is 1 to 100; the default is 50.
  • A cursor is opaque: do not build or change one. A cursor that no page of this list gave is refused with 400 invalid_cursor.
  • Each list has a fixed order (below), which does not change between pages.

In code#

async function* listAll(path: string, key: string) {
  let cursor: string | null = null;
  do {
    const url = new URL(`https://api.hixel.dev${path}`);
    url.searchParams.set("limit", "100");
    if (cursor) url.searchParams.set("cursor", cursor);
    const response = await fetch(url, { headers: { Authorization: `Bearer ${key}` } });
    if (!response.ok) throw new Error(`${response.status}: ${await response.text()}`);
    const page = await response.json();
    yield* page.data;
    cursor = page.next_cursor;
  } while (cursor);
}

for await (const record of listAll("/v1/usage?customer=org_42", process.env.HIXEL_API_KEY!)) {
  console.log(record.request_id, record.billed_micros);
}

Paginated lists#

ListOrder
GET /v1/keysNewest first
GET /v1/customersOldest first
GET /v1/customers/{external_id}/provider_keysOldest first
GET /v1/usageNewest first
GET /v1/customers/{external_id}/ledgerNewest first
GET /v1/walletsBy balance (sort)
GET /v1/eventsNewest first
GET /v1/webhook_endpointsOldest first
GET /v1/webhook_endpoints/{id}/deliveriesNewest first
GET /v1/trace_export_destinationsOldest first
GET /v1/top_ups, GET /v1/customers/{external_id}/top_upsNewest first
GET /v1/auto_refills, GET /v1/customers/{external_id}/auto_refillsNewest first

A wallet's balance can change between two pages of GET /v1/wallets, so a wallet near a page boundary may be listed twice or skipped.