Skip to main content

tokenURI

The tokenURI method is specific to the ERC-721 token standard. It retrieves a Uniform Resource Identifier (URI) for a given token ID. This URI typically points to metadata about the token, such as a JSON file containing attributes like the token's name, description, image, and other attributes. The method is an integral part of the ERC721Instance object, which can be acquired via an appropriate method of the client library (e.g., EdexaClient or similar).

Method Signature

tokenURI(tokenId: string): string

Parameters

ParameterTypeDescription
tokenIdstringA unique identifier corresponding to a specific ERC-721 token within a contract. Each non-fungible token (NFT) within the contract has a distinct tokenId that differentiates it from others. This ID is used to fetch its associated URI.

Returns

  • A string representing the URI associated with the given token ID.

Usage Example

First, instantiate the desired ERC-721 token contract:

const erc721 = edexaClient.getERC721Instance("your ERC-721 contract address here");

Then, call the tokenURI method with a specific token ID:

const tokenId = "12345"; // Example token ID
const uri = erc721.tokenURI(tokenId);
console.log(`Token ID ${tokenId} has the following URI: ${uri}`);

Notes

  • The returned URI might be a full URL (e.g., pointing to an IPFS hash or a web server) or a base URI concatenated with the token ID, depending on the token's implementation.

  • The actual metadata content at the URI is usually not part of the Ethereum blockchain but stored off-chain, ensuring flexibility and minimizing storage costs.

  • Always ensure the validity of the provided token ID. Passing an invalid or non-existent token ID might result in an error or an undefined return value.