Skip to main content

Introduction

In this tutorial, you’ll learn how to retrieve detailed information about any blockchain transaction using its hash. This is essential for building transaction explorers, tracking payments, or verifying on-chain activity in your application. We’ll use the following Moralis API endpoint:

Prerequisites

Step 1: Set Up Your Project

Create a new directory for your project and initialize it:
mkdir get-transaction && cd get-transaction
npm init -y

Step 2: Create the Script

Create a file called index.js and add the following code:
// index.js
const API_KEY = 'YOUR_API_KEY';
const TX_HASH = '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060';

async function getTransaction() {
  const response = await fetch(
    `https://deep-index.moralis.io/api/v2.2/transaction/${TX_HASH}?chain=eth`,
    {
      headers: {
        'X-API-Key': API_KEY,
      },
    }
  );

  const data = await response.json();
  console.log(JSON.stringify(data, null, 2));
}

getTransaction();
Replace YOUR_API_KEY with your actual Moralis API key.

Step 3: Run the Script

Execute the script to fetch the transaction details:
node index.js

Example Response

{
  "hash": "0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060",
  "nonce": "0",
  "transaction_index": "0",
  "from_address": "0xa1e4380a3b1f749673e270229993ee55f35663b4",
  "from_address_label": null,
  "to_address": "0x5df9b87991262f6ba471f09758cde1c0fc1de734",
  "to_address_label": null,
  "value": "31337000000000000",
  "gas": "21000",
  "gas_price": "20000000000",
  "input": "0x",
  "receipt_cumulative_gas_used": "21000",
  "receipt_gas_used": "21000",
  "receipt_status": "1",
  "block_timestamp": "2015-08-07T03:30:33.000Z",
  "block_number": "46147",
  "block_hash": "0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd",
  "transaction_fee": "0.00042"
}

Understanding the Response

FieldDescription
hashThe transaction hash
from_addressThe sender’s wallet address
to_addressThe recipient’s wallet address
valueAmount transferred in wei
gasGas limit set for the transaction
gas_priceGas price in wei
receipt_statusTransaction status (1 = success, 0 = failed)
block_timestampWhen the transaction was mined
block_numberBlock number containing this transaction
transaction_feeTotal fee paid in native token

Next Steps