Local Chain

Run Secondlayer against a Clarinet devnet: Streams, Index, Subgraphs and Webhooks, with no cloud and no account.

Run it inside your Clarinet project.

~/clarinet-project
$secondlayer devnet connect
Secondlayer stack up
api → http://localhost:3800
indexer → http://localhost:3700
The api is published on 127.0.0.1 only: /v1 reads need no credential.
Writes (deploys, webhooks) send INSTANCE_TOKEN=dev-instance-token.
You needWhy
Clarinet and a Clarinet.toml projectThe devnet and your contracts
Docker (Docker Desktop, or OrbStack on macOS)Runs Postgres and the runtime
The CLI: bun add -g @secondlayer/cliProvides secondlayer devnet
Connect doesDetail
Patches settings/Devnet.tomlThe devnet node forwards events to the indexer on :3700
Writes .secondlayer/docker-compose.ymlPostgres plus the one-box runtime image, the one Self-host runs
Pulls and starts bothdocker compose up -d
Sets local credentialsA fixed instance token, a public dev signing key for webhooks, an allowlist so webhooks reach your localhost receiver

The devnet deploys your contracts and streams every block to the indexer.

$clarinet devnet start

Drop services you don't need in settings/Devnet.toml by setting them to true:

KeySavesCosts you
disable_stacks_explorermemorythe explorer UI
disable_bitcoin_explorermemorythe Bitcoin explorer UI
disable_stacks_apimemory, :3999the faucet and the default devnet chain node URL
disable_postgresmemory, :5432the Stacks API, which needs it

Send a call

Use @secondlayer/stacks against the devnet node: the devnet chain defaults to the Stacks API on :3999, so set transport when that API is off.

$bun add @secondlayer/stacks
call.tsts
import { createWalletClient, http } from "@secondlayer/stacks";
import { privateKeyToAccount } from "@secondlayer/stacks/accounts";
import { devnet } from "@secondlayer/stacks/chains";
const client = createWalletClient({
account: privateKeyToAccount(
"753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601", // devnet deployer, from settings/Devnet.toml
),
chain: devnet,
transport: http("http://localhost:20443"), // the devnet node; the Stacks API on :3999 may be off
});
const txid = await client.callContract({
contract: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter",
functionName: "increment",
args: [],
});
console.log(txid);

The reads below use a counter contract that prints an event on each call:

contracts/counter.clarclarity
(define-data-var counter uint u0)
(define-public (increment)
(let ((new-value (+ (var-get counter) u1)))
(var-set counter new-value)
(print {event: "incremented", value: new-value})
(ok new-value)))

Reads need no credential (loopback only). Call increment a few times first.

Streams

Raw events from the node. The newest blocks are held back as a reorg margin, so an event appears a couple of blocks later.

$curl "http://localhost:3800/v1/streams/events?contract_id=ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter"
{
"events": [
{
"cursor": "69:0",
"block_height": 69,
"tx_id": "0x606013dd2a02400045e1d28ab7d1d84649014e4d81063daa1cc83d45af3149ff",
"event_type": "print",
"contract_id": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter",
"payload": { "topic": "print", "raw_value": "0x0c00000002056576656e74…" },
"canonical": true,
"finalized": false
}
],
"next_cursor": "69:0",
...
}

Index

Decoded transactions and events. The id needs its 0x prefix, which broadcastTransaction leaves off.

$curl "http://localhost:3800/v1/index/transactions/0x606013dd2a02400045e1d28ab7d1d84649014e4d81063daa1cc83d45af3149ff"
$curl "http://localhost:3800/v1/index/events?contract_id=ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter&event_type=print"
{
"transaction": {
"tx_id": "0x606013dd2a02400045e1d28ab7d1d84649014e4d81063daa1cc83d45af3149ff",
"block_height": 69,
"tx_type": "contract_call",
"status": "success",
"contract_call": {
"contract_id": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter",
"function_name": "increment",
"result": "1"
},
...
},
...
}
{
"events": [
{
"cursor": "69:0",
"event_type": "print",
"contract_id": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter",
"payload": { "topic": "print", "value": { "event": "incremented", "value": "1" } }
}
],
...
}

