Ethereum API: eth_getTransactionByHash Explained

·

Ethereum's eth_getTransactionByHash API method retrieves detailed information about a specific transaction using its unique hash. This JSON-RPC call returns an object containing sender/recipient addresses, gas metrics, transaction value, and other blockchain metadata—essential for auditing, analytics, and transaction tracking.

👉 Master Ethereum APIs with OKX's developer resources


Core Parameters and Response Structure

Required Parameter

Transaction Response Object

FieldDescription
blockHashBlock containing the transaction (null if pending)
blockNumberBlock height (null if pending)
fromSender's Ethereum address
gasGas units provided by sender
maxFeePerGasMax fee per gas unit (EIP-1559)
hashTransaction's unique identifier
inputSmart contract call data (if applicable)
valueTransferred ETH in Wei

Practical Implementation

Ethers.js Example

const ethers = require("ethers");
const provider = new ethers.JsonRpcProvider("YOUR_RPC_URL");

async function fetchTransaction(txHash) {
  const tx = await provider.send("eth_getTransactionByHash", [txHash]);
  
  console.log(`Sender: ${tx.from}`);
  console.log(`Block: ${tx.blockNumber || 'Pending'}`);
  console.log(`Value: ${ethers.formatEther(tx.value)} ETH`);
}

fetchTransaction("0xb528d7...6099e");

Key Use Cases

  1. Transaction Auditing
    Verify execution details like gas consumption and block inclusion
  2. Wallet Activity Monitoring
    Track outgoing/incoming transfers programmatically
  3. Block Explorer Backends
    Power transaction detail pages in blockchain explorers

FAQ

Q: Why does my transaction return null fields?
A: blockHash, blockNumber, and transactionIndex remain null for pending transactions until mined.

Q: How do I convert Wei to ETH?
A: Use libraries like Ethers.js: ethers.formatEther(valueInWei)

Q: What's the difference between gasPrice and maxFeePerGas?
A: gasPrice is for legacy transactions, while maxFeePerGas applies to EIP-1559 transactions.

👉 Explore Ethereum development tools on OKX


Advanced Considerations