FRED API Client
fredapi is a Python API for Federal Reserve Economic Data (FRED) from the St. Louis Fed, and also provides methods for analyzing point-in-time data from ALFRED. It leverages pandas for data output, returning data as Series or DataFrames. The current version is 0.5.2, with a release cadence of a few updates per year, primarily addressing Python and dependency compatibility.
Warnings
- gotcha An API key is mandatory for all requests. You can obtain one for free from the FRED website. Ensure it's set as the 'FRED_API_KEY' environment variable or passed directly to the `Fred` constructor.
- breaking Older versions of fredapi (<0.5.1) used `DataFrame.append`, which is deprecated in newer pandas versions. Using such old fredapi versions with modern pandas will lead to `FutureWarning`s or errors.
- breaking Versions of fredapi prior to 0.3.7 used HTTP for API calls. FRED's API primarily uses HTTPS. Using very old versions might result in connection errors if FRED no longer supports HTTP for certain endpoints.
Install
-
pip install fredapi
Imports
- Fred
from fredapi import Fred
Quickstart
import os
from fredapi import Fred
# Get a FRED API key from https://fred.stlouisfed.org/docs/api/api_key.html
# Set it as an environment variable or pass directly.
api_key = os.environ.get('FRED_API_KEY', 'YOUR_FRED_API_KEY')
if not api_key or api_key == 'YOUR_FRED_API_KEY':
print("Warning: FRED_API_KEY environment variable not set or placeholder used. Get a key from fred.stlouisfed.org.")
# Using a dummy key to allow the example to run without immediate error,
# but API calls will fail without a valid key.
api_key = 'abcdefghijklmnopqrstuvwxyz123456'
fred = Fred(api_key=api_key)
# Fetch a specific economic data series (e.g., S&P 500)
data = fred.get_series('SP500')
print("Fetched S&P 500 data (last 5 entries):")
print(data.tail())
# Search for data series
search_results = fred.search('unemployment rate')
print("\nSearch results for 'unemployment rate' (first 3):")
print(search_results[['id', 'title']].head(3))