<!-- source: https://www.secondlayer.tools/docs/cursors -->

# Cursors and Pages

Every list returns the same envelope, so one loop pages through any of them.

```json response.json status=200
{
  "events": [{ "block_height": 8054704, "event_index": 12 }],
  "next_cursor": "8054704:12",
  "tip": { "block_height": 8054704, "finalized_height": 8054262 },
  "reorgs": []
}
```

## The Envelope

Rows sit under a key named for the feed, beside the fields below.

| Field | Means |
| --- | --- |
| `next_cursor` | Pass it back as `cursor` for the next page. Set on every page with rows, `null` on an empty one |
| `tip.block_height` | The newest block served. On Index it is the height committed for the event type you read, so two reads can differ; `/blocks` and `/transactions` stay on the ingest tip (`source_block_height`) |
| `tip.finalized_height` | Rows at or below it never reorg |
| `reorgs` | Rows to undo since your last page: [Reorgs](https://www.secondlayer.tools/docs/reorgs.md) |

Subgraph tables answer with `rows`, a numeric cursor and their own tip:

```json response.json status=200
{
  "rows": [{ "_id": 48138, "block_height": 8054704 }],
  "next_cursor": "48137",
  "tip": { "block_height": 8054704, "subgraph_height": 8054704, "blocks_behind": 0 }
}
```

## Page Through It

The SDK's `walk()` follows the cursor for you.

```ts walk.ts
import { Index } from "@secondlayer/sdk";

const index = new Index();

for await (const transfer of index.ftTransfers.walk({
  recipient: "SP3F3GBGAR8781BF831JT4W7J5DR12ZB15A575DEC",
  fromHeight: 0,
})) {
  console.log(transfer.block_height, transfer.amount);
}
```

Over HTTP, pass `next_cursor` back as `cursor` on Index and subgraph routes.

| Surface | Page size | Order |
| --- | --- | --- |
| Index | `limit`, default 200, max 1000 | Oldest first; `order=desc` for newest first on events and ft/nft transfers |
| Streams | `limit`, default 100, max 1000 | Fixed by the cursor, no order param |
| Subgraph tables | `_limit`, default 50, max 1000 | `_id`, or `_sort=<column>&_order=asc\|desc` |

| Index also takes | Effect |
| --- | --- |
| `from_cursor` | Exclusive start; not with `cursor` |
| `from_height`, `to_height` | A height range |
| `wait` | Holds an empty page open (max 25 s) until new data commits; an older instance answers `400`, so fall back to polling |
| `contract_id` | A comma-separated set, max 20, on one cursor |
| `tx_context=true` | Puts `tx_sender`, `tx_type`, `tx_status`, `tx_contract_id` and `tx_function_name` on every event; for `print` it is the only place the sender lives |

On Index, a page shorter than `limit` is the last when `to_height` is at or below `tip.block_height`. [Subgraph reads](https://www.secondlayer.tools/docs/subgraphs.md#read-rows) cover sorting and search.

These subgraph-table requests return `400`, not a clamp:

- A `_limit` that is not an integer, or is `0`, negative or over 1000.
- An `_order` other than `asc` or `desc`.
- A bare `limit`, `order` or `sort`.

## Cursor Format

A cursor is `<block_height>:<event_index>`. Store it as an opaque string; it sorts in chain order.

Both bounds of a reorg's `orphaned_range` use it, tracking decoded-event positions. On transaction-keyed feeds (`/transactions`, `/contract-calls`), compare by the block-height part.

## Errors

Branch on `code`, not on the message.

```json response.json status=400
{
  "error": "unknown query param: limt (did you mean limit?)",
  "code": "VALIDATION_ERROR",
  "request_id": "req_3f9a1c0b7d2e4a6f8b1c2d3e",
  "feedback": { "url": "/v1/feedback" }
}
```

| Field | Is |
| --- | --- |
| `request_id` | Matches the `X-Request-Id` header, sent on every response; quote it in any report |
| `feedback.url` | Where to report a gap |

Report a gap with the `request_id`: [Report a Problem](https://www.secondlayer.tools/docs/report-a-problem.md).

## Read next

- [Reorgs](https://www.secondlayer.tools/docs/reorgs.md)
- [Subgraphs](https://www.secondlayer.tools/docs/subgraphs.md)
- [Report a Problem](https://www.secondlayer.tools/docs/report-a-problem.md)

## Notes for agents

- Rows sit under a named key (events, calls, rows) beside next_cursor, tip and reorgs. Pass next_cursor back as cursor; prefer the SDK walk() or consume().
- Subgraph tables page with _limit and _order; a bare limit or order is a 400, not a clamp.
- Store a cursor as an opaque string. Branch on the error code, not the message.

## Setup

Ensure setup once, skipping any step already done:
- CLI: `bun add -g @secondlayer/cli`
- Instance: `secondlayer init` writes `INSTANCE_TOKEN`. Loopback `/v1` reads need no key.
- Hosted reads: export `SECONDLAYER_API_KEY` (`sk-sl_*`) or buy credits with `secondlayer credits buy` (`balance`, `refill`), then `secondlayer whoami`
