How to Use getBlockHeight
Learn getBlockHeight use cases, code examples, request parameters, response structure, and tips.
The getBlockHeight
RPC method is a simple way to query a Solana node for its current block height. The block height represents the number of blocks that have been processed since the genesis block (slot 0).
This method is useful for quickly understanding how far the chain has progressed or for referencing the latest block height at a specific commitment level.
Common Use Cases
- Monitoring Chain Progression: Periodically call this method to see the chain advancing.
- Getting a Snapshot of Chain Length: Determine the total number of blocks processed up to a certain point (based on commitment).
- Cross-Referencing with Other Data: Use the block height as a reference point when analyzing other on-chain data or events.
Parameters
getBlockHeight
can optionally take a configuration object as its first parameter (or be called with no parameters to use defaults):
config
(object, optional): A configuration object with the following fields:commitment
(string, optional): Specifies the commitment level to use for the query. Defaults tofinalized
.finalized
: The node will query the most recent block confirmed by the supermajority of the cluster as having reached maximum lockout.confirmed
: The node will query the most recent block that has been voted on by a supermajority of the cluster.processed
: The node will query its most recent block. Note that the block may not be complete.
minContextSlot
(number, optional): The minimum slot that the request can be evaluated at. This ensures that the block height returned is from a slot equal to or greater thanminContextSlot
.
If no parameters are provided, the method typically defaults to the finalized
commitment.
Developer Tips
- Commitment Levels Matter: The block height returned can vary based on the
commitment
level specified.finalized
gives the most stable height, whileprocessed
might give a more current but potentially transient height. minContextSlot
for Consistency: UseminContextSlot
if you need to ensure the block height is queried from a state that is at least as recent as a specific slot you are tracking.- Not a Slot Number: Remember that block height is different from the slot number. A slot is a period of time, and not every slot produces a block (these are called skipped slots). Block height is the count of actual blocks produced.
- Basic Chain Health Check: While simple,
getBlockHeight
can be part of a basic chain health or synchronization check for your application or monitoring system.
Response
The result
field of the JSON-RPC response will be a single number:
blockHeight
(number): The current block height (u64) of the node according to the specified commitment level.
Example Response JSON:
This value represents the number of blocks on the longest chain from the genesis block (slot 0) to the current head of the chain, as seen by the queried node.
Example: Fetching the Current Block Height
Let’s fetch the current block height from the Helius Devnet RPC.
Note: Replace YOUR_API_KEY
with your actual Helius API key in the examples below.