APIs used:
  • Get single NFT with cached metadata from contract  

Displaying the current owner of an NFT is fundamental to the functionality of most NFT applications, whether they are marketplaces, galleries, or platforms showcasing digital assets. The unique aspect of NFTs lies in their ownership being transparently and immutably tracked on the blockchain, making it possible to verify who holds an asset at any given time.

This guide walks you through leveraging the Covalent API to retrieve and display the owner of an NFT.

Prerequisites

Before starting, ensure you have:

  • Node.js installed on your computer.

  • An API key from Covalent, which you can obtain on the Covalent website.

Tutorial: Getting the Current Owner of an NFT

1

Setup

First, you need to set up your environment to use the Covalent API. If you haven't already, install the Covalent client SDK by running:

This command will install all necessary libraries to interact with the Covalent API.

npm install @covalenthq/client-sdk
2

Understand the Endpoint

The Covalent API endpoint to get a single NFT with cached metadata is structured as follows:

/v1/{chainName}/nft/{contractAddress}/metadata/{tokenId}/

This endpoint allows you to query detailed metadata for an NFT, including ownership, by providing the blockchain name, the NFT contract address, and the token ID.

Parameters:

  • chainName: The blockchain network (e.g., eth-mainnet).

  • contractAddress: The smart contract address hosting the NFT collection.

  • tokenId: The unique identifier for the NFT within its collection.

Query Params (Optional):

  • no-metadata: If present, the response will omit metadata.

  • with-uncached: Set to true to fetch metadata from upstream servers if not cached.

3

Fetching NFT Ownership

To get the current owner of an NFT, you can use the following JavaScript code. This script utilizes the Covalent API to query the NFT metadata, which includes ownership details.

Remember to replace "Your_Covalent_API_Key", "nft_contract_address_here", and "nft_token_id_here" with your actual Covalent API key, the contract address of the NFT, and the token ID you're interested in, respectively.

import { CovalentClient } from "@covalenthq/client-sdk";

const ApiServices = async () => {
    const client = new CovalentClient("Your_Covalent_API_Key");
    const chainName = "eth-mainnet"; // Specify the blockchain network
    const contractAddress = "nft_contract_address_here"; // NFT contract address
    const tokenId = "nft_token_id_here"; // NFT Token ID

    // Fetch NFT metadata
    const resp = await client.NftService.getNftMetadataForGivenTokenIdForContract(chainName, contractAddress, tokenId);
    console.log(resp.data);
};

ApiServices().catch(console.error);
4

Understanding the Response

Here is a snippet of the API response, which returns all the associated metadata for the specified NFT, including its current owner, which you can find in the current_owner field.

json
nft_data: {
	token_id: "940",
	token_url: "<https://ipfs.io/ipfs/QmarGRwVKPaCe2s5QSSTMEdbYDwKxFz6bAn58YZPPcWc7k/940>",
	original_owner: null,
	current_owner: "0xd2ce458c449727e628944cc2ff25386205ba6f7f",
	external_data: {
		name: "Invisible Friends #940",
		description: "5,000 animated Invisible Friends hiding in the metaverse. A collection by Markus Magnusson & Random Character Collective.",
		asset_url: "<https://nftassets.covalenthq.com/aaed746920ac10d789eedcba6d334448473cdf6dc3ed1719601d6d90c25ca0d5.gif>",
		asset_file_extension: "gif",
		asset_mime_type: "image/gif",
		asset_size_bytes: "1313101",
		image: "<https://nftassets.covalenthq.com/aaed746920ac10d789eedcba6d334448473cdf6dc3ed1719601d6d90c25ca0d5.gif>",
		image_256: "<https://nftassets.covalenthq.com/cdn-cgi/image/width=256,fit/aaed746920ac10d789eedcba6d334448473cdf6dc3ed1719601d6d90c25ca0d5.gif>",
		image_512: "<https://nftassets.covalenthq.com/cdn-cgi/image/width=512,fit/aaed746920ac10d789eedcba6d334448473cdf6dc3ed1719601d6d90c25ca0d5.gif>",
		image_1024: "<https://nftassets.covalenthq.com/cdn-cgi/image/width=1024,fit/aaed746920ac10d789eedcba6d334448473cdf6dc3ed1719601d6d90c25ca0d5.gif>",

Conclusion

From marketplaces and galleries to platforms showcasing digital art and collectibles, the transparency and immutable tracking of ownership on the blockchain make NFTs uniquely secure and verifiable. This guide has equipped you with the knowledge and tools to leverage the Covalent API for retrieving and displaying the owner of an NFT. Happy building!