Skip to Content
Developers

Paging

Reading lists a page at a time, and keeping a copy in step with updatedSince.

Every list in the API pages the same way, with a cursor.

Asking for a page

ParameterWhat it does
limitHow many records per page. Default 25, at most 100
cursorWhere to continue. Pass the nextCursor of the page before
updatedSinceOnly records changed at or after this time (ISO 8601, with a time zone)

The answer

{
  "data": [{ "id": "…" }, { "id": "…" }],
  "hasMore": true,
  "nextCursor": "djE6MDE5MzBhYjQtN2M0ZC03MmUxLWE3ZmMtNGYyZTkwYjA1ZDNi"
}

Records come newest first. When hasMore is false, nextCursor is null and you have them all. New records arriving while you page do not shift your place, because the cursor marks a record, not a position.

Treat the cursor as opaque: pass it back exactly as you got it. A cursor TRAKI did not give out is refused with validation_error.

Reading every page

async function readAll(url) {
  const all = [];
  let cursor = null;
  do {
    const page = new URL(url);
    page.searchParams.set('limit', '100');
    if (cursor) page.searchParams.set('cursor', cursor);
    const res = await fetch(page, {
      headers: { Authorization: `Bearer ${process.env.TRAKI_KEY}` },
    });
    if (!res.ok) throw new Error(`${res.status} ${(await res.json()).code}`);
    const body = await res.json();
    all.push(...body.data);
    cursor = body.hasMore ? body.nextCursor : null;
  } while (cursor);
  return all;
}

Keeping a copy in step with updatedSince

To keep your own copy of a list, read it in full once. After that, ask only for what changed: save the time you start each run, and pass the time of the last run as updatedSince. Records that changed in between come back, and you update them in your copy. Running the same sync twice does no harm.