Build Your First Index

Copy every sBTC transfer into a SQLite file on your machine, reorg-safe and resumable, in four steps.

$bun add @secondlayer/sdk

The SDK reads two variables. Hosted reads use an account key from Keys and Billing; on your own box, see Use these docs on your box.

$export SECONDLAYER_API_URL=https://api.secondlayer.tools
$export SECONDLAYER_API_KEY=sk-sl_...

The sink commits rows and the checkpoint together and rolls back when a reorg lands, so a crash resumes where it stopped.

index.tsts
import { Database } from "bun:sqlite";
import { Index } from "@secondlayer/sdk";
import { bunSqliteSink } from "@secondlayer/sdk/sinks/bun-sqlite";
const db = new Database("transfers.db");
db.exec(
"CREATE TABLE IF NOT EXISTS transfers (cursor TEXT PRIMARY KEY, block_height INTEGER, tx_id TEXT, sender TEXT, recipient TEXT, amount TEXT)",
);
await new Index().events.consume({
eventType: "ft_transfer",
assetIdentifier: "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token::sbtc-token",
fromHeight: 9_340_000,
sink: bunSqliteSink(db, { id: "sbtc-transfers", tables: ["transfers"], height: "block_height" }),
onBatch: (events, ctx) => {
for (const event of events) {
ctx.tx.run("INSERT OR IGNORE INTO transfers VALUES (?, ?, ?, ?, ?, ?)", [
event.cursor, event.block_height, event.tx_id, event.sender, event.recipient, event.amount,
]);
}
},
});

, (fromHeight: 0 backfills from genesis) and . Run it with bun index.ts.

$bun -e 'import { Database } from "bun:sqlite"; console.log(new Database("transfers.db").query("SELECT count(*) AS rows, max(block_height) AS tip FROM transfers").get())'

The count grows until the loop catches up with the chain. Stop it and run it again: it resumes from the committed checkpoint.

Index hands you rows in the shape we decode. For your own API shape and a hosted table, use Subgraphs; to run the loop somewhere permanent, see Deploy a 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`