How to Check the Status of a Transaction using its Hash

Learn how to check the status of a transaction using the transaction hash

Don’t have an API key?

Start using this API in your app today.

📘

API Endpoint

This tutorial uses the getTransactionReceipt endpoint.

Introduction

The transaction hash is a unique identifier that is assigned to every transaction on the blockchain network. In this article, we will explain how you can use Alchemy's getTransactionReceipt API to check the status of a transaction using its transaction hash.

Creating the Status Checker Script


Step 1: Install Node and npm

In case you haven't already, install node and npm on your local machine.

Make sure that node is at least v14 or higher by typing the following in your terminal:

node -v

Step 2: Create an Alchemy app


In case you haven't already, sign up for a free Alchemy account.

2880

Alchemy's account dashboard where developers can create a new app on the Ethereum blockchain.

Next, navigate to the Alchemy Dashboard and create a new app.

Make sure you set the chain to Ethereum and the network to Mainnet. Once the app is created, click on your app's View Key button on the dashboard.

Take note of the HTTP URL.

The URL will be in this form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx

You will need this later.


Step 3: Create a node project

Let's now create an empty repository and install all node dependencies.

Run the following commands in order to create your node project and install Alchemy SDK.

mkdir transaction-status-checker && cd transaction-status-checker
npm init -y
npm install --save alchemy-sdk
touch main.js

This will create a repository named transaction-status-checker that holds all your files and dependencies.

Next, open this repo in your favorite code editor.

We will write all our code in the main.js file.

Step 4: Check the Transaction Status

To check the status of the transaction using its transaction hash, we will use the getTransactionReceipt method.

  • This method accepts a transaction hash and returns the transaction receipt, which is an object that contains the details about the transaction. The returned object has a property called status.
  • If the value of the status property is 1, it means that the transaction was successful, on the other hand, a value of 0 signifies that the transaction failed.
  • If no object (transaction receipt) is returned from the getTransactionReceipt method, it means either the transaction is pending or the hash provided is an invalid/unknown transaction hash.

Add the following code to the main.js file:

// importing the Alchemy SDK
const { Network, Alchemy } = require("alchemy-sdk");

// Optional config object, but defaults to demo api-key and eth-mainnet.
const settings = {
  apiKey: "demo", // Replace with your Alchemy API Key.
  network: Network.ETH_MAINNET, // Replace with your network.
};
const alchemy = new Alchemy(settings);

// Transaction hash for the transaction whose status we want to check
const txHash =
  "0xd488331be3a2f9cdd0f2b351f2b13f5151630aaafd2c2b246f7f3cd7fd0b1dfc";

// Getting the status of the transaction using getTransactionReceipt and logging accordingly
alchemy.core.getTransactionReceipt(txHash).then((tx) => {
  if (!tx) {
    console.log("Pending or Unknown Transaction");
  } else if (tx.status === 1) {
    console.log("Transaction was successful!");
  } else {
    console.log("Transaction failed!");
  }
});

// Status is 1 for success, 0 for failure.

Here's an explanation of what the code is doing:

  • Imports the Alchemy SDK package because we want to use Alchemy's getTransactionReceipt method.
  • Configures the Alchemy SDK with the API Key and sets the network to ETH_MAINNET.
  • Defines the transaction hash for the transaction whose status we want to check.
  • Calls the getTransactionReceipt method of the Alchemy SDK, passing in the transaction hash as an argument.
  • This returns the transaction receipt for the transaction, which contains the status of the transaction.
  • It then, logs the status of the transaction to the console according to the value of the status property. If the tx object returned by getTransactionReceipt is null, it means that the transaction is either pending or unknown. If the value of the status property is 1, it means that the transaction was successful. Otherwise, the transaction failed.

Run the script using:

node main.js

If all goes well, you should see an output with the status of the transaction:

Transaction was successful!

Conclusion

In conclusion, checking the status of a transaction using its hash is a simple process. By following the steps outlined in this article, you can easily track the status of your transaction.


ReadMe