Cursors and Pages

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

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

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

FieldMeans
next_cursorPass it back as cursor for the next page. Set on every page with rows, null on an empty one
tip.block_heightThe 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_heightRows at or below it never reorg
reorgsRows to undo since your last page: Reorgs

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

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

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

walk.tsts
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.

SurfacePage sizeOrder
Indexlimit, default 200, max 1000Oldest first; order=desc for newest first on events and ft/nft transfers
Streamslimit, default 100, max 1000Fixed by the cursor, no order param
Subgraph tables_limit, default 50, max 1000_id, or _sort=<column>&_order=asc|desc
Index also takesEffect
from_cursorExclusive start; not with cursor
from_height, to_heightA height range
waitHolds an empty page open (max 25 s) until new data commits; an older instance answers 400, so fall back to polling
contract_idA comma-separated set, max 20, on one cursor
tx_context=truePuts 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 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.

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.

Branch on code, not on the message.

400response.jsonjson
{
"error": "unknown query param: limt (did you mean limit?)",
"code": "VALIDATION_ERROR",
"request_id": "req_3f9a1c0b7d2e4a6f8b1c2d3e",
"feedback": { "url": "/v1/feedback" }
}
FieldIs
request_idMatches the X-Request-Id header, sent on every response; quote it in any report
feedback.urlWhere to report a gap

Report a gap with the request_id: Report a Problem.

Edit on GitHub

Quick Start

Copy this prompt into your agent.

Set up Secondlayer Index in this project. It reads Stacks chain data, decoded, from block 1.

1. Install `@secondlayer/sdk` with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. Ask me for a Secondlayer API key (`sk-sl_...`, from https://www.secondlayer.tools/signin) and put it in `.env` as `SECONDLAYER_API_KEY`. Never commit it. If I self-host, use `INSTANCE_TOKEN` and my instance URL instead. Until I give you one, read with a free trial key: `curl -X POST https://api.secondlayer.tools/api/public/trial-key` returns `{ key }`, read-only for Streams and Index, good for 24 hours and 100,000 rows. Keep it out of git too.
3. Add a note to `AGENTS.md`, creating it if needed: before using Secondlayer, follow the secondlayer skill if it's installed, and read https://www.secondlayer.tools/llms.txt. Before writing code, read https://www.secondlayer.tools/docs/index.md. Any docs page opens as Markdown when you add `.md` to its URL.
4. If you support skills, run: `bunx skills add ryanwaits/secondlayer --skill secondlayer -y`