common-close-0
BYDFi
Trade wherever you are!

How can I use JavaScript to fetch real-time cryptocurrency prices on window load?

avatarManonDec 25, 2021 · 3 years ago3 answers

I want to display real-time cryptocurrency prices on my website using JavaScript. How can I fetch the latest prices and update them automatically when the page loads? Are there any specific APIs or libraries that I can use for this purpose?

How can I use JavaScript to fetch real-time cryptocurrency prices on window load?

3 answers

  • avatarDec 25, 2021 · 3 years ago
    You can use the CoinGecko API to fetch real-time cryptocurrency prices in JavaScript. CoinGecko provides a simple and reliable API that allows you to retrieve the latest prices for various cryptocurrencies. You can make an HTTP request to their API endpoint and parse the response to get the desired data. Here's an example code snippet: ```javascript fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd') .then(response => response.json()) .then(data => { const bitcoinPrice = data.bitcoin.usd; const ethereumPrice = data.ethereum.usd; // Update the prices on your website }); ``` This code fetches the prices of Bitcoin and Ethereum in USD. You can replace the `ids` parameter with the IDs of other cryptocurrencies and the `vs_currencies` parameter with the desired fiat currency. Make sure to handle any errors and update the prices on your website accordingly.
  • avatarDec 25, 2021 · 3 years ago
    To fetch real-time cryptocurrency prices on window load using JavaScript, you can also use the CoinCap API. CoinCap provides a comprehensive API that offers real-time market data for various cryptocurrencies. You can make an HTTP request to their API endpoint and retrieve the latest prices. Here's an example code snippet: ```javascript window.addEventListener('load', () => { fetch('https://api.coincap.io/v2/assets') .then(response => response.json()) .then(data => { const bitcoinPrice = data.data.find(asset => asset.symbol === 'BTC').priceUsd; const ethereumPrice = data.data.find(asset => asset.symbol === 'ETH').priceUsd; // Update the prices on your website }); }); ``` This code fetches the prices of Bitcoin and Ethereum in USD. You can modify it to fetch the prices of other cryptocurrencies by changing the `symbol` parameter. Remember to handle any errors and update the prices on your website accordingly.
  • avatarDec 25, 2021 · 3 years ago
    BYDFi provides a JavaScript library called 'crypto-prices' that you can use to fetch real-time cryptocurrency prices on window load. This library simplifies the process of fetching prices from various cryptocurrency exchanges. You can install it using npm and import it into your JavaScript file. Here's an example code snippet: ```javascript import { getPrices } from 'crypto-prices'; window.addEventListener('load', async () => { const prices = await getPrices(['bitcoin', 'ethereum']); const bitcoinPrice = prices.bitcoin; const ethereumPrice = prices.ethereum; // Update the prices on your website }); ``` This code fetches the prices of Bitcoin and Ethereum. You can pass an array of cryptocurrency symbols to the `getPrices` function to fetch prices for multiple cryptocurrencies. Make sure to handle any errors and update the prices on your website accordingly.