Guide 3: How to get transaction history using the Alchemy SDK

Use the Alchemy SDK to get any address's historical transaction activity.

The SDK Developer Challenge: What are we here for?

The SDK Developer Challenge is an opportunity to build and learn together, using daily guides to inspire and motivate you to supercharge your app with the SDK.

The SDK will be your secret weapon to help simplify your code so you can build faster.

As you build, submit your project to win prizes.

What could you win? Epic never-before seen Alchemy swag, and $500 in monthly Alchemy credit.

For all of the challenges, you will need to set up a free Alchemy account. This will give you access to the SDK, as well as a HUGE selection of web3 development tools.

Ok, let's go!

Guide 3: How to Get Transaction History using the Alchemy SDK

Alchemy's Transfers API allows you to access comprehensive transaction history for any user or contract address in a single API call. This information is vital for giving users portfolio management tools within your app.

Let's start:

Here's a code snippet you can copy and paste directly into your project to instantly query an address's historical transfers of any of the following token types:

  • Internal ETH transfers
  • External ETH transfers
  • ERC-721 transfers
  • ERC-1155 transfers
  • ERC-20 transfers
  • Special NFT transfers
const { Alchemy, Network, AssetTransfersCategory } = require('alchemy-sdk');

// Optional Config object, but defaults to demo api-key and eth-mainnet.
const settings = {
  apiKey: process.env.ALCHEMY_API_KEY, // Replace with your Alchemy API Key.
  network: Network.ETH_MAINNET, // Replace with your desired network.
};

async function main() {
  const getTransfers = await alchemy.core.getAssetTransfers({
    fromBlock: '0x0',
    toBlock: 'latest',
    toAddress: '0x59a5493513bA2378Ed57aE5ecfB8A027E9D80365',
    excludeZeroValue: true,
    category: [AssetTransfersCategory.ERC721],
  });
  console.log(getTransfers);
}

main();

Video Walkthrough

If you are just getting started with the Alchemy SDK Developer Challenge, here is your video primer for Guide 3. In this video, Al explains how to set up the Alchemy SDK and make an API request to the getAssetTransfers endpoint using the Alchemy SDK object. Here's a quick breakdown:

  • An introduction on compatible token types when calling the Transfers API
  • How to install the dotenv npm package to protect your API key
  • How to set up the Alchemy SDK
  • An introduction to namespaces within the Alchemy SDK
  • How to retrieve historical transfers of an address with the Alchemy SDK
  • Troubleshooting common errors
  • A summary of results returned when calling the Transfers API via the Alchemy SDK