Deep index api get token price

hello,

it’s for sure a javascript noob question, i’m trying to fetch price from the Deep index api like this :

 async function getPrice() {
    fetch(
      "https://deep-index.moralis.io/api/token/ERC20/0xe53ec727dbdeb9e2d5456c3be40cff031ab40a55/price?chain=eth&chain_name=mainnet",
      {
        method: "GET",
        headers: {
          "X-API-KEY":
            "apiKey",
        },
      }
    )
      .then((res) => res)
      .then((data) => console.log(data));
  }
  getPrice();

I’m getting a success Response object like this

Response {type: "cors", url: "https://deep-index.moralis.io/api/token/ERC20/0xe5…0cff031ab40a55/price?chain=eth&chain_name=mainnet", redirected: false, status: 200, ok: true, …}

how can i now retrieve the token price then?

Hey @JonnyDev

I’ve prepared a code example for you:

<html>
  <head>
    <title>Balance Demo</title>

    <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
  </head>

  <body>
    <button onclick="task()">Task</button>;
    <script>
      Moralis.initialize("xxxxxxxxxxxxxxx"); // Application id from moralis.io
      Moralis.serverURL = "xxxxxxxxxxxxxxxx"; //Server url from moralis.io

      async function getPrice() {
        return fetch(
          "https://deep-index.moralis.io/api/token/ERC20/0xe53ec727dbdeb9e2d5456c3be40cff031ab40a55/price?chain=eth&chain_name=mainnet",
          {
            method: "GET",
            headers: {
              accept: "*/*",
              "X-API-KEY":
                "xxxxxxxxxxxxxxxxxxxxx"
            }
          }
        ).then((response) => {
          return response.json();
        });
      }

      async function task() {
        const tokenInfo = await getPrice();
        console.log(tokenInfo);
      }
    </script>
  </body>
</html>

It will return to the console:

image

You can specify the data you want to show e.g. const price = tokenInfo.usdPrice

Hope it will help you :man_mechanic:

2 Likes

Thank you very much, you are a Boss Yomoo !! it’s Work

2 Likes