in-solana by Supanode

getLatestBlockhash — Solana RPC Method

getLatestBlockhash returns a recent blockhash and the last block height at which that blockhash remains valid. Every Solana transaction requires a recent blockhash to be included in the message — this is the method you call before signing and sending a transaction.

Parameters

NameTypeRequiredDescription
commitmentstringNoprocessed, confirmed, or finalized. Default: finalized.
minContextSlotnumberNoMinimum slot at which the request can be evaluated.

Request

curl -X POST https://api.mainnet-beta.solana.com \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getLatestBlockhash",
    "params": [
      {"commitment": "finalized"}
    ]
  }'

TypeScript (@solana/kit)

import { createSolanaRpc } from "@solana/kit";

const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
const { value } = await rpc
  .getLatestBlockhash({ commitment: "finalized" })
  .send();

console.log(`Blockhash: ${value.blockhash}`);
console.log(`Valid until block height: ${value.lastValidBlockHeight}`);

Response

{
  "jsonrpc": "2.0",
  "result": {
    "context": { "slot": 341200000, "apiVersion": "2.2.4" },
    "value": {
      "blockhash": "EkSnNWid2cvwEVnVx9aBqawnmiCNiDgp3gUdkDPTKN1N",
      "lastValidBlockHeight": 289443150
    }
  },
  "id": 1
}
FieldTypeDescription
blockhashstringA base-58 encoded hash. Used in the transaction message as a recent blockhash.
lastValidBlockHeightu64The block height at which the blockhash expires. If the network passes this height before your transaction lands, the transaction is dropped.

How blockhash expiration works

Every Solana transaction message includes a recent blockhash, which serves two purposes:

  1. Deduplication — prevents the same transaction from being processed twice.
  2. Expiration — a transaction is dropped if the network surpasses lastValidBlockHeight before the transaction is confirmed.

Blockhashes are typically valid for about 60-90 seconds (approximately 150 blocks). After that, the transaction is considered expired and will not be processed.

Common errors

  • Blockhash not found — if you reuse an old blockhash, the transaction fails with “Blockhash not found”. Always fetch a fresh blockhash before signing.
  • Node behind — if minContextSlot exceeds the node’s current slot, the request fails.

Performance

getLatestBlockhash is lightweight and returns quickly. However, avoid calling it far in advance of sending the transaction — the blockhash starts expiring immediately. For the best transaction landing rate, fetch the blockhash and send the transaction as close together as possible.

FAQ

Which commitment level should I use for the blockhash? Use confirmed for most transaction-sending scenarios. It gives you a more recent blockhash (longer validity window) than finalized. Use finalized when you need maximum certainty that the blockhash will not be forked out.

How long is a blockhash valid? Approximately 60-90 seconds, or about 150 blocks. The exact duration depends on the network’s block production rate.

What happens if my transaction uses an expired blockhash? The transaction is silently dropped by the network. It will not appear in any block. You need to rebuild the transaction with a fresh blockhash and resend it.

Can I reuse a blockhash for multiple transactions? Yes, as long as it has not expired. However, two transactions with the same blockhash and identical instructions will be treated as duplicates — only one will be processed.

How do I check if a blockhash is still valid? Use isBlockhashValid to check whether a specific blockhash is still valid without fetching a new one.

Parameters

ParameterTypeRequiredDescriptionDefault
commitmentstringNoCommitment level: processed, confirmed, or finalized.finalized
minContextSlotnumberNoMinimum slot at which the request can be evaluated.—

Updated: