0

I am trying to learn how to use the api and trying to print the bitcoin value. When running in node the api call is working and giving the correct output, but in html when the pi is called it is giving network errors. I tried running it in different ide's and applied other things i found on google but had no luck.

Heres my current code,

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Geo Guesser</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <h1 id="price"></h1>


  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
      <script>
      const apiKey = "key";
      const url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?                       symbol=BTC&CMC_PRO_API_KEY=${apiKey}`;

      window.onload = function() {
        axios.get(url)
        .then(response => {
          const bitcoinData = response.data.data.BTC;
          const price = bitcoinData.quote.USD.price;
          document.getElementById("price").innerHTML = `Bitcoin price: $${price}`;
        })
        .catch(error => console.log(error))
      }
    </script>

</body>

</html>

Also, do u recomend using a different api? Thank you in advacne

I tried different methods to calll the api, fetch, azios, etc. Double checked the key and links and api documentation. Made sure calls were being made by checking the console

  • What are the errors? – Pointy Jan 13 '23 at 03:28
  • This is what the console returns D {message: "Network Error", name: "AxiosError", code: "ERR_NETWORK", config: {…}, request: XMLHttpRequest} code: "ERR_NETWORK" config: Object message: "Network Error" name: "AxiosError" request: XMLHttpRequest stack: "AxiosError: Network Error↵ at u.onerror (https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js:1:21766)" __proto__: Object – Karamvir Hira Jan 13 '23 at 03:35
  • Have a [look at this](https://stackoverflow.com/a/64137023). It's a lengthy read, but it's worth it. – FiddlingAway Jan 13 '23 at 17:26
  • If your intent is to learn how to use APIs and how to process their responses, and you don't really care about BTC and the like, you might want to take a look at [this API](https://pokeapi.co/). Or at [this API](https://api.nasa.gov/). – FiddlingAway Jan 13 '23 at 17:38

0 Answers0