[SOLVED] Moralis.Cloud.httpRequest fails with 401

Does anyone know what API Key to use authenticate the header in Moralis.Cloud.httpRequest ?

I keep getting a 401 error returned.

Is it the same API Key as moralisApiKey ?

I setup a Cloud function job like this and trigger it under Jobs in the dashboard:

Moralis.Cloud.job('addUserItems', async (request) => {
  const logger = Moralis.Cloud.getLogger();
  logger.info('Running addUserItems job...');
  logger.info(`GET Request Params Input: ${request.params.input}`);

  const chain = 'polygon';
  const chainId = '137';
  const apiKey = 'xxxxxxxxxxx';
  const ADRESSS = '0x67f4732266c7300cca593c814d46bee72e40659f';

  const getCollections = new Moralis.Query('Collections');
  getCollections.equalTo('chainId', chainId);
  getCollections.containedIn('contract_address', [ADRESSS]);
  getCollections.ascending('name');
  const collections = await getCollections.find({ useMasterKey: true });

  logger.info(`Finding collections: ${collections}`);

  if (collections) {
    await collections.forEach((collection) => {
      const options = {
        token_address: collection.get('contract_address'),
        chain: chain,
      };

      // this is the last log, after this logs the errors at the bottom
      logger.info(
        `URL: https://deep-index.moralis.io/api/v2/nft/${options.token_address}/owners?chain=${options.chain}&format=decimal`
      );

      Moralis.Cloud.httpRequest({
        url: `https://deep-index.moralis.io/api/v2/nft/${options.token_address}/owners?chain=${options.chain}&format=decimal`,
        headers: {
          accept: 'application/json',
          'X-API-Key': apiKey,
        },
      }).then(
        async (httpResponse) => {
          logger.info('RESPONSE:', httpResponse);
          logger.info('RESPONSE LENGTH:', httpResponse.data.result.length);
        },
        function (httpResponse) {
          logger.info(
            'addZedUserItems: error getting NFTs in contract from web3 api'
          );
          logger.info(
            'Request failed with response code ' + httpResponse.status
          );
        }
      );
    });
  }
});

Is the same api key what you can find in web3api interface in admin interface

1 Like