Data & Analytics2025-01-23
FRED API (Federal Reserve Economic Data)
Access 800,000+ economic time series from the Federal Reserve Bank of St. Louis for data analysis and research.
https://fred.stlouisfed.org/docs/api/fred/FRED API
The Federal Reserve Economic Data (FRED) API provides programmatic access to over 800,000 economic time series from 108 sources.
Key Features
Data Coverage
- Macroeconomic Indicators: GDP, inflation, unemployment
- Financial Data: Interest rates, stock indices, exchange rates
- Regional Data: State and MSA-level statistics
- International Data: Economic indicators from multiple countries
API Capabilities
- Free to use with API key
- No rate limits for reasonable use
- Historical data going back decades
- Regular updates (daily, weekly, monthly)
- Multiple data formats (JSON, XML)
Popular Data Series
Key Economic Indicators
- GDP: Gross Domestic Product (GDPC1)
- Unemployment: Civilian Unemployment Rate (UNRATE)
- Inflation: Consumer Price Index (CPIAUCSL)
- Interest Rates: Federal Funds Rate (DFF)
Financial Markets
- S&P 500: Stock Market Index (SP500)
- Treasury Yields: 10-Year Treasury (DGS10)
- Exchange Rates: Dollar Index (DTWEXBGS)
Getting Started
1. Get an API Key
Register for free at https://fred.stlouisfed.org/docs/api/api_key.html
2. Make Your First Request
import requests
import pandas as pd
API_KEY = "your-api-key"
BASE_URL = "https://api.stlouisfed.org/fred/series/observations"
params = {
"series_id": "GDP",
"api_key": API_KEY,
"file_type": "json"
}
response = requests.get(BASE_URL, params=params)
data = response.json()
# Convert to DataFrame
df = pd.DataFrame(data['observations'])
df['date'] = pd.to_datetime(df['date'])
df['value'] = pd.to_numeric(df['value'], errors='coerce')
print(df.head())
Using the fredapi Library
from fredapi import Fred
fred = Fred(api_key='your-api-key')
# Get GDP data
gdp = fred.get_series('GDP')
# Get unemployment rate
unemployment = fred.get_series('UNRATE')
# Search for series
results = fred.search('inflation')
Common Use Cases
Economic Research
- Analyze business cycles
- Study monetary policy impacts
- Compare regional economies
- Research historical trends
Data Science Projects
- Time series forecasting
- Economic indicator modeling
- Causal impact analysis
- Machine learning features
Financial Analysis
- Market trend analysis
- Risk assessment
- Portfolio optimization
- Economic scenario planning
Example: Economic Analysis
import matplotlib.pyplot as plt
from fredapi import Fred
fred = Fred(api_key='your-api-key')
# Get data
unemployment = fred.get_series('UNRATE')
gdp_growth = fred.get_series('A191RL1Q225SBEA')
# Plot
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8))
unemployment.plot(ax=ax1, title='Unemployment Rate')
ax1.set_ylabel('Percent')
gdp_growth.plot(ax=ax2, title='GDP Growth Rate', color='green')
ax2.set_ylabel('Percent')
plt.tight_layout()
plt.show()
Advanced Features
Data Transformations
Apply transformations directly via API:
- Percentage change
- Difference
- Compounded annual rate
- Natural log
params = {
"series_id": "GDP",
"api_key": API_KEY,
"file_type": "json",
"units": "pch" # Percent change
}
Multiple Series
Combine multiple series for analysis:
from fredapi import Fred
import pandas as pd
fred = Fred(api_key='your-api-key')
series_ids = ['GDP', 'UNRATE', 'CPIAUCSL']
data = {}
for series_id in series_ids:
data[series_id] = fred.get_series(series_id)
df = pd.DataFrame(data)
correlation = df.corr()
print(correlation)
Best Practices
- Cache Data: Save frequently used series locally
- Handle Missing Values: Not all series are complete
- Check Frequency: Series have different update frequencies
- Read Documentation: Understand what each series measures
- Attribution: Credit FRED when publishing results
Integration with Analysis Tools
Causal Impact Analysis
from causalimpact import CausalImpact
from fredapi import Fred
fred = Fred(api_key='your-api-key')
# Get data for causal analysis
y = fred.get_series('RETAIL')
x1 = fred.get_series('UNRATE')
x2 = fred.get_series('CPIAUCSL')
data = pd.DataFrame({'y': y, 'x1': x1, 'x2': x2})
# Run causal impact analysis
ci = CausalImpact(data, pre_period, post_period)
print(ci.summary())
Resources
Limitations
- Real-time data may have revisions
- Some series require seasonal adjustment
- International coverage varies by country
- No tick-level financial data