Appendix D — The AlphaVantage API

The AlphaVantage API is a wealth of financial and market datasets.

For more information, see the APIs tutorial.

After obtaining an API key, and setting the API Key as a notebook secret, we are able to access the API key in a secure way:

from google.colab import userdata

API_KEY = userdata.get("ALPHAVANTAGE_API_KEY")

Then we supply the API Key value along with our requests.

For example, requesting unemployment rates:

from pandas import read_csv

request_url = f"https://www.alphavantage.co/query?function=UNEMPLOYMENT&apikey={API_KEY}&datatype=csv"
df = read_csv(request_url)
df.head()
timestamp value
0 2024-11-01 4.2
1 2024-10-01 4.1
2 2024-09-01 4.1
3 2024-08-01 4.2
4 2024-07-01 4.3

Requesting stock prices:

from pandas import read_csv

symbol = "NFLX"
request_url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol={symbol}&apikey={API_KEY}&datatype=csv"
df = read_csv(request_url)
df.head()
timestamp open high low close adjusted_close volume dividend_amount split_coefficient
0 2025-01-03 893.13 898.8300 879.8901 881.05 881.05 2970019 0.0 1.0
1 2025-01-02 895.50 898.5800 877.0000 886.73 886.73 2315685 0.0 1.0
2 2024-12-31 901.80 902.6800 889.4700 891.32 891.32 1875897 0.0 1.0
3 2024-12-30 894.51 908.2299 889.7100 900.43 900.43 2202970 0.0 1.0
4 2024-12-27 916.01 918.1300 894.5000 907.55 907.55 3226158 0.0 1.0