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 = '' + request.params.userAddr + '/nft/' + request.params.nftAddr;
  return Moralis.Cloud.httpRequest({
    url: url,
    params: {chain: "eth"},
    headers: {
    	'accept': 'application/json',
    	'X-API-KEY': '#REDACTED#'
  }).then( function(httpResponce){
  }, function(httpResponse){"error");;

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:

// 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:

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