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()
0 |
2024-12-01 |
4.1 |
1 |
2024-11-01 |
4.2 |
2 |
2024-10-01 |
4.1 |
3 |
2024-09-01 |
4.1 |
4 |
2024-08-01 |
4.2 |
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()
0 |
2025-01-17 |
859.790 |
865.0000 |
852.130 |
858.10 |
858.10 |
3617565 |
0.0 |
1.0 |
1 |
2025-01-16 |
860.960 |
868.9800 |
842.020 |
842.37 |
842.37 |
4028537 |
0.0 |
1.0 |
2 |
2025-01-15 |
836.440 |
850.8400 |
830.410 |
848.26 |
848.26 |
3181383 |
0.0 |
1.0 |
3 |
2025-01-14 |
843.200 |
844.8925 |
823.519 |
828.40 |
828.40 |
3037650 |
0.0 |
1.0 |
4 |
2025-01-13 |
831.525 |
847.0700 |
829.140 |
840.29 |
840.29 |
3024517 |
0.0 |
1.0 |