Beginner 15 minutes

Fetch Stock Prices with Alpha Vantage

Learn to retrieve real-time and historical stock market data. Build a simple stock price checker in 15 minutes.

What You'll Build

A script that fetches the latest stock price for any ticker symbol and shows daily price history.

Prerequisites

  • Basic JavaScript knowledge
  • Understanding of JSON data format
1

Get your free API key

Visit alphavantage.co and click 'Get your free API key'. The free tier allows 25 requests per day and 5 per minute.

2

Fetch a stock quote

Use the GLOBAL_QUOTE function to get the latest price for a stock symbol.

javascript
const symbol = 'AAPL';
const apiKey = 'YOUR_API_KEY';

const response = await fetch(
  `https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${symbol}&apikey=${apiKey}`
);
const data = await response.json();
const quote = data['Global Quote'];

console.log(`Symbol: ${quote['01. symbol']}`);
console.log(`Price: $${parseFloat(quote['05. price']).toFixed(2)}`);
console.log(`Change: ${quote['10. change percent']}`);
3

Get daily price history

Fetch the TIME_SERIES_DAILY data to see how the stock has performed over recent days.

javascript
const response = await fetch(
  `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=AAPL&apikey=YOUR_API_KEY`
);
const data = await response.json();
const timeSeries = data['Time Series (Daily)'];

// Show last 5 days
const days = Object.entries(timeSeries).slice(0, 5);
days.forEach(([date, values]) => {
  console.log(`${date}: Open $${values['1. open']}, Close $${values['4. close']}`);
});
4

Calculate price change

Compare today's price to a week ago to calculate the weekly change percentage.

javascript
async function getWeeklyChange(symbol) {
  const response = await fetch(
    `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${symbol}&apikey=YOUR_API_KEY`
  );
  const data = await response.json();
  const days = Object.values(data['Time Series (Daily)']);

  const today = parseFloat(days[0]['4. close']);
  const weekAgo = parseFloat(days[4]['4. close']);
  const change = ((today - weekAgo) / weekAgo * 100).toFixed(2);

  console.log(`${symbol}: $${today.toFixed(2)} (${change >= 0 ? '+' : ''}${change}% this week)`);
}

await getWeeklyChange('AAPL');

Next Steps

  • Try different stock symbols (MSFT, GOOGL, TSLA)
  • Add intraday data for minute-by-minute prices
  • Build a chart using Chart.js to visualize price history