Reorgs

A reorg orphans blocks your tables already hold: delete your rows from the fork height, inclusive, then read forward again.

$curl "$SECONDLAYER_API_URL/v1/streams/reorgs?since=2026-08-01T00:00:00Z&limit=1" \
-H "Authorization: Bearer sk-sl_…"
200response.jsonjson
{
"reorgs": [
{
"id": "9dba566b-8325-45df-bc17-aed6be93ff70",
"detected_at": "2026-08-01T18:30:11.356245Z",
"fork_point_height": 8683934,
"old_index_block_hash": "0x500e56fb65237ebec60a6da210135d9e96c9f62e0ff5a07cc2bbb72e34e5f849",
"new_index_block_hash": "0x2569032e50f5cc1f783701d992691ab5ab97d56ff4e9a5d58839d5643cc2205a",
"orphaned_range": { "from": "8683934:0", "to": "8683935:14" },
"new_canonical_tip": "8683934:0"
}
],
"next_since": "2026-08-01T18:30:11.356245Z~9dba566b-8325-45df-bc17-aed6be93ff70"
}

Keep rows from the orphaned range and your tables describe a chain that no longer exists. Every read envelope carries the same fork in its reorgs array: Cursors and Pages.

How you read decides how much rollback code you write.

You read withWhat happens on a reorgYou do
A consumerIt rewinds and calls onReorgDelete from the fork height
A sinkIt deletes the rows and rereadsNothing
A subgraphRows are rewritten under new idsWait a few blocks before moving money
A webhookchain.reorg.rollback lists what may have arrivedUndo each entry
A plain list()reorgs[] names what to undoDo it by hand
  • Delivery after a rewind is at least once, so keep writes idempotent.
  • The fork block itself is re-delivered, which is why the delete is inclusive.

A consumer rewinds under these rules:

RuleDetail
ScopeOnly forks at or below your checkpoint roll back; a fork above it has nothing to undo
DepthA rewind deeper than maxRollbackDepth (default 1000 blocks) is refused before anything is deleted
IdleOn empty pages the loop polls reorgs.list, so a fork that lands while idle at the tip is caught, at one extra request per idle poll
GenesisA fork at genesis hands onReorg a null cursor

By hand, roll back rows whose cursor falls inside orphaned_range, then re-fetch from new_canonical_tip. Both bounds are <block_height>:<event_index> cursors; on transaction-keyed feeds, compare by the block height. A consumer does all of this for you.

Check Your Mirror

Compare your stored hashes with the canonical chain any time, and re-sweep from the first block that diverged.

const { canonical } = await sl.index.canonical({ fromHeight: myLowestHeight });
for (const block of canonical) {
if (myHashAt(block.block_height) !== block.block_hash) {
await db.deleteFromHeight(block.block_height);
break;
}
}

Rows at or below tip.finalized_height never reorg, so a consumer that reads only those has nothing to undo. The tradeoff is freshness: finalizedOnly holds delivery until each row is final, which can put you hundreds of blocks behind the tip.

Use it for accounting and reports, and keep the rollback path for anything a user watches live. The option is on the checkpointed consumer.

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`