The Covalent API, renowned for its unified approach to blockchain data, offers developers seamless access to detailed information across over 200 blockchains. With the integration of Covalent's Unified API into the Viem framework, leveraging the TypeScript-based Viem Compatible SDK, developers can now efficiently utilize blockchain data in application development. This guide will walk you through setting up and using the Covalent API with the Viem framework, enhancing your blockchain projects with minimal API calls.

Why Use Covalent with Viem?

Covalent provides a robust solution for accessing comprehensive blockchain data, including transactions, balances, and NFTs, with industry-leading performance. The integration with Viem allows developers to:

  • Access data across multiple blockchains seamlessly.

  • Reduce development overhead with simplified API interactions.

  • Implement features quickly with high-level abstractions and pre-built functions.

Getting Started with Covalent and Viem

1

Installation

First, ensure you have Node.js installed on your system. Then, you can install the necessary packages using npm or Yarn:

Bash
npm install viem @covalenthq/client-viem-sdk

or

Bash
yarn add viem @covalenthq/client-viem-sdk
2

Setting Up a Client

Before you can make any API calls, you need to obtain a Covalent API key. Sign up at the Covalent website and retrieve your key from the API Keys tab. Once you have your API key, you can set up your client as follows:


import { createClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { publicActionCovalent } from '@covalenthq/client-viem-sdk';

const client = createClient({
  chain: mainnet,
  transport: http("YOUR_RPC_URL")  // Replace "YOUR_RPC_URL" with your RPC provider URL
}).extend(publicActionCovalent("YOUR_API_KEY"));
3

Consuming Actions with Covalent

With the client set up, you can now access various blockchain data through the Covalent API. Here are some examples of actions you can perform:

Fetch Token Balances:

jsx
const tokenBalances = await client.BalanceService.getTokenBalancesForWalletAddress("eth-mainnet", "demo.eth");
console.log(tokenBalances.data);

Get Address Activity:

jsx
const walletActivity = await client.BaseService.getAddressActivity("demo.eth");
console.log(walletActivity);

Retrieve NFTs:

jsx
const nfts = await client.NftService.getNftsForAddress("eth-mainnet", "demo.eth");
console.log(nfts);

Check Historical NFT Floor Prices:

jsx
const floorPrices = await client.NftService.getNftMarketFloorPrice("eth-mainnet", "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d");
console.log(floorPrices);

Conclusion

Integrating the Covalent API with the Viem framework empowers developers to build more dynamic, data-rich blockchain applications. By following this guide, you can enhance your applications with extensive blockchain data accessible through a unified API, ensuring your projects are both powerful and efficient. For further details and more advanced use cases, refer to the README documentation provided with the @covalenthq/client-viem-sdk.