0

I am trying to make a site for crypto data using coin-gecko's API. They have 2 different end points for what i require and as such require 2 different URLs.

I had no problem using into the globalUrl to get data such as the total Market cap, volume, etc. which i was able to render into my ejs. My problem is now i cannot use the other URL for this, seeing as I cannot make another get request, what would be the best way to get data from the topCoinsUrl such as say the "id" of bitcoin from the 2nd url please

    const https = require('https');
    const app = express();
    
    app.get("/", function(req, res) {
    
      const globalUrl = "https://api.coingecko.com/api/v3/global";
      const topCoinsUrl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=1h"
    
      https.get(globalUrl , function(response) {
       
        let data = "";
       
        response.on("data", function(chunk) {
          data += chunk
        });
        
        response.on("end", function() {
          const globalMarket = JSON.parse(data);
    
          const totalCryptocurrencies = globalMarket.data.active_cryptocurrencies
          let totalMarketCap = globalMarket.data.total_market_cap.usd
          let totalMarketCapUsd = totalMarketCap.toLocaleString('en-US', {
            style: 'currency',
            currency: 'USD',
          });
          let totalVolume = globalMarket.data.total_volume.usd
          let total24hVolume = totalVolume.toLocaleString('en-US', {
            style: 'currency',
            currency: 'USD',
          });
          let markets = globalMarket.data.markets
          let bitcoinMarketShare = Math.round(globalMarket.data.market_cap_percentage.btc);
    
          res.render("home", {
            totalCryptocurrencies: totalCryptocurrencies,
            totalMarketCap: totalMarketCapUsd,
            total24hVolume: total24hVolume,
            markets: markets,
            bitcoinMarketShare: bitcoinMarketShare
          });
    
        })
      
      }).on("error", function(error) {
        console.error(error)
      });
    });



// Ideally i would like to add this to get the ID of bitcoin, but I get an error when i try to use the 2 get requests:

https.get(topCoinsUrl, function(response) {

    let data = "";
    response.on("data", function(chunk) {
      data += chunk
    });

    response.on("end", function() {
      const topCoinsUrl = JSON.parse(data);

      let bitcoinId = topCoinsUrl[0].symbol


res.render("home", {
  bitcoinId: bitcoinId
})

    })
    // Error handler
  }).on("error", function(error) {
    console.error(error)
  });


});
Ola
  • 1
  • 2
  • 2
    This just sounds like you need to understand how the API works so you can build the right request. Are you asking a specific programming question (how do I write code to do X) or are you asking how to use this particular bitcoin API? Please clarify. – jfriend00 Mar 15 '22 at 01:44
  • Hello, I made an edit to be a bit more specific, essentially i want to know the way i can write code to use both Urls in one get request as currently they are separate and i get errors as a result – Ola Mar 15 '22 at 02:45
  • 1
    You can't use two URLs in one request. You can make one request and then use the results of that to build the second request and then make that second request and wait for its results. Or, you can make two requests in parallel and collect both results. It really depends upon what you're trying to do and how the API works. Right now, it's still not clear what exactly you want us to help you with. – jfriend00 Mar 15 '22 at 02:46
  • FYI, a library such as [got()](https://www.npmjs.com/package/got) or [axios()](https://www.npmjs.com/package/axios) makes these requests a whole lot easier as these higher level libraries are promise based and a operate at a higher level thus you have to write a lot less code to use them. – jfriend00 Mar 15 '22 at 02:48
  • I am trying to make a website that displays crypto data, the first url gives me the data i need for the global market such as total market cap, total volume, etc. but it does not give specific details about individual coins which the 2nd url does but I am trying to get both of them to display/render on the same page via the get request – Ola Mar 15 '22 at 02:53
  • You said _"now i cannot use the other URL for this, seeing as I cannot make another get request..."_. This doesn't make sense to me. Maybe you don't understand how to have two requests in flight at the same time. If that is the case, then see [nodejs multiple http requests in loop](https://stackoverflow.com/questions/19911429/nodejs-multiple-http-requests-in-loop). – Wyck Mar 15 '22 at 02:53
  • Okay, thanks. Would look into them – Ola Mar 15 '22 at 02:54
  • Another possible duplicate (albeit in the context of axios) would be [How to post multiple Axios requests at the same time?](https://stackoverflow.com/questions/61385454/how-to-post-multiple-axios-requests-at-the-same-time) – Wyck Mar 15 '22 at 02:55
  • @Wyck yeah that is exactly it. I just dont know how to get both in flight at the same time. Would check the link, thank you – Ola Mar 15 '22 at 02:56
  • General advice on how to get multiple things happening at once [Call async/await functions in parallel](https://stackoverflow.com/questions/35612428/call-async-await-functions-in-parallel). – Wyck Mar 15 '22 at 02:59

1 Answers1

0

If you wish to make 2 simultaneous requests, you should use something like Promise.all() . Create two network requests and fire them at the same time using Promise.all & collect their result.

You can use Blurebird as well... http://bluebirdjs.com/docs/api/promise.all.html

Saksham Khurana
  • 872
  • 13
  • 26