Skip to content
Alchemy Logo

StreamBlocks

Beta

HyperCore APIs are in beta. Message fields, limits, and behavior may change, including breaking changes.

streams.v1.BlockStream/StreamBlocks is a server-streaming method. It sends one message per HyperCore block, in ascending block order, until you cancel the call or the server ends it with a status.

See the overview for the endpoint, authentication, and the full blocks.proto.

message StreamBlocksRequest {
  optional uint64 start_block = 1;
}
FieldTypeRequiredDescription
start_blockuint64NoBlock number to start from, inclusive. Omit it to receive only new blocks.

RequestResult
start_block omittedThe stream starts with the next new block.
start_block inside the retained windowThe server replays from start_block through the latest block, then continues with new blocks. There are no gaps or duplicates between the replayed and new blocks.
start_block older than the retained windowThe call fails with NOT_FOUND. No messages are sent.
start_block newer than the latest blockThe stream starts at the latest block, which can be lower than start_block. Skip blocks you don't need.

Replay only covers a short window of recent blocks (a few seconds of mainnet), so it is for reconnecting clients rather than history.

A start_block the server can't replay as a continuous range returns NOT_FOUND, even if the block is recent. This also happens when the window has no blocks yet.

The stream is for live data and short reconnects. It doesn't serve historical ranges.

message BlockMessage {
  uint64 block_number = 1;
  bytes data = 2;
}
FieldTypeDescription
block_numberuint64Block height. Matches the top-level height in data. It is not the consensus round (abci_block.round).
databytesThe block as UTF-8 JSON, passed through unchanged. Parse it with a JSON parser.

data decodes to a JSON object with these top-level keys:

KeyContents
abci_blockBlock header and contents: time, round, parent_round, proposer, hardfork, and signed_action_bundles.
respsResponses to the block's actions.
heightBlock height, equal to block_number.

The block format comes from the Hyperliquid node, not from this API, and can change when the node changes. Ignore keys you don't use.

  • Messages arrive in ascending block_number order, with no duplicates within a stream.
  • Block numbers are normally consecutive. If a number is skipped, the skipped block isn't available on the stream, and a resume into that range returns NOT_FOUND.

Block sizes vary widely. Most are hundreds of kilobytes, and some are several megabytes. Set your client's maximum receive message size well above the common 4 MB default.

Every error ends the stream with a gRPC status. There are no error messages inside the stream. Branch on the status code; message text may change.

CodeWhenMessageWhat to do
UNAUTHENTICATEDAt call startMust be authenticated!Send x-token: <API_KEY> metadata. Don't retry until the key is fixed.
NOT_FOUNDAt call startstart_block is older than the retained block windowThe missed blocks are no longer retained. Call again without start_block to resume live.
ABORTEDDuring the streamblock stream fell behind live blocksYour client read too slowly. Reconnect from your last block plus one, and read faster.
UNAVAILABLEAt call start or during the streamFor example block feed not ready, block stream connection limit reached, or server drainingTemporary. Reconnect with backoff from your last block plus one.
DEADLINE_EXCEEDEDWhen your call deadline expiresSet by your clientDon't set a deadline on this call. Reconnect.

The server buffers a small number of blocks for each stream. If your client stops reading and that buffer fills, the server ends the stream with ABORTED.

To avoid this, read messages continuously and process them off the receive path, for example through a bounded queue. Because the retained window is short, a reconnect after a long stall returns NOT_FOUND.

  1. Record block_number after you process each message.
  2. When the stream ends with ABORTED, UNAVAILABLE, or a network error, call again with start_block set to your last block_number plus one. Use exponential backoff with jitter.
  3. If the call returns NOT_FOUND, the blocks you missed are no longer retained. Fetch them from another source if you need them, then call without start_block to resume live.
  4. On UNAUTHENTICATED, stop and fix the API key.

start_block is inclusive. If you resume from your last block_number instead of the next one, you receive that block again; drop it.

The server doesn't support reflection, so pass blocks.proto from the overview. Don't set -max-time; it ends the stream when it expires.

grpcurl \
  -proto blocks.proto \
  -H "x-token: $ALCHEMY_API_KEY" \
  -max-msg-sz 67108864 \
  -d '{}' \
  hypercore-mainnet.g.alchemy.com:443 \
  streams.v1.BlockStream/StreamBlocks

To resume, pass a block number:

grpcurl \
  -proto blocks.proto \
  -H "x-token: $ALCHEMY_API_KEY" \
  -max-msg-sz 67108864 \
  -d '{"start_block": 1194130000}' \
  hypercore-mainnet.g.alchemy.com:443 \
  streams.v1.BlockStream/StreamBlocks

grpcurl prints each message as JSON, with blockNumber as a string and data as base64:

{
  "blockNumber": "1194129990",
  "data": "eyJhYmNpX2Jsb2NrIjp7InRpbWUiOiIyMDI2LTEwLTIzVDA4OjMxOjMyLjc4MDg2MDc2OSIs..."
}

Uses @grpc/grpc-js and @grpc/proto-loader, with blocks.proto in the working directory. The client resumes from the next block after a disconnect.

stream-blocks.js
const grpc = require("@grpc/grpc-js");
const protoLoader = require("@grpc/proto-loader");
 
const definition = protoLoader.loadSync("blocks.proto", { longs: String });
const { BlockStream } = grpc.loadPackageDefinition(definition).streams.v1;
 
const client = new BlockStream(
  "hypercore-mainnet.g.alchemy.com:443",
  grpc.credentials.createSsl(),
  { "grpc.max_receive_message_length": 64 * 1024 * 1024 },
);
 
let lastBlock = null; // block_number of the last processed block, as a string
 
function connect() {
  const metadata = new grpc.Metadata();
  metadata.set("x-token", process.env.ALCHEMY_API_KEY);
 
  const request =
    lastBlock === null ? {} : { startBlock: (BigInt(lastBlock) + 1n).toString() };
  const call = client.StreamBlocks(request, metadata);
 
  call.on("data", (message) => {
    const block = JSON.parse(message.data.toString("utf8"));
    console.log(message.blockNumber, block.abci_block.time);
    lastBlock = message.blockNumber;
  });
 
  // "status" fires exactly once when the call ends, whether it failed or the
  // server completed it normally, so reconnect from here.
  call.on("error", () => {}); // failures are handled in "status" below
  call.on("status", (status) => {
    if (status.code !== grpc.status.OK) {
      console.error(grpc.status[status.code], status.details);
    }
    if (status.code === grpc.status.UNAUTHENTICATED) return; // fix the API key
    if (status.code === grpc.status.NOT_FOUND) lastBlock = null; // resume live
    setTimeout(connect, 1000); // use exponential backoff with jitter in production
  });
}
 
connect();
Was this page helpful?