tokenURI

tokenURI

The tokenURI method is exclusive to the ERC-721 token standard and serves the purpose of fetching a Uniform Resource Identifier (URI) associated with a given token ID. This URI typically directs to metadata concerning the token, often stored in a JSON format containing details such as the token's name, description, image, and other attributes. Embedded within the ERC721Instance object, this method can be accessed through appropriate functions provided by client libraries such as ZkyugaClient.

Method Signature

tokenURI(tokenId: string): string

Parameters

Parameter
Type
Description

tokenId

string

A unique identifier specific to an ERC-721 token within a contract. Each non-fungible token (NFT) has a distinct tokenId, distinguishing it from others. This ID is used to retrieve the associated URI.

Returns

  • A string representing the URI linked with the provided token ID.

Usage Example

Begin by instantiating the desired ERC-721 token contract:

const erc721 = zkyugaClient.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 URI returned may be a complete URL, pointing to resources such as IPFS hashes or web servers, or it may be a base URI concatenated with the token ID, depending on the token's implementation.

  • The actual metadata content at the URI typically resides off-chain, reducing storage costs and enhancing flexibility.

  • Always validate the provided token ID to avoid potential errors or undefined return values.


Last updated