How to update NFT metadata for via NFT Collection/Edition Contract

In this guide, we will explore how to update the NFT metadata using code and the Thirdweb SDK.

💡
This method only works for our prebuilt NFT Collection & Edition contracts

Here is the code to use to update the NFT Metadata

import { defineChain } from "thirdweb/chains";
import { privateKeyToAccount } from "thirdweb/wallets";
import { sendTransaction, getContract, createThirdwebClient } from "thirdweb";
import { setTokenURI } from "thirdweb/extensions/erc1155"; // only for ERC-1155
import { setTokenURI } from "thirdweb/extensions/erc721"; // only for ERC-721
import { upload } from "thirdweb/storage";


export async function main() {
  const client = createThirdwebClient({
    secretKey: "",
  });
  const account = privateKeyToAccount({
    client,
    privateKey: "",
  });
  const contract = getContract({
    client,
    chain: defineChain(80002),
    address: "",
  });

  const uri = await upload({
    client,
    files: [{
      "name": "Mata NFT #30",
      "description": "A dummy NFT collection of 30 unique pixelated arts based on HashLips Art Engine sample assets. This is for testing purposes only and not real NFT collection.",
      "image": "ipfs://QmUF6KuAZR9oAxd46kSjj7PagBEeL2yrxHYjbUEaVQQvBg/30.png",
      "dna": "f8b824d7c1e8c6f9f6cebc0618258438c9c0e8f6",
      "edition": 0,
      "date": 1665696158414,
      "external_url": "https://github.com/warengonzaga/nft-dummy-assets",
      "creator": "Waren Gonzaga",
      "attributes": [
        {
          "trait_type": "Background",
          "value": "Normal"
        },
        {
          "trait_type": "Eyeball",
          "value": "White"
        },
        {
          "trait_type": "Eye Color",
          "value": "Yellow"
        },
        {
          "trait_type": "Iris",
          "value": "Normal"
        },
        {
          "trait_type": "Shine",
          "value": "Normal"
        }
      ],
      "compiler": "The thirdweb Art Engine"
    }],
  });
 
  const tx = setTokenURI({
    contract,
    tokenId: 0n,
    uri
  })
  try {
    const transactionResult = await sendTransaction({
      transaction: tx,
      account: account,
  
    });
  } catch (e) {
    console.log(e)
  }

}
main();
 

Can’t get this working? If you’ve followed the above and still have issues, contact our support team for help.

 
Did this answer your question?
😞
😐
🤩