How to retrieve Candy Machine details from NFT using JavaScript on Solana

Need help finding Candy Machine information for a specific NFT

I’m working on a project where I need to trace back from an NFT to its original Candy Machine. I want to get the candy machine address or ID that minted a particular NFT token.

I’ve been experimenting with different approaches using JavaScript libraries but haven’t found the right solution yet.

First attempt was with @solana/web3.js to check wallet accounts:

const userTokens = await rpcConnection
    .getParsedTokenAccountsByOwner(walletPublicKey, {
      programId: SPL_TOKEN_PROGRAM_ID,
    })
    .then((response) => response.value);

Then I tried using metaplex/js which gave me creator information:

const nftMintAccount = new metaplex.Account(targetNftAddress, accountInfo);
if (nftMintAccount) {
  const nftMetadata = metaplex.programs.metadata.Metadata.from(nftMintAccount);
  console.log(nftMetadata.data?.data?.creators);
}

The creator data shows up fine but I can’t locate any candy machine references. Is there a specific field or method I should be looking for? Maybe I need to dig deeper into the metadata structure or use a different approach entirely.

Any guidance on the correct way to link an NFT back to its originating candy machine would be really helpful.

the creators array doesnt always have the candy machine addr. try checkin the nft’s tx history - find the mint tx & trace back to see which program mntd it. the cm addr is usually in the tx logs, not the metadata.

hmm interesting challenge! I’ve been working on something similar and hit the same wall.

Try parsing the mint instruction data directly. When you find that original mint transaction like CreativePainter45 mentioned, the candy machine address is buried in the instruction accounts - usually as a signer or in the remaining accounts array.

Are you using candy machine v1 or v2? The structure changed between versions and v2 stores things differently. Also curious - what type of NFTs are you working with? Some collections use custom minting programs that don’t follow the standard candy machine pattern.

Have you tried the @metaplex-foundation/mpl-candy-machine package? It has helper methods that might be easier than digging through raw transaction data manually.

Oh and one more thing - are you getting specific errors when you try to access the transaction history, or is the data structure just not what you expected?

Been wrestling with this exact problem for months. The trick is checking the collection metadata field instead of just creators. Most candy machines set themselves as the collection authority when minting. Fetch the collection NFT first, then check its update authority - that’s usually your candy machine address. Here’s what worked for me:

const metadata = await metaplex.nfts().findByMint({ mintAddress });
if (metadata.collection?.address) {
  const collectionNft = await metaplex.nfts().findByMint({ 
    mintAddress: metadata.collection.address 
  });
  console.log(collectionNft.updateAuthority); // this should be your CM
}

Way more reliable than parsing transaction logs since some candy machines delegate minting to other wallets. Only caveat is it doesn’t work for older collections that didn’t use the collection standard properly.