'Solana: Get Candy Machine info from a NFT token using Js

I am trying to find out what is the candy machine id or address for a specific nft.

So far I have tried looping through wallet accounts using @solana/web3.js

const tokenAccounts = await connection
        .getParsedTokenAccountsByOwner(publicKey, {
          programId: TOKEN_PROGRAM_ID,
        })
        .then((context) => context.value);

Also tried metaplex/js, I got some more info like creators:

const mintAccount = new metaplex.Account(searchNftAddress, mintAccInfo);
          if (mintAccount) {
            const metadata = metaplex.programs.metadata.Metadata.from(mintAccount);
            console.log(metadata.data?.data?.creators);
          }

But I do not find candy machine info.



Solution 1:[1]

If I understand well, do you wan to know the owner of a specific NFT?

This works for me, I took it from here https://solanacookbook.com/references/nfts.html#candy-machine-v1

import {Connection, clusterApiUrl, PublicKey} from '@solana/web3.js';

const connection = new Connection(clusterApiUrl('mainnet-beta'));

export const getOwnerAddresses = async (tokenMint) => {
    //const connection = new Connection('https://api.mainnet-beta.solana.com');

    const largestAccounts = await connection.getTokenLargestAccounts(new PublicKey(tokenMint));
    const largestAccountInfo = await connection.getParsedAccountInfo(largestAccounts.value[0].address);
    //console.log(largestAccountInfo.value.data);
    console.log(largestAccountInfo.value.data.parsed.info.owner);
    /*
    PublicKey {
        _bn: <BN: 6ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9>
    }
     */
};

You just only have to create import the module and use it like this:

getOwnerAddresses('XXXXXXXXX');

Solution 2:[2]

I think when you upload the nfts, in cache folder, a new json will be created and you get the id from there:

{
  "program": {
    "uuid": "Wzsae7",
    "config": "Serai5EV4oisGhcZHYHGaL4J16ebWKU2yagUrer5fBaK"
  },
  "items": {
    "0": {
      "link": "https://arweave.net/flpdtSkJcdospfV93Yfy_tUTrY3abDWDMvTyNMLiY2",
      "name": "TEST 0",
      "onChain": true
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kangulo
Solution 2 Yilmaz