How to Build a Real-Time Crypto Price Tracker for Any Coin: A Coding Guide for Crypto and Web Developers

Ankit Vagabond
By
Ankit Vagabond
Editor in Chief
Beyond his commitment to technology journalism, Ankit is a joyful gymgoer who believes in maintaining a balanced lifestyle.
5 Min Read
Disclosure: This website may contain affiliate links, which means we may earn a commission if you click on the link and make a purchase. We only recommend products or services that we personally use and believe will add value to my readers. Your support is appreciated!
Getting your Trinity Audio player ready...

Cryptocurrency markets are dynamic and fast-moving, with thousands of coins traded across various exchanges. For websites and apps catering to crypto enthusiasts, offering real-time price data for any cryptocurrency is a game-changer in engagement and user retention. This blog post will guide you through creating a live price tracker that can fetch and display the latest prices of any cryptocurrency, using a flexible API, JavaScript, and Chart.js for beautiful visualizations.

- Advertisement -

Why a Multi-Coin Real-Time Price Tracker Matters

  • Versatility: Users can check prices for multiple coins, not just the popular ones.
  • Higher Engagement: Broad coverage attracts a wider crypto audience.
  • SEO Benefits: Targets diverse keywords like “Bitcoin live price,” “Ethereum price tracker,” “altcoin prices,” and more.
  • Developer Appeal: Coding tutorials for flexible, scalable solutions attract programmers and tech enthusiasts.

Step 1: Choosing the Right API

For tracking any cryptocurrency, CoinGecko’s free API is ideal due to:

  • No API key requirement.
  • Wide crypto coin coverage (over 13,000 coins).
  • Real-time price, market cap, volume, and more.
  • Support for multiple fiat currencies.

API endpoint example to fetch price for any coin:

- Advertisement -
texthttps://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd,inr

Replace {coin_id} with the specific coin’s id like bitcoin, ethereum, or dogecoin.


Step 2: Basic HTML Structure

Create a form for users to input the coin they want to track plus a container for showing the live price and chart:

xml<label for="coinInput">Enter Coin ID (e.g., bitcoin, ethereum):</label>
<input type="text" id="coinInput" placeholder="bitcoin" />
<button id="trackBtn">Track Coin</button>

<div id="price-container" style="font-size: 24px; font-weight: bold; margin-top: 10px;">
  Enter a coin to start tracking.
</div>
<canvas id="priceChart" width="600" height="300"></canvas>

Step 3: Adding Chart.js Library

Include Chart.js via CDN:

xml<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

Step 4: JavaScript: Fetch & Display Any Coin’s Price Real-Time

This script enables live tracking of user-specified coins, updating price and chart every minute.

javascriptconst ctx = document.getElementById('priceChart').getContext('2d');
let priceData = [];
let timeLabels = [];
let currentCoin = 'bitcoin'; // default coin

const priceChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: timeLabels,
    datasets: [{
      label: 'Price (USD)',
      data: priceData,
      fill: false,
      borderColor: 'rgba(99, 132, 255, 1)',
      backgroundColor: 'rgba(99, 132, 255, 0.2)',
      tension: 0.2,
      pointRadius: 2,
    }]
  },
  options: {
    responsive: true,
    animation: false,
    scales: {
      x: { title: { display: true, text: 'Time (HH:mm)' } },
      y: { title: { display: true, text: 'Price in USD' }, beginAtZero: false }
    }
  }
});

async function fetchCoinPrice(coin) {
  try {
    const response = await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=usd`);
    if (!response.ok) throw new Error('Coin not found or API limit reached');
    const data = await response.json();
    if (!data[coin]) throw new Error('Invalid coin id');
    const priceUSD = data[coin].usd;

    const now = new Date();
    const timeLabel = now.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit' });

    document.getElementById('price-container').innerHTML = `${coin.charAt(0).toUpperCase() + coin.slice(1)} Price: $${priceUSD.toLocaleString()}`;

    priceData.push(priceUSD);
    timeLabels.push(timeLabel);

    if (priceData.length > 30) {
      priceData.shift();
      timeLabels.shift();
    }

    priceChart.update();
  } catch (error) {
    document.getElementById('price-container').textContent = `Error: ${error.message}`;
    console.error('Fetch error:', error);
  }
}

// Initial fetch for default coin
fetchCoinPrice(currentCoin);
let intervalId = setInterval(() => fetchCoinPrice(currentCoin), 60000);

document.getElementById('trackBtn').addEventListener('click', () => {
  const coinInput = document.getElementById('coinInput').value.toLowerCase().trim();
  if (!coinInput) return;
  
  // Reset data for new coin
  priceData = [];
  timeLabels = [];
  priceChart.data.labels = timeLabels;
  priceChart.data.datasets[0].data = priceData;

  currentCoin = coinInput;
  fetchCoinPrice(currentCoin);

  if (intervalId) clearInterval(intervalId);
  intervalId = setInterval(() => fetchCoinPrice(currentCoin), 60000);
});

Step 5: SEO and User Engagement Tips

  • Optimize meta tags with keywords like “live cryptocurrency prices,” “track any crypto coin,” and “real-time crypto charts.”
  • Provide educational content on how to use the tracker and understand crypto market concepts.
  • Enable social sharing and user comments to boost activity and enhance SEO ranking.
  • Consider adding alerts or favorites in future versions to deepen engagement.

Conclusion

Building a flexible, real-time cryptocurrency price tracker that supports any coin is a powerful feature to add to crypto websites or blogs. The combination of CoinGecko’s vast API, interactive Chart.js visualizations, and straightforward JavaScript makes it accessible for developers of all levels.

This solution boosts traffic by targeting a wide user base interested in diverse cryptocurrencies, while also serving as valuable content for the coding community. Integrate this tracker today to make your website a hub for live crypto data and coding knowledge!

- Advertisement -

About the Author

Beyond his commitment to technology journalism, Ankit is a joyful gymgoer who believes in maintaining a balanced lifestyle.

Share This Article
Leave a Comment