Intermediate 20 minutes

Build a Cryptocurrency Price Checker

Check real-time cryptocurrency prices using the Coinbase API. Track Bitcoin, Ethereum, and more.

What You'll Build

A crypto price checker that displays current prices, calculates portfolio values, and tracks multiple cryptocurrencies.

Prerequisites

  • Basic JavaScript knowledge
  • Understanding of REST APIs
  • Familiarity with cryptocurrency terminology
1

Understand Coinbase public endpoints

Coinbase offers public API endpoints for price data that require no authentication. These are perfect for building price trackers.

2

Get a spot price

Fetch the current spot price for any cryptocurrency pair (e.g., BTC-USD, ETH-EUR).

javascript
const response = await fetch('https://api.coinbase.com/v2/prices/BTC-USD/spot');
const { data } = await response.json();

console.log(`${data.base}: $${parseFloat(data.amount).toLocaleString()} ${data.currency}`);
3

Track multiple cryptocurrencies

Fetch prices for multiple coins in parallel using Promise.all.

javascript
const coins = ['BTC', 'ETH', 'SOL', 'DOGE', 'ADA'];

const prices = await Promise.all(
  coins.map(async (coin) => {
    const res = await fetch(`https://api.coinbase.com/v2/prices/${coin}-USD/spot`);
    const { data } = await res.json();
    return { coin: data.base, price: parseFloat(data.amount) };
  })
);

console.log('Crypto Prices (USD):');
prices.forEach(p => {
  console.log(`  ${p.coin}: $${p.price.toLocaleString()}`);
});
4

Get buy and sell prices

Compare the buy and sell prices to see the current spread.

javascript
async function getCryptoPrices(coin, currency = 'USD') {
  const [buyRes, sellRes, spotRes] = await Promise.all([
    fetch(`https://api.coinbase.com/v2/prices/${coin}-${currency}/buy`).then(r => r.json()),
    fetch(`https://api.coinbase.com/v2/prices/${coin}-${currency}/sell`).then(r => r.json()),
    fetch(`https://api.coinbase.com/v2/prices/${coin}-${currency}/spot`).then(r => r.json())
  ]);

  const buy = parseFloat(buyRes.data.amount);
  const sell = parseFloat(sellRes.data.amount);
  const spot = parseFloat(spotRes.data.amount);
  const spread = ((buy - sell) / spot * 100).toFixed(2);

  console.log(`${coin}/${currency}:`);
  console.log(`  Buy:  $${buy.toLocaleString()}`);
  console.log(`  Sell: $${sell.toLocaleString()}`);
  console.log(`  Spot: $${spot.toLocaleString()}`);
  console.log(`  Spread: ${spread}%`);
}

await getCryptoPrices('BTC');
await getCryptoPrices('ETH');
5

Calculate portfolio value

Build a simple portfolio calculator that shows the total value of your crypto holdings.

javascript
// Define your portfolio
const portfolio = [
  { coin: 'BTC', amount: 0.5 },
  { coin: 'ETH', amount: 3.0 },
  { coin: 'SOL', amount: 20 }
];

// Fetch current prices
const values = await Promise.all(
  portfolio.map(async (holding) => {
    const res = await fetch(`https://api.coinbase.com/v2/prices/${holding.coin}-USD/spot`);
    const { data } = await res.json();
    const price = parseFloat(data.amount);
    const value = price * holding.amount;
    return { ...holding, price, value };
  })
);

// Display portfolio
console.log('Portfolio Summary:');
let total = 0;
values.forEach(v => {
  console.log(`  ${v.amount} ${v.coin} @ $${v.price.toLocaleString()} = $${v.value.toLocaleString()}`);
  total += v.value;
});
console.log(`\nTotal Value: $${total.toLocaleString()}`);

Next Steps

  • Add price alerts for specific thresholds
  • Build a chart showing 24-hour price history
  • Add support for different fiat currencies (EUR, GBP)