Get Token Prices for multiple tokens

Am using the API to fetch token prices and rates in my nodejs project but unfortunately it not as easy at its been explained in the documentation, i keep getting error. please help.

...........
require("dotenv").config();

const Moralis = require("moralis");

const { EvmChain } = require("@moralisweb3/common-evm-utils");

// Initialize Moralis asynchronously

const startMoralis = async () => {

  try {

    await Moralis.start({ apiKey: process.env.COIN_API_KEY });

    const response = await Moralis.EvmApi.token.getMultipleTokenPrices();

    return response.raw; // Return this data for the route to send as response

  } catch (e) {

    console.error(e);

    throw e; // Re-throw the error to handle it in the router

  }

};

module.exports = { startMoralis };

...........
const express = require("express");

const router = express.Router();

const { startMoralis } = require("../utils/moralisApi");

router.get("/tokens-rates", async (req, res) => {

  try {

    const data = await startMoralis();

    res.json(data);

  } catch (error) {

    res.status(500).json({ error: 'Failed to fetch token rates' });

  }

});

module.exports = router; 



.....

and here is my server.js
...


const cryptoRatesRoutes  = require("./routes/cryptorates");

app.use("/api/tokens-rates", cryptoRatesRoutes);

Moralis web3 is good. But you can get the token prices more easily.

i need solution please or any recommendation

This post was flagged by the community and is temporarily hidden.

Hi @bamfrontend

Can you please share more details on the error you received? So we know what is throwing the error.

you are not putting any parameter on that call.

 const response = await Moralis.EvmApi.token.getMultipleTokenPrices({
    "chain": "0x1",
    "include": "percent_change"
  },{
    "tokens": [
      {
        "tokenAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7"
      },
      {
        "tokenAddress": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
      },
      {
        "exchange": "uniswapv2",
        "tokenAddress": "0xae7ab96520de3a18e5e111b5eaab095312d7fe84",
        "toBlock": "16314545"
      },
      {
        "tokenAddress": "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"
      }
    ]
  });

this is an example, also check https://docs.moralis.io/web3-data-api/evm/reference/get-multiple-token-prices