getLatestBlockhash
Returns the latest blockhash
Common use cases
| Transaction Construction Pre-Flight | Get fresh blockhash immediately before building and sending transaction. Pattern: const { blockhash, lastValidBlockHeight } = await rpc.getLatestBlockhash({ commitment: 'confirmed' }) |
| Transaction Expiration Validation | Check current block height against lastValidBlockHeight to determine if transaction can still be submitted. Critical for retry logic and transaction monitoring. |
| Trading Bot Transaction Batching | Retrieve single blockhash to use across multiple transactions in atomic bundle. Ensures all transactions in bundle share same lifetime window. |
| Durable Nonce Alternative | Simple blockhash for short-lived transactions (vs. durable nonce for offline signing). Use case: Server-side transaction construction with immediate submission. |
| Finalized Commitment for Production Safety | Use getLatestBlockhash({ commitment: 'finalized' }) to avoid minority fork issues. Guarantees blockhash from canonical chain, preventing "blockhash not found" errors. |
Parameters
config (object, optional)
Configuration object containing the following fields:
Fields:
commitment(string): The commitment describes how finalized a block is at that point in time. See Configuring State Commitment.minContextSlot(number): The minimum slot that the request can be evaluated at
Request
- cURL
- JavaScript
- Python
curl https://solana-mainnet.api.syndica.io/api-key/YOUR_API_KEY \
-X POST \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getLatestBlockhash",
"params": [
{
"commitment": "processed"
}
]
}'
// Using fetch
const response = await fetch('https://solana-mainnet.api.syndica.io/api-key/YOUR_API_KEY', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "getLatestBlockhash",
"params": [
{
"commitment": "processed"
}
]
})
});
const data = await response.json();
console.log(data);
import requests
import json
url = 'https://solana-mainnet.api.syndica.io/api-key/YOUR_API_KEY'
headers = {'Content-Type': 'application/json'}
data = {
"jsonrpc": "2.0",
"id": 1,
"method": "getLatestBlockhash",
"params": [
{
"commitment": "processed"
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())
Replace mainnet with devnet in the URL to query devnet instead.
Response
{
"jsonrpc": "2.0",
"result": {
"context": {
"slot": 2792
},
"value": {
"blockhash": "EkSnNWid2cvwEVnVx9aBqawnmiCNiDgp3gUdkDPTKN1N",
"lastValidBlockHeight": 3090
}
},
"id": 1
}
Response Fields
blockhash (string)
A Hash as base-58 encoded string
lastValidBlockHeight (u64)
Last block height at which the blockhash will be valid
FAQ and Troubleshooting
Why do I get "Blockhash not found" error when simulating transactions?
Three causes: (1) Used processed/confirmed blockhash during minority fork - use finalized commitment; (2) Blockhash expired (~60 sec lifetime) - retrieve immediately before sending; (3) Network mismatch - verify wallet and RPC are on same network (devnet/mainnet).
What's the difference between lastValidBlockHeight and blockhash expiration?
lastValidBlockHeight is the absolute block height after which the blockhash becomes invalid. The cluster maintains a rolling window of ~150 blocks (~60 seconds) where blockhashes are valid. Compare current block height (from getBlockHeight) to lastValidBlockHeight to check transaction freshness.
When should I use finalized vs confirmed commitment?
Use finalized for production transactions to guarantee blockhash from canonical chain, avoiding minority fork issues. Use confirmed for development/testing when speed matters more than absolute safety. processed is rarely appropriate for getLatestBlockhash.
Why does getLatestBlockhash not work on my localnet?
This method was introduced in Solana v1.9 (August 2022). If running older version (≤v1.8), use deprecated getRecentBlockhash instead. In Agave 2.0+, getRecentBlockhash is removed entirely.
How long is a blockhash valid?
Approximately 60 seconds or ~150 blocks (400ms/block). The exact validity is determined by lastValidBlockHeight - current_block_height. Best practice: retrieve blockhash immediately before transaction construction to maximize valid submission window.
Related Methods
getBlockHeight
Returns current block height, used with lastValidBlockHeight to validate blockhash freshness before submission.
isBlockhashValid
Explicitly checks if a blockhash is still valid for transaction submission, useful for retry logic.
getFeeForMessage
Requires valid blockhash to calculate transaction fees, commonly called after getLatestBlockhash.
sendTransaction
Primary consumer of blockhash - transaction must include recent blockhash from getLatestBlockhash.
External Resources
- Solana Official Documentation
- Solana Cookbook - Working with Transactions - Official patterns for constructing and sending transactions, includes blockhash usage best practices
- Anza Agave GitHub - Transaction Simulation Tests - Reference implementation showing how Solana's own RPC client uses
getLatestBlockhashin transaction construction