/substreams-preflight:substreams-sinkfrom substreams-preflight
Expert guidance for building custom Substreams sinks and data pipelines in Go, JavaScript, Python, or Rust. Covers gRPC streaming, cursor persistence, and chain reorg handling.
Ships in substreams-preflight. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Expert assistant for consuming Substreams data - building production-grade sinks and data pipelines.
| User goal | Skill |
|---|---|
| Custom app sink (Go / JS / Python / Rust SDK consumer) | this skill (substreams-sink) |
SQL module (db_out / DatabaseChanges / From-proto) | substreams-sql |
| Run the sink yourself (CLI, schema, ops on your infra) | substreams-sink-deploy-local |
| StreamingFast hosts the sink (Portal HostedService) | substreams-hosted-sink (+ thegraph-market-api) |
| graph_out / EntityChanges module (produce entities) | this skill (inline proto) + substreams-dev |
Prefer existing sink CLIs (the built-in SQL sink — substreams sink postgres / substreams sink clickhouse —, files, kv) over a hand-rolled consumer when they already cover the destination.
A Substreams sink is an application that:
Note: Before building a custom sink, consider using existing solutions:
- Built-in SQL sink (
substreams sink postgres/substreams sink clickhouse) - For PostgreSQL and ClickHouse. Handles cursor management, reorgs, batching, and schema management out of the box. Install viabrew install streamingfast/tap/substreamsor download binaries. For full CLI ops, loadsubstreams-sink-deploy-local.- substreams-sink-kv - For key-value stores.
- substreams-sink-files - For file-based outputs (JSON, CSV, Parquet).
Important: Both
substreams-sink-postgresand the standalonesubstreams-sink-sqlbinary are deprecated names. The SQL sink now lives in thesubstreamsCLI (substreams sink postgres/substreams sink clickhouse), which supports both PostgreSQL and ClickHouse.The examples in this guide use database code for illustration purposes. For production SQL database sinks, the built-in SQL sink is highly recommended as it solves cursor persistence, reorg handling, batching, and many edge cases already.
mainnet.eth.streamingfast.io:443)All Substreams endpoints require authentication. Get your API key from The Graph Market - sign up at thegraph.market/auth/signup.
CLI Authentication (Recommended):
substreams auth # Interactive authentication, stores token locally
Quick Token Generation: Visit thegraph.market/auth/substreams-devenv to generate a JWT token from your API key directly in the browser.
Environment Variables (Alternative):
# Set API key (recommended)
export SUBSTREAMS_API_KEY=<your-api-key>
# Or set bearer token directly
export SUBSTREAMS_API_TOKEN=<your-jwt-token>
The substreams auth command handles token exchange and local storage automatically.
Note — this is data-plane auth, not Portal auth.
SUBSTREAMS_API_KEY/SUBSTREAMS_API_TOKENauthenticate the sink against the streaming data endpoints. They are separate from the StreamingFast Portal admin API (billing, usage, hosted deployments), which thethegraph-market-apiskill covers. A Portal device-code/Bearer token from that skill is scoped to Portal routes only and will not authenticate a sink — use thesubstreams authflow here.
Before writing a graph_out or db_out module, choose the correct output proto. These are different sink contracts; mixing them up produces a buildable-but-wrong pipeline.
| You want to write to... | Output proto type | Crate / proto package | When to use |
|---|---|---|---|
| The Graph (subgraph entities) | sf.substreams.sink.entity.v1.EntityChanges | substreams-entity-change (BROKEN — see below) | graph_out modules feeding a Graph Node, hosted subgraph, or substreams-sink-subgraph |
| Postgres / ClickHouse / SQL DB | sf.substreams.sink.database.v1.DatabaseChanges | substreams-database-change = "4" | db_out modules feeding substreams sink postgres/clickhouse or hosted SQL sink |
| Custom sink (Go/Rust consumer) | Your own proto type | n/a | Bespoke consumers reading raw module output |
Rule: never use DatabaseChanges for graph-out, never use EntityChanges for SQL sinks. They are not interchangeable. Acceptance tests in eval corpus auto-zero on wrong proto type.
Blocker — read before adding
substreams-entity-change.
Do not rely on the substreams-entity-change crate for modern substreams = "0.7" pipelines:
prost = "0.11" / substreams = "0.5" → prost trait conflicts with the current toolchain.prost ^0.13 but still depends on substreams ^0.6, so it does not drop cleanly into a 0.7 tree.No version constraint fully fixes this for 0.7 today. Prefer inlining the proto (below). Re-check crates.io before changing this advice.
New projects: prefer SQL (
db_out+substreams-sql/ the built-in SQL sink) overgraph_outunless you specifically need Graph Node / EntityChanges.
Define sf.substreams.sink.entity.v1 types inline. This proto is for graph-out (The Graph) only. Do NOT use it for SQL sinks — that's a different proto package (sf.substreams.sink.database.v1).
1. Add the proto definition (proto/entity.proto):
syntax = "proto3";
package sf.substreams.sink.entity.v1;
message EntityChanges {
repeated EntityChange entity_changes = 5; // NOT 1 — consumers read field 5
}
message EntityChange {
string entity = 1;
string id = 2;
// Deprecated, this is not used within `graph-node`.
uint64 ordinal = 3;
enum Operation {
OPERATION_UNSPECIFIED = 0;
OPERATION_CREATE = 1;
OPERATION_UPDATE = 2;
OPERATION_DELETE = 3;
OPERATION_FINAL = 4;
}
Operation operation = 4;
repeated Field fields = 5;
}
message Value {
oneof typed {
int32 int32 = 1;
string bigdecimal = 2;
string bigint = 3;
string string = 4;
string bytes = 5; // hex string, NOT proto `bytes`
bool bool = 6;
int64 timestamp = 7;
Array array = 10;
}
}
message Array {
repeated Value value = 1;
}
message Field {
string name = 1;
optional Value new_value = 3;
// Deprecated, this is not used within `graph-node`.
optional Value old_value = 5;
}
Field numbers are load-bearing.
entity_changesis 5, not 1;old_valueis 5, not 2;Value.bytesis astring(hex), not protobytes. Getting any of these wrong produces a module that builds, streams, and silently writes zero entities — the consumer decodes a field number you never wrote.substreams runwill still look correct, because it decodes with your own package's descriptor; the mismatch only surfaces against a real Graph Node.
Wire compatibility: Both the package name AND the exact message/field definitions (numbers + types) must match.
package sf.substreams.sink.entity.v1;is required — do NOT change it tosf.substreams.sink.database.v1(that's the SQL sink). The block above is a verbatim copy of the canonical source; re-copy from there rather than retyping, and do not simplify field types or renumber fields.
2. Reference in manifest (substreams.yaml):
protobuf:
files:
- proto/entity.proto
# ... other protos
modules:
- name: graph_out
kind: map
inputs:
- map: map_events
output:
type: proto:sf.substreams.sink.entity.v1.EntityChanges # ← graph-out
# NOT: proto:sf.substreams.sink.database.v1.DatabaseChanges (that's SQL)
3. Use the generated type in Rust:
use crate::pb::sf::substreams::sink::entity::v1::{EntityChange, EntityChanges, Field, Value};
use crate::pb::sf::substreams::sink::entity::v1::entity_change::Operation;
use crate::pb::sf::substreams::sink::entity::v1::value::Typed;
// `Field.new_value` and `old_value` are `Value` messages, so the prost-generated
// Rust type is `Option<Value>` — you cannot assign a raw String/Vec<u8>/u64 directly.
// Wrap each scalar in the appropriate `Typed::*` oneof variant. Helpers below
// cover the common cases; add `Typed::Bool`, `Typed::Int32`, `Typed::Bigdecimal`,
// `Typed::Timestamp` (i64), `Typed::Array` as needed for your schema.
// Note `Typed::Bytes` takes a `String` (hex), not a `Vec<u8>` — see the proto above.
fn val_string(s: impl Into<String>) -> Option<Value> {
Some(Value { typed: Some(Typed::String(s.into())) })
}
fn val_bigint(decimal_str: impl Into<String>) -> Option<Value> {
// BigInt is wire-encoded as its decimal string, e.g. "12345"
Some(Value { typed: Some(Typed::Bigint(decimal_str.into())) })
}
#[substreams::handlers::map]
pub fn graph_out(events: Events) -> Result<EntityChanges, substreams::errors::Error> {
let mut changes = EntityChanges::default();
for mint in events.mints {
changes.entity_changes.push(EntityChange {
entity: "NftMint".to_string(),
id: format!("{}-{}", mint.tx_hash, mint.log_index),
ordinal: mint.ordinal,
operation: Operation::Create as i32,
fields: vec![
// tokenId: uint256 → BigInt-as-decimal-string
Field { name: "tokenId".to_string(), old_value: None, new_value: val_bigint(mint.token_id.clone()) },
// address / hash fields: keep as hex strings for Graph Node consumption
Field { name: "to".to_string(), old_value: None, new_value: val_string(mint.to.clone()) },
Field { name: "txHash".to_string(), old_value: None, new_value: val_string(mint.tx_hash.clone()) },
],
});
}
Ok(changes)
}
Do NOT add substreams-entity-change to Cargo.toml when using this workaround. The generated proto code is sufficient; adding the crate triggers the prost conflict.
For SQL sinks (Postgres / ClickHouse /
db_out), usesubstreams-database-change = "4"— seesubstreams-sql/SKILL.md. Those crates ARE compatible with the current toolchain. Do NOT inlineDatabaseChangesproto and call your modulegraph_out— that mixes sink types and the run will fail (or worse, succeed silently with garbage data).
| Language | Recommendation | Best For |
|---|---|---|
| Go | Official SDK (Recommended) | Production sinks, StreamingFast sinks |
| JavaScript | Official SDK | Web apps, Node.js services |
| Python | Reference implementation | Prototyping, data analysis |
| Rust | Reference implementation | High-performance custom sinks |
Import path is github.com/streamingfast/substreams/sink (package lives in the main Substreams module; the old standalone github.com/streamingfast/substreams-sink module is deprecated).
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
. "github.com/streamingfast/cli"
"github.com/streamingfast/logging"
pbsubstreamsrpc "github.com/streamingfast/substreams/pb/sf/substreams/rpc/v2"
"github.com/streamingfast/substreams/sink"
)
var expectedOutputModuleType = "sf.substreams.sink.database.v1.DatabaseChanges" // adjust to your module
var zlog, tracer = logging.RootLogger("my-sink", "github.com/you/my-sink")
func main() {
logging.InstantiateLoggers()
Run("my-sink", "Custom Substreams sink",
Command(sinkRunE,
"sink [<manifest> [<output_module>]]",
"Run the sink",
RangeArgs(0, 2),
Flags(func(flags *pflag.FlagSet) { sink.AddFlagsToSet(flags) }),
),
OnCommandErrorLogAndExit(zlog),
)
}
func sinkRunE(cmd *cobra.Command, args []string) error {
manifestPath := "substreams.yaml"
outputModule := sink.InferOutputModuleFromPackage
if len(args) > 0 {
manifestPath = args[0]
}
if len(args) > 1 {
outputModule = args[1]
}
sinker, err := sink.NewFromViper(
cmd,
expectedOutputModuleType,
manifestPath,
outputModule,
"my-sink/1.0.0", // userAgent
zlog,
tracer,
)
if err != nil {
return fmt.Errorf("create sinker: %w", err)
}
// Load persisted cursor on restart; blank = start from module initialBlock / flags
cursor := sink.NewBlankCursor()
sinker.Run(context.Background(), cursor, sink.NewSinkerHandlers(
handleBlockScopedData,
handleBlockUndoSignal,
))
return nil
}
func handleBlockScopedData(ctx context.Context, data *pbsubstreamsrpc.BlockScopedData, isLive *bool, cursor *sink.Cursor) error {
// Process data.Output.MapOutput, then persist cursor AFTER success
_ = cursor
return nil
}
func handleBlockUndoSignal(ctx context.Context, undoSignal *pbsubstreamsrpc.BlockUndoSignal, cursor *sink.Cursor) error {
// Rewind data to undoSignal.LastValidBlock, persist cursor
_ = cursor
return nil
}
CLI flags (via AddFlagsToSet): -e/--endpoint, -s/--start-block, -t/--stop-block, --final-blocks-only, -p/--params, auth env vars, etc.
See references/go-sink.md for complete guide.
Prefer createGrpcTransport (faster for large streams) over Connect HTTP. Requires "type": "module" in package.json. Pin your deps — the unpinned install fails to resolve:
npm install @substreams/[email protected] @connectrpc/[email protected] @connectrpc/[email protected]
@connectrpc/connect 2.x peers @bufbuild/protobuf ^2, but @substreams/core still peers ^1 — installing both unpinned gives ERESOLVE.
Official pattern:
import {
createRequest,
streamBlocks,
createAuthInterceptor,
createRegistry,
fetchSubstream,
} from "@substreams/core";
import { createGrpcTransport } from "@connectrpc/connect-node";
const TOKEN = process.env.SUBSTREAMS_API_TOKEN;
const ENDPOINT = "https://mainnet.eth.streamingfast.io:443";
const SPKG = "https://spkg.io/streamingfast/substreams-eth-block-meta-v0.4.3.spkg";
const MODULE = "db_out";
const pkg = await fetchSubstream(SPKG);
const registry = createRegistry(pkg);
const transport = createGrpcTransport({
baseUrl: ENDPOINT,
interceptors: [createAuthInterceptor(TOKEN)],
jsonOptions: { typeRegistry: registry },
});
const request = createRequest({
substreamPackage: pkg,
outputModule: MODULE,
productionMode: true,
startBlockNum: 17000000n, // number | bigint — a string is a TS type error
stopBlockNum: "+1000",
startCursor: (await getCursor()) ?? undefined,
});
let blockCount = 0;
for await (const response of streamBlocks(transport, request)) {
const msg = response.message;
if (msg.case === "blockScopedData") {
// Process msg.value.output, then persist msg.value.cursor AFTER success
await writeCursor(msg.value.cursor);
blockCount++;
} else if (msg.case === "blockUndoSignal") {
// Rewind to msg.value.lastValidBlock.number, persist lastValidCursor
await writeCursor(msg.value.lastValidCursor);
} else if (msg.case === "fatalError") {
// Server-side module failure — without this branch it looks like success
throw new Error(`fatal error in module ${msg.value.module}: ${msg.value.reason}`);
}
}
// A clean exit with zero blocks is NOT success — on createGrpcTransport a bad
// token ends the stream silently instead of throwing Unauthenticated.
if (blockCount === 0) throw new Error("stream ended with 0 blocks — check your token");
Wrap the stream loop in retry/backoff for normal disconnects. See references/javascript-sink.md for complete guide.
import grpc
from sf.substreams.rpc.v2 import service_pb2, service_pb2_grpc
creds = grpc.ssl_channel_credentials()
with grpc.secure_channel("mainnet.eth.streamingfast.io:443", creds) as channel:
stub = service_pb2_grpc.StreamStub(channel)
metadata = [("authorization", f"Bearer {token}")]
request = service_pb2.Request(
start_block_num=17000000,
stop_block_num=17001000,
modules=package.modules,
output_module="map_events",
production_mode=True,
)
for response in stub.Blocks(request, metadata=metadata):
if response.WhichOneof("message") == "block_scoped_data":
# Process block data
pass
elif response.WhichOneof("message") == "block_undo_signal":
# Handle reorg
pass
See references/python-sink.md for complete guide.
Reference implementation (no official SDK). Stream wrapper shape from the examples:
use futures03::StreamExt; // brings `stream.next()` into scope
use substreams_stream::{BlockResponse, SubstreamsStream};
// `substreams_stream` / `substreams` here are LOCAL modules vendored from the
// examples repo (`mod substreams_stream;`) — there is no `substreams-stream`
// crate, and do NOT `cargo add substreams` (that's the WASM module crate).
// SubstreamsStream::new(endpoint, cursor, package, output_module, start_block, stop_block)
let mut stream = SubstreamsStream::new(
endpoint, // Arc<SubstreamsEndpoint>
cursor, // Option<String>
Some(package), // Option<Package>
"map_events".to_string(), // output module name
start_block, // i64
stop_block, // u64 (0 = live)
);
while let Some(response) = stream.next().await {
match response? {
BlockResponse::New(data) => {
// Process block data, then persist cursor AFTER success
persist_cursor(&data.cursor)?;
}
BlockResponse::Undo(signal) => {
// Handle reorg, then persist last_valid_cursor
rewind_to_block(signal.last_valid_block.as_ref())?;
persist_cursor(&signal.last_valid_cursor)?;
}
}
}
See references/rust-sink.md for complete guide.
The cursor is the most critical piece of sink development.
RULE #1: Persist cursor AFTER successful processing, never before.
RULE #2: On restart, load persisted cursor and resume from there.
RULE #3: Blank/empty cursor means start from the beginning.
The cursor is an opaque string that encodes:
Cursor persistence patterns:
| Storage | Use Case | Example |
|---|---|---|
| File | Development, single instance | cursor.txt |
| Database | Production, multi-instance | Cursors table with module key |
| Redis | High availability | Key-value with TTL |
See references/cursor-reorg.md for detailed patterns.
When a chain reorganizes, you receive a BlockUndoSignal:
BlockUndoSignal {
last_valid_block: BlockRef { number: 17000100, id: "0xabc..." }
last_valid_cursor: "opaque-cursor-string"
}
Required actions:
last_valid_block.numberlast_valid_cursorFinal blocks only mode (recommended for most sinks):
final_blocks_only: true in request (--final-blocks-only on the CLI, finalBlocksOnly: true in JS)Fatal errors (do not retry):
Unauthenticated - Invalid or expired tokenInvalidArgument - Bad request parametersRetryable errors (implement exponential backoff):
Unavailable - Server temporarily unavailableResourceExhausted - Rate limitedInternal - usually a transient stream reset (RST_STREAM) on long-lived streams, not a server bug — retry itThe official Rust reference implementation treats only
Unauthenticatedas fatal and reconnects on everything else. Do not classifyInternalas fatal: it is the standard gRPC surfacing of a routine disconnect, and treating it as fatal kills sinks on exactly the reconnect they should ride out.
A clean stream end is not proof of success. Handle the fatal_error message of the response oneof explicitly — a server-side module panic arrives there, and a switch that only covers block_scoped_data / block_undo_signal will fall through and report success. Likewise, if you asked for a range and processed zero blocks, treat that as an error rather than a clean exit.
Exponential backoff pattern:
Base delay: 500ms
Max delay: 45-90 seconds
Jitter: Add random 0-100ms
| Feature | Production Mode | Development Mode |
|---|---|---|
| Parallel execution | Yes | No |
| Output | Single module only | All modules |
| Performance | Optimized | Debug-friendly |
| Use case | Sinks | Testing, debugging |
Always use production mode for sinks:
| Language | How |
|---|---|
| Python | production_mode=True — field on service_pb2.Request |
| JavaScript | productionMode: true — option to createRequest |
| Rust | production_mode: true — field on the v3 Request |
| Go | already the default — see below |
In Go, production mode is already the default with sink.NewFromViper — just do not pass --development-mode. (There is no sink.WithProductionMode(); the sink.With* options do not include one, and all of them are deprecated in favour of configuring SinkerConfig via sink.NewFromConfig.)
| Network | Endpoint |
|---|---|
| Ethereum Mainnet | mainnet.eth.streamingfast.io:443 |
| Ethereum Sepolia | sepolia.eth.streamingfast.io:443 |
| Polygon | polygon.streamingfast.io:443 |
| Arbitrum One | arb-one.streamingfast.io:443 |
| Optimism | mainnet.optimism.streamingfast.io:443 |
| Base Mainnet | base-mainnet.streamingfast.io:443 |
| BNB | bnb.streamingfast.io:443 |
| Solana Mainnet-Beta | mainnet.sol.streamingfast.io:443 |
| NEAR Mainnet | mainnet.near.streamingfast.io:443 |
Full list: docs.substreams.dev/reference-material/chains-and-endpoints (raw data: TheGraphNetworksRegistry.json)
# Explicit range
--start-block 17000000 --stop-block 17001000
# From manifest initialBlock — omit --start-block entirely (empty = use initialBlock).
# There is no `:` syntax; `--start-block :` fails with "start block is invalid".
--stop-block 17001000
# Relative stop (process 1000 blocks)
--start-block 17000000 --stop-block +1000
# To chain head (live streaming)
--start-block 17000000 --stop-block 0
Pass runtime parameters to modules:
# Single parameter
-p "map_events=0xa0b86a33e6..."
# Multiple parameters
-p "map_events=0xa0b86a33..." -p "filter_module=type:transfer"
# JSON parameters
-p 'map_events={"contracts":["0x123","0x456"],"min_value":1000}'
Generate language bindings from .spkg files:
# Install buf
go install github.com/bufbuild/buf/cmd/buf@latest
# Generate from local .spkg
buf generate --exclude-path="google" ./my-substreams.spkg#format=bin
# Generate from URL
buf generate "https://spkg.io/streamingfast/substreams-eth-block-meta-v0.4.3.spkg#format=binpb"
# Generate from buf registry
buf generate buf.build/streamingfast/substreams --include-imports
"Unauthenticated" error:
"Connection refused" error:
No data received:
initialBlock in manifest is before your start blockcreateGrpcTransport usually means a bad token — the trailers-only Unauthenticated is swallowed. Re-run with createConnectTransport to surface the real ConnectError.EntityChanges proto field numbers against the canonical proto — entity_changes must be 5. substreams run will look fine either way.Slow processing:
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Go, JavaScript, Python, Rust
Other plugins' skills for the same job
npx claudepluginhub xuechunhhh/substreams-preflight --plugin substreams-preflight11 skills in this plugin
/substreams-bitcoinExpert knowledge for developing Bitcoin Substreams. Use when working with Bitcoin blockchain data, UTXO model, transaction parsing, address extraction, or Esplora API compatibility./substreams-convertExpert knowledge for converting existing blockchain indexing projects into Substreams. Use when migrating a subgraph (The Graph) or a Solana program/contract into a Substreams module pipeline./substreams-devCross-cutting Substreams development — manifests, module graphs (map/store/index), protobuf, performance, debugging. For Ethereum/EVM contract decoding use substreams-ethereum; for Solana programs use substreams-solana; for SQL sinks use substreams-sql.