Skip to content

How to Paginate Results

The Chainabit API supports two pagination strategies: offset-based and cursor-based. This guide shows how to use both and iterate through complete result sets.

Setup

bash
export TOKEN="your-access-token"

Offset-Based Pagination

Use the limit and offset query parameters to navigate through pages by position.

Request

bash
curl -s "https://api.chainabit.com/api/v1/ai/sessions?limit=20&offset=0" \
  -H "Authorization: Bearer $TOKEN"

Response

json
{
  "data": [
    { "id": "sess_01HQA...", "title": "Developer Pipeline" },
    { "id": "sess_01HQB...", "title": "Research Assistant" }
  ],
  "meta": {
    "total": 47,
    "limit": 20,
    "offset": 0,
    "hasNextPage": true,
    "hasPreviousPage": false
  }
}

Parameters

ParameterTypeDefaultDescription
limitinteger20Number of items per page (max 100)
offsetinteger0Number of items to skip

Check for More Pages

Use the meta.hasNextPage field to determine whether another page exists. Advance the offset by the limit value:

bash
# Page 1
curl -s "https://api.chainabit.com/api/v1/ai/sessions?limit=20&offset=0" -H "Authorization: Bearer $TOKEN"

# Page 2
curl -s "https://api.chainabit.com/api/v1/ai/sessions?limit=20&offset=20" -H "Authorization: Bearer $TOKEN"

# Page 3
curl -s "https://api.chainabit.com/api/v1/ai/sessions?limit=20&offset=40" -H "Authorization: Bearer $TOKEN"

Iterate Through All Pages

bash
OFFSET=0
LIMIT=20
HAS_NEXT=true

while [ "$HAS_NEXT" = "true" ]; do
  RESPONSE=$(curl -s "https://api.chainabit.com/api/v1/ai/sessions?limit=$LIMIT&offset=$OFFSET" \
    -H "Authorization: Bearer $TOKEN")

  # Process the current page
  echo "$RESPONSE" | jq '.data[] | .title'

  # Check for next page
  HAS_NEXT=$(echo "$RESPONSE" | jq -r '.meta.hasNextPage')
  OFFSET=$((OFFSET + LIMIT))
done

Cursor-Based Pagination

Some endpoints use cursor-based pagination for more efficient traversal of large or frequently changing datasets. Instead of an offset, you pass an opaque cursor token.

Request

bash
curl -s "https://api.chainabit.com/api/v1/billing/invoices?limit=25" \
  -H "Authorization: Bearer $TOKEN"

Response

json
{
  "data": [
    { "id": "inv_01HQS...", "status": "paid" },
    { "id": "inv_01HQT...", "status": "paid" }
  ],
  "meta": {
    "limit": 25,
    "nextCursor": "eyJpZCI6InNlc3Npb25fMDFIUVQuLi4ifQ==",
    "hasNextPage": true
  }
}

Parameters

ParameterTypeDefaultDescription
limitinteger25Number of items per page (max 100)
cursorstring(none)Cursor token from a previous response

Fetch the Next Page

Pass the nextCursor value as the cursor parameter:

bash
curl -s "https://api.chainabit.com/api/v1/billing/invoices?limit=25&cursor=eyJpZCI6Imludl8wMUhRVC4uLiJ9" \
  -H "Authorization: Bearer $TOKEN"

Iterate Through All Pages

bash
CURSOR=""
HAS_NEXT=true

while [ "$HAS_NEXT" = "true" ]; do
  if [ -z "$CURSOR" ]; then
    URL="https://api.chainabit.com/api/v1/billing/invoices?limit=25"
  else
    URL="https://api.chainabit.com/api/v1/billing/invoices?limit=25&cursor=$CURSOR"
  fi

  RESPONSE=$(curl -s "$URL" -H "Authorization: Bearer $TOKEN")

  # Process the current page
  echo "$RESPONSE" | jq '.data[] | .title'

  # Check for next page
  HAS_NEXT=$(echo "$RESPONSE" | jq -r '.meta.hasNextPage')
  CURSOR=$(echo "$RESPONSE" | jq -r '.meta.nextCursor // empty')
done

Choosing a Strategy

Offset-basedCursor-based
Jump to a specific pageYesNo
Stable during concurrent insertsNo (items can shift)Yes
Performance on large datasetsSlower at high offsetsConsistent
Use whenSmall datasets, random page accessLarge or real-time datasets

If an endpoint supports both strategies, cursor-based pagination is recommended for production workloads.

Built with purpose.