How to use Ganache or Hardhat (local devchain) with Moralis Web3 API?

Hi guys,

I’m following a tute on getting Cloud API to work with my dapp, wrote this cloud function based on the tute:

Moralis.Cloud.define("getNFTs", async (request) => {
  let url = 'https://deep-index.moralis.io/api/v2/' + request.params.userAddr + '/nft/' + request.params.nftAddr
  logger.info(url);
  return Moralis.Cloud.httpRequest({
    url: url,
    params: {chain: "eth"},
    headers: {
    	'accept': 'application/json',
    	'X-API-KEY': '#REDACTED#'
  	}
  }).then( function(httpResponce){
    return httpResponce.data;
  }, function(httpResponse){
    logger.info("error");
    logger.info(httpResponse);
  })
});

works great!

What I cant do is get it to work with my local Ganache instance? Is it even possible?

What I tried was setting the chain: “localdevchain” on a Ganache environment server instance connected with frpc.

Thanks guys!

The API won’t work with your local dev chain unfortunately, you can use mainnet and testnets for all networks we support, but getting your ganache to API is not possible as API is big infrastructure behind the scenes and we need to run the nodes ourselves to feed the API with data, thus we cant feed API with your ganache

1 Like

Btw instead of API, I recommend using the same function but in SDK, it’s way cleaner and easier: https://docs.moralis.io/moralis-server/web3-sdk/account#getnfts

// get NFTs for current user on Mainnet
const userEthNFTs = await Moralis.Web3API.account.getNFTs();

// get testnet NFTs for user
const testnetNFTs = await Moralis.Web3API.account.getNFTs({ chain: 'ropsten' });

// get polygon NFTs for address
const options = { chain: 'matic', address: '0x...' };
const polygonNFTs = await Moralis.Web3API.account.getNFTs(options);
1 Like

Ok cool, actually I think this is the one I was looking for, I must have overlooked it:

https://docs.moralis.io/moralis-server/web3-sdk/account#getnftsforcontract

const options = { chain: 'matic', address: '0x...', token_address: '0x...' };
const polygonNFTs = await Moralis.Web3API.account.getNFTsForContract(options);
2 Likes