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

# Build Your First Index

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

## Install the SDK

```bash
bun add @secondlayer/sdk
```

## Set your key

The SDK reads two variables. Hosted reads use an account key from [Keys and Billing](https://www.secondlayer.tools/docs/authentication.md); on your own box, see [Use these docs on your box](https://www.secondlayer.tools/docs/self-host.md#use-these-docs-on-your-box).

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

## Write the consume loop

The [sink](https://www.secondlayer.tools/docs/sinks.md) commits rows and the checkpoint together and rolls back when a reorg lands, so a crash resumes where it stopped.

```ts index.ts
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,
      ]);
    }
  },
});
```

Name the event, pick a start block (`fromHeight: 0` backfills from genesis) and hand the checkpoint to a sink. Run it with `bun index.ts`.

## Read the rows

```bash
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](https://www.secondlayer.tools/docs/subgraphs.md); to run the loop somewhere permanent, see [Deploy a Consumer](https://www.secondlayer.tools/docs/deploy.md).

## Read next

- [Keys and Billing](https://www.secondlayer.tools/docs/authentication.md)
- [Self-host](https://www.secondlayer.tools/docs/self-host.md)
- [Sinks](https://www.secondlayer.tools/docs/sinks.md)
- [Subgraphs](https://www.secondlayer.tools/docs/subgraphs.md)
- [Deploy a Consumer](https://www.secondlayer.tools/docs/deploy.md)

## Notes for agents

- Install '@secondlayer/sdk' and set SECONDLAYER_API_URL and SECONDLAYER_API_KEY (or the instance token on a self-hosted box). new Index() reads them from env.
- index.events.consume({ eventType, fromHeight, sink, onBatch }) with bunSqliteSink: write only through ctx.tx so rows and checkpoint commit in one transaction; the sink rolls back reorgs. Create the table with a block_height column first.

## 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`
