Skip to main content

Pagination with cursors

GET /animals returns one page at a time. Each response carries the records, a cursor for the next page, and a flag that says whether another page exists.

200 OK
{
"data": [
{ "id": "pet_krypto_210", "name": "Krypto", "status": "AVAILABLE" },
{ "id": "pet_luna_118", "name": "Luna", "status": "AVAILABLE" }
],
"next_cursor": "eyJvZmZzZXQiOjQwfQ",
"has_more": true
}

Parameters​

ParameterTypeDefaultBounds
limitinteger201 to 100
cursorstringnoneThe next_cursor value from the previous page

Omit cursor on the first call. Every later call sends the value the prior response returned, unchanged.

Read the flag, not the array​

A page can hold fewer rows than limit and still leave more behind, because filters run after the slice. Twenty rows on a page says nothing about the rows behind it.

has_more: false marks the end, and next_cursor returns null on that same page. A loop keyed to an empty data array will stop late and waste a call. A loop keyed to has_more stops exactly once.

Walk every page​

cursor=""
while :; do
page=$(curl -s -G https://api.bludhavenpets.com/v1/animals \
--data-urlencode "status=AVAILABLE" \
--data-urlencode "limit=100" \
--data-urlencode "cursor=${cursor}")
echo "$page" | jq -r '.data[] | [.id, .name] | @tsv'
has_more=$(echo "$page" | jq -r '.has_more')
[ "$has_more" = "true" ] || break
cursor=$(echo "$page" | jq -r '.next_cursor')
done
Node.js
async function listAvailableAnimals(token) {
const animals = [];
let cursor = '';

for (;;) {
const url = new URL('https://api.bludhavenpets.com/v1/animals');
url.searchParams.set('status', 'AVAILABLE');
url.searchParams.set('limit', '100');
if (cursor) url.searchParams.set('cursor', cursor);

const response = await fetch(url, {
headers: { Authorization: `Bearer ${token}` },
});
if (!response.ok) throw new Error(`List failed with ${response.status}`);

const page = await response.json();
animals.push(...page.data);
if (!page.has_more) return animals;
cursor = page.next_cursor;
}
}
Python
import requests


def list_available_animals(token):
animals = []
cursor = ""
while True:
params = {"status": "AVAILABLE", "limit": 100}
if cursor:
params["cursor"] = cursor
response = requests.get(
"https://api.bludhavenpets.com/v1/animals",
params=params,
headers={"Authorization": f"Bearer {token}"},
timeout=20,
)
response.raise_for_status()
page = response.json()
animals.extend(page["data"])
if not page["has_more"]:
return animals
cursor = page["next_cursor"]

Treat the cursor as opaque​

The value encodes a position the server chose. A client that decodes it and builds its own breaks when the encoding changes, and the change isn't a contract break.

Carry the string across the wire without trimming, decoding, or reformatting it. It fits in a query parameter, and some proxies will truncate a length they consider unreasonable. Keep the whole value.

Keep the filters steady​

A cursor belongs to the filter set that produced it. Send status, species, branch_id, and temperament_tag unchanged on every page of one walk.

Change a filter mid-walk and the position no longer describes the same result set. Start again from an empty cursor when the filter changes, which is what a search box does on each keystroke anyway.

Collections that return everything​

GET /retail/inventory and GET /animals/{animalId}/health-records return one complete object rather than a page. Inventory holds a map of SKU to count, and a health record belongs to one animal.

A client that writes against the envelope shape handles both cases with one branch on Array.isArray(body.data). Read the shape the contract declares rather than assuming a list.