Self-host
The same product on your Postgres: one container, no cluster, no queue, no meter.
You need Docker, the hardware and the CLI: bun add -g @secondlayer/cli.
Configure
Start
| Step | What it does |
|---|---|
POSTGRES_PASSWORD | Set it in .env after copying |
init | Generates the instance token and signing keys; appending them puts everything compose reads in one .env |
pull | Fetches the published images; without it compose reuses any older copy on the box |
/health never answers | docker compose logs secondlayer says why, most often a floor |
Use these docs on your box
Point SECONDLAYER_API_URL at your own API.
| Case | Credential |
|---|---|
| Reads on a loopback bind | None. A header with an unknown or empty key is ignored, so the same curl lines work unchanged |
| Writes over curl, any read past loopback | Authorization: Bearer $INSTANCE_TOKEN |
| CLI, SDK, MCP | Send INSTANCE_TOKEN for you on any URL that is not the hosted API; MCP needs it in the client config's env, not just your shell |
SECONDLAYER_API_KEY | Leave it for the hosted account: credits and the Contract state plan's state_writes fetch read it, so setting it to the instance token breaks them. With it set and no INSTANCE_TOKEN, tools use the hosted API unless the URL is set |
POSTGRES_PORT, INDEXER_PORT | Keep on loopback |
Fill it with history
A new box holds only what your node sends from now on. Restore history from the archive, free and with no account.
Stop it
The index, chainstate and keys survive. Never add -v.
Support is community: github.com/ryanwaits/secondlayer.
Profiles
Set them in .env.
| Profile | What runs |
|---|---|
| default | postgres + secondlayer (API, ingest, decoder, subgraph, webhooks) |
full-node | + bundled Stacks node and bitcoind |
bitcoin | + runes, the Runes indexer (needs a Bitcoin node with txindex=1) |
NODE_MODE | You bring | Also set |
|---|---|---|
external (default) | Your Stacks node | |
stacks | Your Stacks node, same as external | |
full | Nothing: both nodes run in the box | BITCOIN_RPC_PASSWORD, the full-node profile |
No bundled Stacks-only mode: it needs Bitcoin data.
Pin a version
docker compose up runs latest; pin a commit instead.
| Fact | Detail |
|---|---|
| Images | ghcr.io/ryanwaits/secondlayer-runtime and secondlayer-bitcoin, tagged by short commit |
docker compose up -d --build | Builds your checkout instead |
Connect your node
With NODE_MODE=external, this Config.toml entry sends every block: secondlayer observer --mode signer-shared --endpoint 10.0.0.5:3700 prints it.
| Field | indexer | signer-shared | Effect |
|---|---|---|---|
endpoint | host:port | host:port | Where the node posts blocks. No scheme. |
events_keys | ["*"] | ["*"] | Every standard event. Unknown keys stop a stock node from starting. |
timeout_ms | 2000 | 500 | How long the node waits on each delivery. |
disable_retries | false | true | false retries until delivered, holding up the node meanwhile. |
| Node on another host | Value | Why |
|---|---|---|
INDEXER_PORT in .env | 10.0.0.5:3700 | The default 127.0.0.1:3700 is unreachable off-box |
STACKS_NODE_RPC_URL in .env | http://<node-ip>:20443 | Polled for the tip when pushes stop |
| Firewall | 3700 open to the node's IP only | The observer port takes unauthenticated posts |
Beside a signer, use signer-shared. The tradeoff: a missed block stays a gap until secondlayer repair fetches it from the archive, free.
Config
Secrets come from secondlayer init; unknown keys fail before ingest.
| Key | Default | Notes |
|---|---|---|
NETWORK | mainnet | mainnet, testnet, devnet |
DATABASE_URL | compose | Injected; one Postgres |
NODE_MODE | external | external, stacks, full |
DATA_DIR | /data | Handler + runtime files |
API_PORT | 127.0.0.1:3800 | Publish spec for the API port |
INDEXER_PORT | 127.0.0.1:3700 | Publish spec for the observer port |
API_PUBLISH_ADDR | compose | Injected from the same API_PORT value the port is published with |
Instance token
init writes INSTANCE_TOKEN to .env.local: not the account key from Keys and Billing.
| Surface | Credential | Behavior |
|---|---|---|
/v1 on 127.0.0.1 | none | your Postgres, no meter |
/v1 past loopback | INSTANCE_TOKEN | same header on Index, Streams, subgraphs |
Writes (/api/subgraphs, /api/webhooks, /api/node) | INSTANCE_TOKEN | loopback included |
Loopback is the publish spec (API_PUBLISH_ADDR), not the caller's address.
API_PORT | Effect |
|---|---|
127.0.0.1:3800 (default) | /v1 reads stay keyless |
0.0.0.0:3800 | Every /v1 request carries the token |
| Either | Writes take the token. The container binds 0.0.0.0 so the published port reaches it, and an instance that binds past loopback with no token refuses to start |
What differs on your box
| Area | On your box |
|---|---|
| Metering | None. Your Postgres, no meter, no rate limit |
| Subgraph sources | Any. Hosted takes event filters, contract_call and contract_deploy |
| Typed ORM schemas | secondlayer codegen subgraph needs direct Postgres, so it is self-host only |
| Handler RPC calls | Set STACKS_NODE_RPC_URL on the subgraph processor, pointing at the node you already run |
| History below your bootstrap floor | A free archive backfill |
| Webhook limits | Any max_retries and timeout_ms, no rate limits, egress as your network allows |
| Proof routes | Witness, burn and Bitcoin-header routes answer 503 PROOFS_UNAVAILABLE until PROOF_SIDECAR_URL points at a proof sidecar. The Bitcoin transaction route reads the bundled bitcoind (full-node profile, txindex=1) or the node at BITCOIN_RPC_URL, and answers 502 PROOF_SOURCE_ERROR when it can't reach one |
| Bitcoin output spends | /v1/bitcoin/outspend reads the same node and needs Bitcoin Core 24 or later. It answers 503 BITCOIN_UNAVAILABLE until BITCOIN_RPC_URL is set, and 502 BITCOIN_SOURCE_ERROR when it can't read the node |
| Error reports | No /v1/feedback. Error bodies link to a GitHub issue instead |
| Consumers on another box | Deploy a Consumer |
Verify and repair
Archive owns flags and exit codes.
Archive trust root
One key checks every manifest. The tradeoff: rotating it means a CLI release.
| Order | Key source |
|---|---|
| 1 | --public-key |
| 2 | ARCHIVE_SIGNING_PUBLIC_KEY in your env |
| 3 | The key compiled into the CLI release |
| Fact | Detail |
|---|---|
| Applies to | bootstrap, verify and repair |
init | Writes the resolved key into .env.local, so the instance verifies with no path to any Secondlayer host |
http:// key sources | Never consulted |
latest.json pointer | Must verify against the same key before its snapshot is followed |
| Pin a different archive | Replace ARCHIVE_SIGNING_PUBLIC_KEY in .env; a value you set there survives init re-runs |
Running other teams' subgraphs
Handlers run with the instance's trust: give each team its own stack.
| Per team | Setting |
|---|---|
| Database | Its own Postgres and its own network |
| Containers | api and subgraph-processor under gVisor (runtime: runsc), as a non-root user |
| Limits | Read-only root filesystem, bounded memory, CPU and process count |
| Networking | gVisor can't reach Docker's embedded DNS: a static address for Postgres and a plain resolv.conf |
docker/workload/tenant.compose.yml is the working shape.
Not in the box
| Missing | Instead |
|---|---|
| Multi-tenant catalog | None |
| A managed stack | Hosted subgraphs and webhooks run on api.secondlayer.tools with an account key |
| Archive publisher | A separate image |
| Pay-per-call metering | The x402 rail was removed |
Mainnet full history is about 500 GB.
| Setup | RAM | Disk |
|---|---|---|
| App services, external node, mainnet | 4 GB | 600 GB |
| App services, external node, testnet/devnet | 4 GB | 80 GB |
| Bundled node, mainnet (floor) | 96 GB | 2.5 TB |
| Bundled node, mainnet (recommended) | 128 GB | 3 TB NVMe |
| Sizing fact | Detail |
|---|---|
| Growth | About 3 GB per 100k blocks for core datasets, 6 GB with a broad decoder set; averages over history much denser at the tip |
| Bundled node | Stacks chainstate alone measures 1.3 TB, and bitcoind is additional |
| Postgres | max_connections of 50 or more |
POSTGRES_SHARED_BUFFERS | Compose sets 1 GB; for a full-chain sync give it about 25% of RAM, since at the stock 128 MB inserts wait on index reads |
| Runes | Adds RAM and disk: Runes |
Floors enforced at start
The runtime refuses to boot below the RAM, disk and max_connections floors.
| Fact | Detail |
|---|---|
| Disk | Total size, not free |
| Unmeasurable dimension | Skipped |
SECONDLAYER_ALLOW_UNDERSIZED | Downgrades the refusal to a warning, so an undersized box can come back after an upgrade |
Back up first, then move to a newer release.
Backup
One bundle: the index plus the keys, encrypted under your passphrase.
| Flag or env | Effect |
|---|---|
--passphrase or SECONDLAYER_BACKUP_PASSPHRASE | The passphrase the keys are encrypted under |
--no-secrets | Writes a data-only bundle |
--force (restore) | Lets a restore write over a database that already holds chain data |
| Restore guarantee | Detail |
|---|---|
| Key mismatch | Refuses before writing if the instance's secrets key isn't the one the data was encrypted with |
| All or nothing | pg_restore runs in one transaction and stops on the first error, so a full disk leaves the target as it was. The tradeoff is no parallel restore |
| Scope check | After the load, the canonical block range is compared with the manifest; a dump that stops short exits 1 |
| Digest | Streamed, so a multi-gigabyte bundle hashes without loading into memory |
| Bundle contents | Detail |
|---|---|
| Keys | INSTANCE_TOKEN, SECONDLAYER_SECRETS_KEY, STREAMS_SIGNING_PRIVATE_KEY |
| After restore | Confirm with verify all |
| Storage | Apart from Postgres backups: the archive rebuilds the index, not the keys |
Upgrade
Move from commit X to Y. Read the changelog after X first.
| Keep | Why |
|---|---|
postgres_data volume | chain, subgraph tables, webhook rows |
subgraphs_data volume | compiled handlers |
secondlayer_data volume | the generated secrets key, when SECONDLAYER_SECRETS_KEY is unset |
SECONDLAYER_SECRETS_KEY | decrypts webhook signing secrets in Postgres |
INSTANCE_TOKEN | callers; required at boot |
STREAMS_SIGNING_PRIVATE_KEY, SECONDLAYER_WEBHOOK_SIGNING_PRIVATE_KEY | platform signature on deliveries |
POSTGRES_PASSWORD | the volume already uses it |
| Never | Why |
|---|---|
docker compose down -v | Deletes postgres_data |
Losing SECONDLAYER_SECRETS_KEY | Every signing secret becomes unreadable: rotate each webhook |
| Mainnet dump | Takes hours: snapshot the volume if you can |
| CLI version | A separate npm version from the images |
secondlayer init --force | Mints a new secrets key, leaving webhook secrets unreadable |
| Bootstrap as an upgrade | It refuses a database that already has blocks |
Migrations run at boot, forward-only.
To roll back, pin X again: the schema stays migrated.
Edit on GitHub