[API] fredr

API & Databases R Courses

Access 670,000 US and international time series through the fredR API.

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
06-01-2020

Database description

The Federal Reserve Economic Data (fredR) provides a complete set of R bindings to the Federal Reserve Economic Data (FRED) RESTful API, provided by the Federal Reserve Bank of St. Louis. The functions gives access to all indicators and allow the user to search for and fetch more than 627 000 series from 89 sources as well as associated metadata within the FRED database.

The Federal Reserve Economic Data : https://fred.stlouisfed.org/

API key

To assess FRED data, you need an API key that is provided by the institution. First, you have to (freely) register to the institution at: https://research.stlouisfed.org/useraccount/register/step1. After providing a valid email adress, you will receive an activation email. Click on it, and you will be forwarded to a control panel.

Click on the Account tab, and select API Keys. Then, click on + Request API Key. After entering details on the use of fredR’s data, you will be provided with a unique API key. Such key will be in the form of:

APIkey <- “abcdefghijklmnopqrstuvwxyz123456”

First, you need to enter your API key in the function fredr_set_key(). This will take the API Key given by fredR and will allow the acces to the database.

# Register API key
library(fredr)
fredr_set_key("abcdefghijklmnopqrstuvwxyz123456")
# A tibble: 1,000 x 16
   id    realtime_start realtime_end title observation_sta…
   <chr> <chr>          <chr>        <chr> <chr>           
 1 DEXU… 2021-02-19     2021-02-19   U.S.… 1999-01-04      
 2 EXUS… 2021-02-19     2021-02-19   U.S.… 1999-01-01      
 3 AEXU… 2021-02-19     2021-02-19   U.S.… 1999-01-01      
 4 DTWE… 2021-02-19     2021-02-19   Trad… 2006-01-02      
 5 TWEX… 2021-02-19     2021-02-19   Trad… 2006-01-01      
 6 TWEX… 2021-02-19     2021-02-19   Trad… 2006-01-01      
 7 DEXC… 2021-02-19     2021-02-19   Chin… 1981-01-02      
 8 EXCH… 2021-02-19     2021-02-19   Chin… 1981-01-01      
 9 AEXC… 2021-02-19     2021-02-19   Chin… 1981-01-01      
10 DTWE… 2021-02-19     2021-02-19   Trad… 1973-01-02      
# … with 990 more rows, and 11 more variables: observation_end <chr>,
#   frequency <chr>, frequency_short <chr>, units <chr>,
#   units_short <chr>, seasonal_adjustment <chr>,
#   seasonal_adjustment_short <chr>, last_updated <chr>,
#   popularity <int>, group_popularity <int>, notes <chr>

Now, with your API key and using the fredR library, you can access data sources.

Functions

The functions listed below allow you to search and download specific data from the fredR database using a key and different parameters. Each of these functions are detailed below and some examples are provided.

By using the search function fredr_search(), you will have all indicators related to a specific query. The function fredr_search() takes as an input an indicator from the fredR database and returns a table containing information related to the input inserted.

For example, it could be interesting to evaluate the exchange rate.

# Using search function 
fredr_series_search_text(search_text = "exchange rate") 
# A tibble: 1,000 x 16
   id    realtime_start realtime_end title observation_sta…
   <chr> <chr>          <chr>        <chr> <chr>           
 1 DEXU… 2021-02-19     2021-02-19   U.S.… 1999-01-04      
 2 EXUS… 2021-02-19     2021-02-19   U.S.… 1999-01-01      
 3 AEXU… 2021-02-19     2021-02-19   U.S.… 1999-01-01      
 4 DTWE… 2021-02-19     2021-02-19   Trad… 2006-01-02      
 5 TWEX… 2021-02-19     2021-02-19   Trad… 2006-01-01      
 6 TWEX… 2021-02-19     2021-02-19   Trad… 2006-01-01      
 7 DEXC… 2021-02-19     2021-02-19   Chin… 1981-01-02      
 8 EXCH… 2021-02-19     2021-02-19   Chin… 1981-01-01      
 9 AEXC… 2021-02-19     2021-02-19   Chin… 1981-01-01      
10 DTWE… 2021-02-19     2021-02-19   Trad… 1973-01-02      
# … with 990 more rows, and 11 more variables: observation_end <chr>,
#   frequency <chr>, frequency_short <chr>, units <chr>,
#   units_short <chr>, seasonal_adjustment <chr>,
#   seasonal_adjustment_short <chr>, last_updated <chr>,
#   popularity <int>, group_popularity <int>, notes <chr>

fredr_observations()

By using the observation function fredr_observations(), you can add parameters in order to evaluate data concerning an input.

For example, it could be interesting to evaluate the exchange rate (input) between Euro and $US from 2000 to 2017 (“EXUSEU” in the previous results table).

# Downloading data concerning exchange rate
dataFred <- fredr_series_observations(series_id = "EXUSEU", 
             observation_start = as.Date("2000-01-01"),
             observation_end = as.Date("2017-01-01"))

tl;dr

# Register API key
library(fredr)
fredr_set_key("abcdefghijklmnopqrstuvwxyz123456")

# Using search function 
fredr_series_search_text(search_text = "exchange rate")

# Downloading data concerning exchange rate
dataFred <- fredr_series_observations(series_id = "EXUSEU", 
             observation_start = as.Date("2000-01-01"),
             observation_end = as.Date("2017-01-01"))

Code learned this week

Command Detail
fredr_set_key() Set the key to allow access to the data
fredr_search() Find data related to an input given
fredr_observations() Find data related to parameters

References

This course uses the fredR package documentation


Citation

For attribution, please cite this work as

Warin (2020, June 1). Thierry Warin, PhD: [API] fredr. Retrieved from https://warin.ca/posts/api-fredr/

BibTeX citation

@misc{warin2020[api],
  author = {Warin, Thierry},
  title = {Thierry Warin, PhD: [API] fredr},
  url = {https://warin.ca/posts/api-fredr/},
  year = {2020}
}