How to Manage a Multichain Project Using Alchemy SDK
Learn how to manage multiple API keys and network endpoints in the same project using Alchemy SDK
Introduction
Web3 development often requires interacting with multiple blockchain networks, each with its own API endpoint and authentication keys. Managing all these connections and ensuring the correct API key and endpoint are used for each chain can be time-consuming and error-prone. Alchemy SDK provides a solution to this problem with its AlchemyMultiChainClient
, a wrapper around the Alchemy SDK that allows for easy management of multiple chains in a single application. In this tutorial, we’ll walk through a demo project that demonstrates how to use the AlchemyMultiChainClient
to query NFTs for an owner on multiple chains.
Project Setup
To follow along with this tutorial, you’ll need to have Node.js and npm installed on your machine. You’ll also need to clone the demo project from Alchemy’s GitHub repository by running the following command in your terminal:
After cloning the repo, navigate to the project directory and run the following command to install its dependencies:
Now that we have the demo project set up, we can move on to querying multiple chains.
Querying Multiple Chains
The src/index.ts
file contains a simple demo that uses the AlchemyMultichainClient
class to make requests across multiple chains. Let’s walk through the code to understand how it works.
First, we import the necessary modules:
The AlchemyMultichainClient
class is imported from the alchemy-multichain-client
file in the same directory, and the Network
is imported from the Alchemy SDK.
Next, we define the configuration settings for our multichain application. This is done by creating a defaultConfig
object which contains the default settings that will be used across all networks. In this example, the defaultConfig
object includes an Alchemy API key and specifies the ETH_MAINNET
as the default network.
We can also include optional setting overrides for specific networks, if needed. This is done by creating an overrides
object which maps network IDs to their respective configuration overrides. For example, in the overrides object in the example code below, we are overriding the API key for the MATIC_MAINNET
and ARB_MAINNET
networks.
With the configuration settings defined, we can now create an instance of the AlchemyMultichainClient
by passing in the defaultConfig
and overrides
objects.
Once the AlchemyMultichainClient
instance is created, we can start making requests to different networks using the forNetwork
method. This method returns a new instance of the AlchemyMultichainClient
with the specified network settings, so you can chain requests together.
In the example code below, we are using the forNetwork
method to make requests to the ETH_MAINNET
and MATIC_MAINNET
networks to get the NFTs for an owner with a specified wallet address. We are also specifying the pageSize option to limit the number of NFTs returned to 5.
Finally, we log the results of our requests to the console.
Testing the Script
To test the script, make sure you have typescript installed by running this command, in the root of terminal:
Next, you need to compile your TypeScript file by running the following command:
This will generate a JavaScript file called index.js in the same directory as your index.ts file. You can then run the index.js file using Node.js:
Finally, it will log the mainnetNfts
and maticNfts
for the given owner in your terminal 🎉
Conclusion
In this tutorial, we have seen how to use the Alchemy SDK with multiple chains in a single application using the AlchemyMultichainClient
class. We have also seen how to manage the different API keys and endpoints for different chains. With the knowledge gained from this tutorial, you can now start building your own multichain applications using the Alchemy SDK.