# Pagination (https://docs.predictorsdk.com/guides/pagination)

Walk long lists page by page with cursors, by hand or with the SDKs.

Endpoints that can return long lists answer one page at a time. Each page ends
with a `next_cursor`; send it back as `cursor` to get the next page. The cursor
is opaque, so don't try to read or build one.

## Endpoints that paginate [#endpoints-that-paginate]

| Endpoint | Default `limit` | Max `limit` |
|---|---:|---:|
| `GET /v1/matching-markets/sports` | 25 | 100 |
| `GET /v1/markets` | 25 | 100 |
| `GET /v1/polymarket/wallet/positions` | 50 | 100 |
| `GET /v1/crypto-prices/binance` | 100 | 100 |

A few details:

- Wallet positions keep the page size of the first page on every later page,
  because it travels in the cursor.
- Crypto prices are per second, newest first. With no `start_time`, `end_time`
  or `cursor`, the route answers the single latest price and no next page. Its
  old `pagination_key` parameter is a `400` that names `cursor`.
- `GET /v1/matching-markets/sports/lookup` doesn't paginate. It answers at most
  100 IDs in one response, with no `pagination` block, `limit` or `cursor`.

## Response shape [#response-shape]

Every paginated response carries its items in `data`, in page order, and a `pagination` block that is always the same:

```json
{
  "pagination": {
    "limit": 25,
    "total": 143,
    "has_more": true,
    "next_cursor": "eyJzIjoiYWJjMTIzZWYi..."
  }
}
```

| Field | Description |
|---|---|
| `limit` | Page size used for this response. Wallet positions cursors preserve the first page's size; other endpoints echo the `limit` query param (or the default). Crypto prices report `1` when you send no time range. |
| `total` | Total matching items across all pages, or `null` when the route cannot count them: wallet positions and crypto prices, whose upstreams give no total. `0` only ever means an empty result. Use `has_more` and `next_cursor` whatever `total` says. |
| `has_more` | `true` when more pages exist beyond this one. |
| `next_cursor` | Pass back via the `cursor` query param for the next page. Absent when `has_more` is `false`. |

## Walking every page [#walking-every-page]

The SDKs walk the cursor for you. A call to one of the four paginated operations returns a page (TypeScript and Go) or a pager (Python); iterating it visits every item on every page, sends each next `cursor`, and repeats the filters that issued it. With cURL you follow `next_cursor` yourself.

**cURL**
```bash
# Page 1
curl -H "Authorization: Bearer $API_KEY" \
  "https://api.predictorsdk.com/v1/matching-markets/sports?limit=25"

# Page 2 (use next_cursor from page 1's response)
curl -H "Authorization: Bearer $API_KEY" \
  "https://api.predictorsdk.com/v1/matching-markets/sports?limit=25&cursor=eyJzIjoi..."
```
**TypeScript**
```ts
import { PredictorSDK } from "@predictorsdk/client";

const allEvents: PredictorSDK.CanonicalSportsEvent[] = [];

// Iterating the page walks every page.
for await (const event of await client.getSportsMatchingMarkets({ limit: 50 })) {
  allEvents.push(event);
}

// Or step through pages yourself: page.data is the current page,
// page.response.pagination its pagination block.
const page = await client.getSportsMatchingMarkets({ limit: 50 });
while (true) {
  console.log(page.data.length, page.response.pagination.total);
  if (!page.hasNextPage()) break;
  await page.getNextPage(); // moves `page` onto the next page
}
```
**Python**
```python
# Iterating the pager walks every page.
all_events = list(client.get_sports_matching_markets(limit=50))

# Or step through pages yourself: page.items is the current page,
# page.response.pagination its pagination block.
pager = client.get_sports_matching_markets(limit=50)
for page in pager.iter_pages():
    print(len(page.items), page.response.pagination.total)
```
**Go**
```go
var allEvents []*predictorsdk.CanonicalSportsEvent

page, err := client.GetSportsMatchingMarkets(ctx, &predictorsdk.GetSportsMatchingMarketsRequest{
    Limit: predictorsdk.Int(50),
})
if err != nil {
    return err
}

// The iterator walks every page, starting with this one.
iterator := page.Iterator()
for iterator.Next(ctx) {
    allEvents = append(allEvents, iterator.Current())
}
if err := iterator.Err(); err != nil {
    return err
}

// Or step through pages yourself: page.Results is the current page,
// page.Response.Pagination its pagination block. GetNextPage returns
// core.ErrNoPages after the last page, which is not a failure.
```

Iterating walks every page, so to read only the first, use `page.data` (TypeScript), `pager.items` (Python) or `page.Results` (Go). A manual `cursor` is still accepted, to resume a walk you saved; send it with the same filters.

## Important rules [#important-rules]

### Keep request shape constant across pages [#keep-request-shape-constant-across-pages]

