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);