from google.colab import userdata
= userdata.get("ALPHAVANTAGE_API_KEY") API_KEY
35 The AlphaVantage API
As mentioned, the AlphaVantage API provides a wealth of financial and economic datasets.
35.1 Setup
To make requests for this data, we first need to obtain an API key. It is possible to obtain your own key using their sign up form, or just use one of the prof’s “premium keys”.
Before moving on, set this credential it as a notebook secret called ALPHAVANTAGE_API_KEY
.
Reading the credential from notebook secrets:
35.2 Example: Unemployment Data
For these examples, we will fetch some historical unemployment data.
Before we start it will be necessary to read the documentation for this endpoint:
https://www.alphavantage.co/documentation/#unemployment
After reading the docs, we see we are able to request this data in either JSON or CSV format. We will start by fetching this data in JSON format, and then also alternatively demonstrate how to fetch the data in CSV format.
35.2.1 JSON Format
Fetching the data in JSON format:
import requests
#url = "https://www.alphavantage.co/query?function=UNEMPLOYMENT&apikey=demo"
= f"https://www.alphavantage.co/query?function=UNEMPLOYMENT&apikey={API_KEY}"
url
= requests.get(url)
response response
<Response [200]>
Investigating and parsing the nested structure:
= response.json()
parsed_response print(type(parsed_response))
parsed_response.keys()
<class 'dict'>
dict_keys(['name', 'interval', 'unit', 'data'])
print("NAME:", parsed_response["name"])
print("INTERVAL:", parsed_response["interval"])
print("UNIT:", parsed_response["unit"])
NAME: Unemployment Rate
INTERVAL: monthly
UNIT: percent
= parsed_response["data"]
data print(type(data))
len(data)
<class 'list'>
931
We see a list of around a thousand data points. Each one is a dictionary that looks like this:
0] data[
{'date': '2025-07-01', 'value': '4.2'}
The unemployment rate value is a string, but we need it to be numeric, so we take a moment to make a clean version of the data:
= []
clean_data for item in data:
"date": item["date"], "rate": float(item["value"])})
clean_data.append({
0] clean_data[
{'date': '2025-07-01', 'rate': 4.2}
Now we are able to perform arithmetic calculations with the data, and also chart it properly.
Making a line chart of the data over time:
import plotly.express as px
= [item["date"] for item in clean_data]
dates = [item["rate"] for item in clean_data]
rates
= px.line(x=dates, y=rates, height=350,
fig =f"US Unemployment Rates ({parsed_response['interval']})",
title={"x": "Date", "y": "Unemployment Rate (%)"}
labels
) fig.show()
35.2.2 CSV Format
After reading the docs, we learn that in order to fetch the data in CSV format, we need to append &datatype=csv
to the end of the request URL.
Fetching the data in CSV format:
from pandas import read_csv
= f"https://www.alphavantage.co/query?function=UNEMPLOYMENT&apikey={API_KEY}"
url += "&datatype=csv"
url
= read_csv(url)
df ={"timestamp": "date", "value": "rate"}, inplace=True)
df.rename(columns df.head()
date | rate | |
---|---|---|
0 | 2025-07-01 | 4.2 |
1 | 2025-06-01 | 4.1 |
2 | 2025-05-01 | 4.2 |
3 | 2025-04-01 | 4.2 |
4 | 2025-03-01 | 4.2 |
This gives us a pandas
DataFrame
datatype. It doesn’t contain some of the same context as the JSON formatted response (like the interval), but the data is already in a clean format with numeric rates.
Making a line chart of the data over time:
import plotly.express as px
= px.line(x=df["date"], y=df["rate"], height=350,
fig =f"US Unemployment Rates (monthly)",
title={"x": "Date", "y": "Unemployment Rate (%)"}
labels
) fig.show()
Alright, that’s it!
With these examples complete, now you should feel confident in your ability to fetch and process any of the other datasets from the API.