The cursor is scoped to the request shape it was issued with. If you change `include_settled`, `provider`, `category`, or any other supported filter mid-traversal, you'll get a `400`:

```json
{
  "code": "invalid_cursor",
  "message": "pagination cursor does not match the current filters; a cursor continues the provider and category values that issued it, on /v1/markets only",
  "status_code": 400,
  "param": "cursor"
}
```

On matching-markets the message also quotes the cursor and names the
parameters it binds (`include_settled`, `include_submarkets`,
`player_prop_match`, `league`, `scheduled_date` and `participant`).

Drop the cursor and start fresh from page 1 whenever filters change.

### Wallet positions cursors preserve the page size [#wallet-positions-cursors-preserve-the-page-size]

For `GET /v1/polymarket/wallet/positions`, choose `limit` on the first page.
When following `next_cursor`, omit `limit` or keep it the same: a different
valid `limit` does not change the cursor's page size, and `pagination.limit`
reports the size actually used. Keep the same resolved wallet address.

If a saved positions cursor returns `400`, start again from the first page:
cursors issued before the move to Polymarket's Data API v2 can't be continued.

### Crypto price cursors carry their range [#crypto-price-cursors-carry-their-range]

For `GET /v1/crypto-prices/binance`, `next_cursor` carries the `start_time` of
the request that issued it, so you can follow it with only `currency`, `limit`
and `cursor`: the next page stays inside the range. Sending a different
`start_time` with a cursor is a `400`, and `end_time` is ignored when a cursor
is sent. Pages run newest first, each the `limit` newest seconds at or before
the end of the last one, and walking every page returns every second of the
range exactly once.

```bash
curl -H "Authorization: Bearer $API_KEY" \
  "https://api.predictorsdk.com/v1/crypto-prices/binance?currency=btcusdt&start_time=1775680500000&end_time=1775680559999&limit=30"

# next page: cursor only, from the previous pagination.next_cursor
curl -H "Authorization: Bearer $API_KEY" \
  "https://api.predictorsdk.com/v1/crypto-prices/binance?currency=btcusdt&limit=30&cursor=eyJpZCI6..."
```

### Market cursors bind to an immutable snapshot [#market-cursors-bind-to-an-immutable-snapshot]

`GET /v1/markets` ties a traversal to the catalog as it was on its first page.
Venues refreshing in the meantime don't change what the cursor sees, so the
remaining pages come from the same catalog and nothing is skipped.

An older catalog normally stays available for up to 8 hours, though it can go
sooner when storage runs short. After that, an unfinished cursor gets:

```json
{
  "code": "invalid_cursor",
  "message": "pagination cursor is stale; restart from the first page",
  "status_code": 400,
  "param": "cursor"
}
```

Restart from page 1 whenever a retained cursor goes stale.

### `snapshot.observed_at` tells you how old the page is [#snapshotobserved_at-tells-you-how-old-the-page-is]

The catalog is read from a stored copy, not from the venues, so its rows are as
old as the last crawl of each venue, not as old as your request. Every
`GET /v1/markets` response says how old:

```json
{
  "data": [ ],
  "snapshot": { "observed_at": "2026-08-25T13:41:22.108Z" },
  "pagination": { "limit": 25, "total": 1393200, "has_more": true, "next_cursor": "..." }
}
```

Three things to know:

- **It is the oldest time.** Each venue is crawled on its own schedule, so the
  catalog mixes crawls of different ages, and `observed_at` is when the oldest
  of them began. Every row is at least that fresh, and most are fresher.
- **`?provider=` makes it exact.** With a provider filter, it is that venue's
  own crawl time, the cheapest way to get a precise age for the rows you care
  about.
- **It doesn't change during a traversal.** Every page of one traversal reports
  the same value, even while newer crawls land. Start from the first page again
  to see a newer catalog.

`null` means the bound snapshot carries no read time; treat it as unknown age
rather than as fresh.

### Cursors are ephemeral [#cursors-are-ephemeral]

Treat cursors as short-lived: finish a traversal promptly, don't count on the
full 8 hours, and don't share them between clients. Start a new first page when
you want the latest data.

## Default behavior [#default-behavior]

- **No `limit`** → `limit=25` for matching-markets and markets; wallet positions use `50` on the first page and the cursor's page size on later pages; crypto prices use `100`.
- **No `cursor`** → first page.
- **`limit > 100`** → `400` with `code: "invalid_parameter"` and `param: "limit"`.
- **Malformed `cursor`** → `400` with `code: "invalid_cursor"`.
- **Cursor with stale scope** → `400` with `code: "invalid_cursor"`; the message says the cursor does not match the current filters.
- **A `/v1/markets` cursor whose catalog is gone** → `400` with `code: "invalid_cursor"` and the message `pagination cursor is stale; restart from the first page`.