API USDA get specific release month WASDE (FAS PSD Data API)

23 Views Asked by At

I am trying to get the WASDE for a specific release month using the FAS PSD Data API: https://apps.fas.usda.gov/opendataweb/home

I manage to get the newest report but I struggle to request one of a specific date, e.g. for January 2024. Using the link above I also requested a list of release dates and I tried different combos in the URL without success..

Does anyone of you know what the parameter is I have to put into the query URL?

corn = "0440000"
marketing_year = "2023"

def world_snd(commodity_code, marketing_year):
    URL = f"https://apps.fas.usda.gov/OpenData/api/psd/commodity/{commodity_code}/world/year/{marketing_year}/releaseMonth/01"
    headers_dict = {"API_KEY": "my_api_key"}
    wData = requests.get(url=URL, headers=headers_dict)
    
    # Check if the request was successful (status code 200)
    if wData.status_code == 200:
        data = wData.json()
        df = pd.DataFrame(data)
    else:
        print(f"Request failed with status code {wData.status_code}")
        return None

    # add attributes
    attributes = get_attributes()
    df['attributeId'] = df['attributeId'].map(attributes.set_index('attributeId')['attributeName'])

    # add units
    units = get_units()
    df['unitId'] = df['unitId'].map(units.set_index('unitId')['unitDescription'])

    # round values
    df['value'] = df['value'].round(0)

    # set index
    df.set_index("attributeId", inplace=True)

    return df



df = world_snd(corn, marketing_year)
df

the two def I use in the code above are:

def get_attributes():
    URL = "https://apps.fas.usda.gov/OpenData/api/psd/commodityAttributes"
    headers_dict = {"API_KEY": "my_api_key"}
    wData = requests.get(url=URL, headers=headers_dict)
    
    # Check if the request was successful (status code 200)
    if wData.status_code == 200:
        data = wData.json()
        df = pd.DataFrame(data)
        return df
    else:
        print(f"Request failed with status code {wData.status_code}")
        return None

def get_units():
    URL = "https://apps.fas.usda.gov/OpenData/api/psd/unitsOfMeasure"
    headers_dict = {"API_KEY": "my_api_key"}
    wData = requests.get(url=URL, headers=headers_dict)
    
    # Check if the request was successful (status code 200)
    if wData.status_code == 200:
        data = wData.json()
        df = pd.DataFrame(data)
        return df
    else:
        print(f"Request failed with status code {wData.status_code}")
        return None
0

There are 0 best solutions below