How do I download data from a web API and turn it into a table?
Build a request for a monthly interest-rate series, turn the JSON answer into a pandas Series, and wrap the request in a function with a timeout that stops on error answers.
After this lesson, you can turn a web API’s answer into a series of monthly Treasury yields.
How do I ask for one series and a range of dates?
A web API, an application programming interface, answers a request sent to its address, the endpoint, with the settings as parameters, so a dictionary of parameters names the series, the dates, the answer format and your personal access code. Here the series is GS10, the monthly 10-year US Treasury yield, from January to June 2024, at FRED, the database of the Federal Reserve Bank of St. Louis.
requests, installed with python -m pip install requests, sends web requests, and requests.Request(...).prepare().url shows the address it would send without sending it. os.environ.get("FRED_API_KEY", "YOUR_KEY") reads the access code from an environment variable, as in the lesson on keys and folders, and the preview replaces it with REDACTED, because an error message can show the full address. In the address, ? starts the parameters, = joins a name to its value and & separates the parameters. This lesson runs offline with six made-up observations.
The following box builds the parameters and previews the address.
json.loads reads the JSON text into Python dictionaries and lists, pd.DataFrame(data["observations"]) makes one row per observation, and pd.to_numeric(..., errors="coerce") turns the yields from text into numbers. May’s yield arrives as ".", FRED’s mark for a missing value, so it becomes NaN, and the mean of the other five months is 4.27%.
JSON stores named values inside braces and lists inside square brackets, and every value here is text. The default errors="raise" stops at the dot with a ValueError. Dividing by 100 turns percent into decimals, and .set_axis() labels the yields with their dates. FRED’s monthly GS10 yield is the mean of the month’s daily yields, so a backtest uses it only after the month ends.
The following box reads the answer, converts it and computes the mean.
ValueError
date
2024-01-01 0.0408
2024-02-01 0.0416
2024-03-01 0.0427
2024-04-01 0.0449
2024-05-01 NaN
2024-06-01 0.0436
Name: value, dtype: float64
4.27%
How do I request any series with one function?
get_percent_series sends the request with requests.get, stops on an error answer with .raise_for_status(), and converts the observations as above, so one call returns any FRED series in percent as decimals. timeout=30 stops the wait to connect or between incoming bytes after 30 seconds.
An error answer, such as 404 or 500, can also contain JSON, so reading it as observations would hide the error behind a missing field. columns=["date", "value"] keeps both columns when no rows come back. With a free access code from FRED’s website, get_percent_series("TB3MS", "2024-01-01", "2024-12-01") returns the 3-month Treasury bill rate for 2024.
The following box defines the function without calling it.