Examples Using the Alchemy SDK
Real-world examples on how to query information from the blockchain using the most powerful Web3 SDK.
If you’re looking to start using the Alchemy SDK, but want some plug-and-play examples on how to get started with common use cases, here’s the place to start! We’ll walk you through setting up the Alchemy SDK and a variety of basic tasks you can do.
Setting up the Alchemy SDK
This guide assumes you already have an Alchemy account and access to our Dashboard.
1. Create an Alchemy Key
To access Alchemy’s free node infrastructure, you need an API key to authenticate your requests.
You can create API keys from the dashboard. Check out this video on how to create an app:
Or follow the written steps below:
First, navigate to the “create app” button in the “Apps” tab.
Fill in the details under “Create App” to get your new key. You can also see apps you previously made and those made by your team here. Pull existing keys by clicking on “View Key” for any app.
You can also pull existing API keys by hovering over “Apps” and selecting one. You can “View Key” here, as well as “Edit App” to whitelist specific domains, see several developer tools, and view analytics.
2. Install and set up the Alchemy SDK
To install the Alchemy SDK, you want to create a project, and then navigate to your project directory to run the installation. Let’s go ahead and do that! Once we’re in our home directory, let’s execute the following:
With NPM:
Next, create a file named index.js
and add the following contents:
You should ultimately replace demo
with your Alchemy HTTP API key.
Unfamiliar with the async stuff? Check out this Medium post.
3. Run your dApp using Node
You should now see the latest block number output in your console!
4. Import SDK Utils (Optional)
Optionally, you can import SDK utils to perform certain operations, such as parsing ETH to Wei as shown in the example below:
Below, we’ll list a number of examples on how to make common requests on Ethereum or EVM blockchains.
SDK Core Endpoint Examples
How to Get the Latest Block Number on Ethereum
A block is generated on Ethereum approximately every 15 seconds. If you’re looking for the latest block, you can plug in the following:
How to Get a Block By Its Block Hash on Ethereum
Every block on Ethereum correspond to a specific hash. If you’d like to look up a block by its hash, you can plug in the following code:
How to Get a Block By Its Block Number on Ethereum
Every block on Ethereum correspond to a specific number. If you’d like to look up a block by its number, you can plug in the following code:
How to Get Logs for an Ethereum Transaction
Logs are essentially a published list of user-defined events that have happened on the blockchain during an Ethereum transaction. You can learn more about them in Understanding Logs: Deep Dive into eth_getLogs.
Here’s an example on how to write a getLogs query on Ethereum:
How to Make an Eth_Call on Ethereum
An eth_call in Ethereum is essentially a way to execute a message call immediately without creating a transaction on the block chain. It can be used to query internal contract state, to execute validations coded into a contract or even to test what the effect of a transaction would be without running it live.
Here’s an example on how to write a call query on Ethereum:
How to Get a Transaction by Its Hash on Ethereum
Every transaction on Ethereum correspond to a specific hash. If you’d like to look up a transaction by its hash, you can plug in the following code:
How to Get a Transaction Receipt on Ethereum
Every transaction on Ethereum has an associated receipt with metadata about the transaction, such as the gas used and logs printed during the transaction. If you’d like to look up a transaction’s receipt, you can plug in the following code:
How to get a User’s Transaction Count on Ethereum
The number of transactions a user has sent is particularly important when it comes to calculating the nonce for sending new transactions on Ethereum. Without it, you’ll be unable to send new transactions because your nonce won’t be set correctly.
How to Fetch Historical Transactions on Ethereum
Oftentimes, you’ll want to look up a set of transactions on Ethereum between a set of blocks, corresponding to a set of token types such as ERC20 or ERC721, or with certain attributes. Alchemy’s proprietary Transfers API allows you to do so in milliseconds, rather than searching every block on the blockchain.
How to Estimate the Gas of a Transaction on Ethereum
Oftentimes, you’ll need to calculate how much gas a particular transaction will use on the blockchain to understand the maximum amount that you’ll pay on that transaction. This example will return the gas used by the specified transaction:
How to Get the Current Gas Price in Ethereum
You can retrieve the current gas price in wei using this method:
SDK Websocket Examples
How to Subscribe to New Blocks on Ethereum
If you’d like to open a WebSocket subscription to send you a JSON object whenever a new block is published to the blockchain, you can set one up using the following syntax.
How to Subscribe to Pending Transactions on Ethereum
Alchemy exposes a proprietary WebSockets endpoint pendingTransactions that allows you to receive a JSON object whenever a pending transaction matching a set of requirements is submitted or confirmed on the blockchain.
In the following example, you can receive a JSON object whenever a pending transaction is submitted with the recipient as Vitalik Buterin’s contract address.
SDK NFT API Examples
How to Check the Owner of an NFT
If you’d like to identify which contract address currently owns a specific NFT, you can use Alchemy’s proprietary NFT API to search the blockchain. See the example below:
How to get All NFTs in a Collection
If you’d like to print the metadata of all the NFTs in a specific collection, you can use Alchemy’s proprietary NFT API to search the blockchain. See the example below:
How to Get All NFTs Owned by an Address
If you’d like to retrieve all the NFTs owned by a specific user address, you can use Alchemy’s proprietary NFT API to search the blockchain. See these example below:
SDK Transact Examples
How to Send a Transaction to the Blockchain
One of the most common requests is sending a transaction to be mined onto the blockchain. Here’s some example code showing you how to make a transaction from scratch, sign it, and send it to be confirmed.
How to Send a Private Transaction to the Blockchain
If you’re worried about your transactions being front-run on Ethereum, Flashbots has created a tool to allow you to submit transactions without exposing them to the entire blockchain! It provides greater privacy while maintaining all the features of sendTransaction.
SDK Notify Examples
How to Create an Alchemy Notify Webhook
If you’d like to set up a Webhook to alert you when a pending transaction is submitted, mined, or when activity happens on an NFT address, we have a simple CRUD API that lets you create, update, and delete our Notify Webhooks programmatically.