Subgraphs

Generate a subgraph from indexed prints (send a call first), deploy it, then read its table. dev-instance-token is a local value, not a secret.

$secondlayer subgraphs create counter --from-contract ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter
$export SECONDLAYER_API_URL=http://localhost:3800
$export INSTANCE_TOKEN=dev-instance-token
$secondlayer subgraphs deploy subgraphs/counter.ts
$curl "$SECONDLAYER_API_URL/v1/subgraphs/counter/counter?_limit=5"
{
"rows": [
{
"_id": "1",
"_block_height": "69",
"_tx_id": "0x606013dd2a02400045e1d28ab7d1d84649014e4d81063daa1cc83d45af3149ff",
"topic": "print",
"value": { "event": "incremented", "value": "1" }
},
...
],
"next_cursor": "3",
"tip": { "block_height": "77", "subgraph_height": 77, "blocks_behind": 0 }
}

Webhooks

A receiver on your machine is host.docker.internal, not localhost.

receiver.tsts
Bun.serve({
port: 4999,
hostname: "0.0.0.0",
async fetch(request) {
console.log(
[...request.headers.keys()].filter((key) => key.includes("signature")).join(","),
await request.text(),
);
return new Response("ok");
},
});
$secondlayer webhooks create counter-prints -u http://host.docker.internal:4999/hook \
--trigger '{"type":"print_event","contractId":"ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter"}'

The receiver logs one line per call:

webhook-signature,x-secondlayer-signature,x-secondlayer-signature-keyid {"type":"chain.print_event.apply","timestamp":"2026-11-03T17:59:56.000Z","data":{"event":{"data":{"topic":"print","value":{"event":"incremented","value":"1"},…},"type":"contract_event","tx_id":"0x606013dd…49ff","event_index":0},"action":"apply","trigger":"print_event","canonical":true,"block_height":69}}

Deliveries are signed with a public dev key: GET /api/webhooks/signing-key returns it (verification).

Commands for the running stack.

Mock sBTC

Mint mock sBTC on your devnet. It needs the Stacks API on :3999, so leave disable_stacks_api off.

$secondlayer devnet faucet --to ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
minting 1000000 sats to ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
via ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.sbtc-deposit as signer ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
✓ Minted 1000000 sats of mock sBTC to ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.
tx 954d9045f1cfbe86db4c837952b59dea34ae857849aa3299f0fdac1544aaf601
Mock sBTC on your devnet — not the canonical asset.
FlagDefaultPurpose
--to <address>requiredRecipient Stacks address
--amount <sats>1000000Amount in sats, minimum 546
--contract <id><deployer>.sbtc-depositsBTC deposit contract
--rpc-url <url>http://localhost:3999Devnet node RPC

Watch it work

-w refreshes every 2s; --limit <n> sets the recent rows.

~/clarinet-project
$secondlayer devnet status
STACK
● indexer healthy :3700
● api healthy :3800
INGEST
chain tip 77 indexed 77 lag caught up last block 57s ago
SUBGRAPHS
counter active block 77 counter · 3 rows
ACTIVITY (recent)
72 counter print {"even…"3"}
70 counter print {"even…"2"}
69 counter print {"even…"1"}

Services are secondlayer and postgres.

$secondlayer devnet logs secondlayer -f

Options

FlagDefaultPurpose
--project <dir>nearest Clarinet.tomlClarinet project directory
--image-tag <tag>latestPublished image tag to run
--owner <owner>Secondlayer namespaceghcr image owner to pull from
--no-upnonePatch config and write compose without starting Docker

Tear down

$secondlayer devnet down # stop the stack
$secondlayer devnet down --purge # stop and wipe the local index volumes
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`