Use of BLS API

Author

Xiaoyi Zhao

Published

May 28, 2025

Setup

knitr::opts_chunk$set(echo = TRUE)

Use the BLS API to retrieve the data

# Set the library and install the packages
library(devtools)
library(blsR)

# The introduction about this package: https://cran.r-project.org/web/packages/blsR/blsR.pdf
devtools::install_github("groditi/blsR")

# Acquire the API by registering through https://www.bls.gov/developers/home.htm
bls_set_key("your personal API")

test_series <- get_series(series_id = "series id you found", 
                         start_year = 2016, 
                         end_year = 2024, 
                         api_key = bls_get_key())

I work through the series number by https://data.bls.gov/PDQWeb/la, and we discover that the series number for each county is “FCN+Fips code+00000000”

ACS API

# Set the library and install the packages
library(censusapi)
library(tidycensus) 

# Acquire the API by registering through https://api.census.gov/data/key_signup.html

census_api_key("your api key", install = TRUE, overwrite = TRUE)
# Acquire the API by registering through https://api.census.gov/data/key_signup.html

#The main function to retrive the data here are https://cran.r-project.org/web/packages/tidycensus/tidycensus.pdf

#to check the name of the variable
v15 <- load_variables(2015, "acs5", cache = TRUE)
View(v15)

# for example, if I want to know the age
# here is the explanation of the codebook: https://data.census.gov/table/ACSDT5Y2022.B01001

nc_acs_2015 <- get_acs(geography = "county", 
              year = 2015,
              variables = c(age = "B01001A_003"), 
              state = "NC",
              survey = "acs5",
              output = "wide")