Checkpoints

Commit your rows and the cursor together, and a crash resumes where it stopped.

, , and when a reorg lands.

consumer.tsts
import { Index } from "@secondlayer/sdk";
const index = new Index();
await index.events.consume({
eventType: "ft_transfer",
fromCursor: await db.loadCheckpoint(),
fromHeight: 0,
onBatch: async (events, ctx) => {
await db.transaction(async (tx) => {
for (const row of events) await tx.upsert(row);
await tx.saveCheckpoint(ctx.cursor);
});
return ctx.cursor;
},
onReorg: (reorg) => db.deleteFromHeight(reorg.fork_point_height),
});
ft_transfer
  • fromCursor is null on the first run, and fromHeight: 0 backfills as far as your instance holds. Older history is restored from the archive, free.
  • A reorg rewinds the cursor to the fork point and fires onReorg. Delete inclusive of the fork block: Reorgs.
  • Fold running balances in a sink's onRollback: Sinks.
  • Where to run it: Deploy.

consume is on sl.streams.events, sl.index.events and sl.index.contractCalls. Every signature is in the SDK reference.

OptionEffect
eventTypeA literal narrows the rows: "ft_transfer" hands onBatch typed amount and sender, "print" hands it payload.topic
contractIdOne id, or up to 20 to follow a protocol on one cursor. Mutually exclusive with trait
txContext: trueJoins the submitting transaction into each event (tx_sender, tx_type, ...). Same on walk and list
finalizedOnly: trueHolds delivery to rows at or below tip.finalized_height, so onReorg is unnecessary
signalAbort, checked between batches and never mid-commit; see Deploy
index.contractCalls.consume()The same loop for decoded calls. Already carries sender and function_name, so it needs no txContext

finalizedOnly trades freshness for safety, and can put you hundreds of blocks behind the tip.

ctx says where the sweep has reached, so liveness needs no bookkeeping of its own.

FieldIs
cursorThe checkpoint to commit with your rows
heightHighest canonical block a row was delivered from; parks on a quiet filter
scannedHeightHighest block verified; an empty page proves nothing matches up to the tip. Rolled back on a reorg
tipHeightChain tip as of this page's read
blocksBehindtipHeight - scannedHeight, floored at 0; about 0 for a caught-up tail even when height is old

Every feed offers three verbs. They are not interchangeable.

const { rows, next_cursor } = await sl.index.blocks.list({ limit: 10 });
for await (const t of sl.index.ftTransfers.walk({ contractId })) {}
await sl.index.events.consume({ eventType: "ft_transfer", onBatch });
VerbGives youFollows cursorsCheckpointsReorgsEnds
list()One page plus the envelopeNo; you pass cursor backNoReports reorgs[]; you actImmediately
walk()An async iterable of rowsYesNoIgnoredOn catching the tip
consume()A loop calling your onBatchYesYes; you commit the cursorRewinds and fires onReorgNever; tails until aborted

walk() into a database is a trap: a crash restarts from zero, a fork leaves orphaned rows behind, and it stops at the tip. Writing rows you intend to keep is consume().

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`