I'm using the Bureau of Labor Statistics (BLS) API to retrieve Consumer Price Index (CPI) data for the CUUR0000SA0 series. My goal is to obtain CPI data from April 2020 to April 2024. However, the API response only includes data starting from January 2022, and I'm unable to retrieve data prior to this date.
Here is the code I am using to make the API request:
import requestsimport jsonimport pandas as pd# ConstantsCPI_URL = "https://api.bls.gov/publicAPI/v2/timeseries/data/"API_KEY = "<<MY API KEY>>"# Function to get CPI data from the APIdef get_cpi_data(start_year, end_year): headers = {"Content-Type": "application/json"} data = json.dumps({"seriesid": ["CUUR0000SA0"],"startyear": str(start_year),"endyear": str(end_year),"registrationkey": API_KEY }) response = requests.post(CPI_URL, headers=headers, data=data) response_json = response.json() # Print the API response for debugging purposes print(json.dumps(response_json, indent=4)) return response_json# Test the function with a range of yearsstart_year = 2020end_year = 2024 try: cpi_data = get_cpi_data(start_year, end_year)except ValueError as e: print(e)The API response includes the following data (truncated here to save space):
{"status": "REQUEST_SUCCEEDED","responseTime": 155,"message": [],"Results": {"series": [ {"seriesID": "CUUR0000SA0","data": [ {"year": "2024","period": "M04","periodName": "April","latest": "true","value": "313.548","footnotes": [ {} ] }, ... {"year": "2022","period": "M01","periodName": "January","value": "281.148","footnotes": [ {} ] } ] } ]}Questions:
Why is the data for months prior to January 2022, including April 2020, not being returned in my API requests?Are there any specific limitations or additional parameters required in the API request to retrieve the desired data?
Some Sources
The BLS API info is here: https://www.bls.gov/developers/api_signature_v2.htm
And a text file of the series going back many decades (but not available beyond March 2023) is here: https://download.bls.gov/pub/time.series/cu/cu.data.1.AllItems