Skip to content

Commit 842799b

Browse files
authored
Create LiveCryptoRate.js
i forgot to add html file there should be a html file with just one line code <div id="btcPrice">Fetching BTC Price...</div> If you're making this request from a frontend application, ensure the API supports CORS. If not, you might need to route the request through your server or use a proxy.
1 parent 2da7496 commit 842799b

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

LiveCryptoRate.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Define the API endpoint
2+
const apiEndpoint = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd";
3+
4+
// Function to fetch and display the BTC price
5+
function fetchBTCPrice() {
6+
fetch(apiEndpoint)
7+
.then(response => response.json())
8+
.then(data => {
9+
const btcPrice = data.bitcoin.usd;
10+
console.log(`Current BTC Price: $${btcPrice}`);
11+
// You can also update this on your webpage, e.g.:
12+
// document.getElementById("btcPrice").innerText = `Current BTC Price: $${btcPrice}`;
13+
})
14+
.catch(error => {
15+
console.error("Error fetching BTC price:", error);
16+
});
17+
}
18+
19+
// Call the function to fetch and display the price
20+
fetchBTCPrice();

0 commit comments

Comments
 (0)