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"// Every page, fetched as the loop needs it.
for await (const record of hixel.usage.list({ customer: "org_42", limit: 100 })) {
console.log(record.request_id);
}
// Or one page at a time, resuming from a cursor you kept.
const page = await hixel.usage.list({ customer: "org_42", limit: 100, cursor: "eyJ0Ijoi…" });
console.log(page.data.length, page.next_cursor);limitis 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
400invalid_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#
| List | Order |
|---|---|
GET /v1/keys | Newest first |
GET /v1/customers | Oldest first |
GET /v1/customers/{external_id}/provider_keys | Oldest first |
GET /v1/usage | Newest first |
GET /v1/customers/{external_id}/ledger | Newest first |
GET /v1/wallets | By balance (sort) |
GET /v1/events | Newest first |
GET /v1/webhook_endpoints | Oldest first |
GET /v1/webhook_endpoints/{id}/deliveries | Newest first |
GET /v1/trace_export_destinations | Oldest first |
GET /v1/top_ups, GET /v1/customers/{external_id}/top_ups | Newest first |
GET /v1/auto_refills, GET /v1/customers/{external_id}/auto_refills | Newest 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.