Skip to main content

Setting Token URI

This guide outlines the process of setting or updating the Uniform Resource Identifier (URI) for a token using the ERC1155 API. The setTokenURI method enables you to associate a new URL with a specific token, providing essential information about the token's properties or metadata.


Overview of the setTokenURI Method

The Token URI is a vital component in the ERC1155 standard, offering a way to link tokens to external information, typically hosted as a JSON file. This information can include the token's name, description, image, and other attributes.


Prerequisites

  • Installed the edeXa-sdk in your project.
  • An authentication token obtained through the SDK's authentication method. This token is necessary to authorize your requests.

Sample Code Snippet

Here is a JavaScript code snippet demonstrating the use of the setTokenURI method:

import { ERC1155, Network } from 'edeXa-sdk';

async function setTokenURI() {
// Configuration settings for the ERC1155 instance
const settings = {
network: Network.SANDBOX, // Use Network.MAINNET for production environments
};

const erc1155 = new ERC1155({
...settings,
authorization: `Bearer ${token}`, // Replace `${token}` with your actual token
});

// Data required to set the token URI
const dataToSetTokenURI = {
tokenId: 'uniqueTokenId', // Token ID of the token
URL: 'https://example.com/token-metadata.json', // New URL for the token
chaincode: 'optionalChaincodeName', // Optional chaincode name
channel: 'optionalChannelName', // Optional blockchain channel
};

try {
// Execute the operation to set the token URI
const setTokenURIResponse = await erc1155.setTokenURI(dataToSetTokenURI);
// Handle success
} catch (error) {
// Handle error
}
}

Request Parameters

ParameterTypeRequiredDescription
tokenIdStringYesThe ID of the token for which to set the URI.
URLStringYesThe new URL to associate with the given token ID.
chaincodeStringNoThe name of the smart contract or chaincode managing the tokens.
channelStringNoThe name of the blockchain channel where the operation occurs.

Response Format

Upon successfully setting the token URI, the setTokenURI method returns a response object containing:

KeyTypeDescription
tokenIdStringThe unique identifier of the token.
URIStringThe newly set URI of the token.