Pleas i need help on how to to fetch price of tokens

please my code is not fetching the right price it keeps saying NaN instead of displaying the ratio of two token,
const express = require(“express”);
const Moralis = require(“moralis”).default;
const app = express();
const cors = require(“cors”);
require(“dotenv”).config();
const port = 3001;

app.use(cors());
app.use(express.json());

app.get("/tokenPrice", async (req, res) => {

const {query} = req;

const responseOne = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressOne
});

const responseTwo = await Moralis.EvmApi.token.getTokenPrice({
address: query.addressTwo
});

const usdPrices ={
tokenOne: responseOne.raw.usdPrice,
tokenTwo: responseTwo.raw.usdPrice,
ratio: responseOne.raw.usdPrice / responseTwo.raw.usdPrice
}

return res.status(200).json({usdPrices});
});

Moralis.start({
apiKey: process.env.MORALIS_KEY,
}).then(() => {
app.listen(port, () => {
console.log(Listening for API Calls);
});
});

1 Like

Hi @lincs

NaN can mean that the value is returned as null or there must me some errors fetching the price. Can you try adding any console.log to debug what is returning from the API?

1 Like