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. Get started for free

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:

shell
$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.

$mkdir transaction-status-checker && cd transaction-status-checker
>npm init -y
>npm install --save viem
>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:

1import { createPublicClient, http } from 'viem'
2import { mainnet } from 'viem/chains'
3
4// Replace with your Alchemy API Key
5const apiKey = "demo";
6
7const client = createPublicClient({
8 chain: mainnet,
9 transport: http(`https://eth-mainnet.g.alchemy.com/v2/${apiKey}`)
10})
11
12// Transaction hash for the transaction whose status we want to check
13const txHash = "0xd488331be3a2f9cdd0f2b351f2b13f5151630aaafd2c2b246f7f3cd7fd0b1dfc";
14
15// Getting the status of the transaction using getTransactionReceipt and logging accordingly
16const checkTransactionStatus = async () => {
17 try {
18 const receipt = await client.getTransactionReceipt({ hash: txHash });
19
20 if (!receipt) {
21 console.log("Pending or Unknown Transaction");
22 } else if (receipt.status === 'success') {
23 console.log("Transaction was successful!");
24 } else {
25 console.log("Transaction failed!");
26 }
27 } catch (error) {
28 console.log("Pending or Unknown Transaction");
29 }
30};
31
32checkTransactionStatus();

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

  • Imports either Viem or Ethers.js library to interact with Ethereum.
  • Configures the client/provider with the Alchemy API key and Ethereum mainnet.
  • Defines the transaction hash for the transaction whose status we want to check.
  • Calls the getTransactionReceipt method, 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 receipt is null, it means that the transaction is either pending or unknown. For Viem, the status is ‘success’ or ‘reverted’, while for Ethers.js the status is 1 for success and 0 for failure.

Run the script using:

shell
$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.