Pagination
Endpoints that return long lists answer one page at a time.
- Ask for a page size with
limit(the credit history takes up to 200; the default is 50). - The answer’s
meta.next_cursoris an opaque string. Send it back ascursorwith the same filters to get the next page. - When
meta.next_cursorisnull, you have everything. - Don’t build or change cursors yourself, and don’t store them for later: they describe a position in one listing.
GET /v1/ledger (credit history) pages this way today:
"""Read the credit balance, then page through the credit history with the cursor."""
import jsonimport osimport urllib.parseimport urllib.request
BASE = os.environ.get("ZEMO_API_BASE", "https://api.zemodata.com")HEADERS = {"Authorization": f"Bearer {os.environ['ZEMO_API_KEY']}"} # needs the ledger_read scope
def get(path: str, **params: str) -> dict: query = urllib.parse.urlencode({key: value for key, value in params.items() if value}) request = urllib.request.Request(f"{BASE}{path}?{query}", headers=HEADERS) with urllib.request.urlopen(request, timeout=30) as response: return json.load(response)
balance = get("/v1/credits")["data"]print(f"balance {balance['balance']}, reserved {balance['held']}, available {balance['available']}")for lot in balance["lots"]: print(f" {lot['remaining']} {lot['kind']} credits, expire {lot['expires_at'] or 'never'}")
# Newest first. Keep asking with meta.next_cursor until it is null.cursor, entries = "", 0while True: page = get("/v1/ledger", limit="50", cursor=cursor) for entry in page["data"]: entries += 1 print( f" {entry['created_at']} {entry['kind']:<10} {entry['delta']:>+6} -> {entry['balance_after']}" ) cursor = page["meta"]["next_cursor"] if not cursor: breakprint(entries, "history entries")// Read the credit balance, then page through the credit history with the cursor.
const BASE = process.env.ZEMO_API_BASE ?? "https://api.zemodata.com";const headers = { Authorization: `Bearer ${process.env.ZEMO_API_KEY}` }; // needs the ledger_read scope
async function get(path, params = {}) { const query = new URLSearchParams(Object.entries(params).filter(([, value]) => value)); const response = await fetch(`${BASE}${path}?${query}`, { headers }); if (!response.ok) throw new Error(`${response.status}: ${await response.text()}`); return response.json();}
const { data: balance } = await get("/v1/credits");console.log(`balance ${balance.balance}, reserved ${balance.held}, available ${balance.available}`);for (const lot of balance.lots) { console.log(` ${lot.remaining} ${lot.kind} credits, expire ${lot.expires_at ?? "never"}`);}
// Newest first. Keep asking with meta.next_cursor until it is null.let cursor = "";let entries = 0;do { const page = await get("/v1/ledger", { limit: "50", cursor }); for (const entry of page.data) { entries += 1; console.log(` ${entry.created_at} ${entry.kind.padEnd(10)} ${entry.delta} -> ${entry.balance_after}`); } cursor = page.meta.next_cursor;} while (cursor);console.log(entries, "history entries");# Read the credit balance, then page through the credit history with the cursor.set -euo pipefailBASE="${ZEMO_API_BASE:-https://api.zemodata.com}"AUTH="Authorization: Bearer $ZEMO_API_KEY" # needs the ledger_read scope
curl --silent --show-error --fail-with-body -H "$AUTH" "$BASE/v1/credits"echo
# Newest first. Keep asking with meta.next_cursor until it is null.cursor=""pages=0while :; do page="$(curl --silent --show-error --fail-with-body -H "$AUTH" -G "$BASE/v1/ledger" \ --data-urlencode "limit=50" ${cursor:+--data-urlencode "cursor=$cursor"})" echo "$page" pages=$((pages + 1)) cursor="$(printf '%s' "$page" | sed -n 's/.*"next_cursor": *"\([^"]*\)".*/\1/p')" [ -n "$cursor" ] || breakdoneecho "$pages page(s) of history entries"GET /v1/usage answers a whole report at once (up to limit rows, 1,000 by default and at most
10,000); meta.truncated is true when there were more rows. Narrow the dates or the grouping, or
download the CSV.