Skip to content

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_cursor is an opaque string. Send it back as cursor with the same filters to get the next page.
  • When meta.next_cursor is null, 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:

python/credits.py
"""Read the credit balance, then page through the credit history with the cursor."""
import json
import os
import urllib.parse
import 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 = "", 0
while 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:
break
print(entries, "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.