Skip to main content
Historical stock price data is essential for backtesting trading strategies, building financial models, and rendering price charts. This guide walks you through fetching OHLCV (open, high, low, close, volume) price data using Python.

Prerequisites

  • Python 3.7+
  • A Financial Datasets API key (sign up here)
  • The requests library (pip install requests)

Step 1: Set Up Authentication

Every API request requires your API key in the X-API-KEY header.
import requests

headers = {
    "X-API-KEY": "your_api_key_here"
}

Step 2: Fetch Daily Stock Prices

Use the Prices API to pull end-of-day prices for any US ticker. You must specify a ticker, interval, start_date, and end_date.
import requests

headers = {
    "X-API-KEY": "your_api_key_here"
}

ticker = "AAPL"
interval = "day"  # options: day, week, month, year
start_date = "2024-01-01"
end_date = "2024-12-31"

url = (
    f"https://api.financialdatasets.ai/prices/"
    f"?ticker={ticker}"
    f"&interval={interval}"
    f"&start_date={start_date}"
    f"&end_date={end_date}"
)

response = requests.get(url, headers=headers)
prices = response.json().get("prices")

for price in prices[:5]:
    print(f"{price['time']}: Open={price['open']}, Close={price['close']}, Volume={price['volume']}")

Step 3: Change the Interval

Switch interval to get weekly, monthly, or yearly aggregated prices:
# Weekly prices
interval = "week"

# Monthly prices
interval = "month"

# Yearly prices
interval = "year"

Step 4: Get a Real-Time Price Snapshot

For the latest price, use the Snapshot API:
url = f"https://api.financialdatasets.ai/prices/snapshot?ticker=AAPL"

response = requests.get(url, headers=headers)
snapshot = response.json().get("snapshot")

print(f"Price: ${snapshot['price']}")

Available Tickers

You can fetch the full list of available tickers:
GET https://api.financialdatasets.ai/prices/tickers/
This returns all 17,000+ tickers with historical price data from NYSE and NASDAQ, going back 20+ years.

Next Steps