Looking up a confirmed Bitcoin transaction with its block hash
An indexer that saves a block hash next to each transaction ID can retrieve the transaction from a Bitcoin Core node that retains the block, even when txindex is disabled.
Try a known transaction from the tip
block=$(bitcoin-cli getbestblockhash)
tx=$(bitcoin-cli getblock "$block" 1 | jq -r '.tx[0]')
bitcoin-cli getrawtransaction "$tx" 1 "$block" | jq '{txid, blockhash, in_active_chain}'
The example selects the coinbase transaction from the sampled tip. Passing the third argument directs Core to that particular block. Verify the returned IDs and in_active_chain. A reorganisation between the first and last command can leave a readable block outside the active chain.
Diagnose a lookup using only the transaction ID
bitcoin-cli getindexinfo txindex
bitcoin-cli getrawtransaction "$tx" 1
Without a block hash, Core can look up a transaction from its mempool or use its transaction index. On a node with no transaction index, the second command can fail for the same confirmed transaction the first example successfully read. An empty object from the filtered getindexinfo call means there is no matching active index. If txindex is present, check synced and best_block_height before using it to serve historical lookups.
For an ingestion pipeline, store both identifiers as soon as the block is read. If a later query names a pruned block, use a node retaining the relevant data. An explicit hash identifies where to look and still requires the block to be available.
bitcoincore.org getrawtransaction · bitcoincore.org getindexinfo