I'm a beginner and I'm getting a 422 error when running a code to extract data from an API endpoint. I Googled the code and realized it's an (Unprocessable Entity) status code, but I'm not sure how to fix it.
The documentation for the API is right here: https://github.com/fedspendingtransparency/usaspending-api/blob/master/usaspending_api/api_contracts/contracts/v2/search/spending_by_award.md
Can anyone please let me know how to modify my code?
import requests
url = "https://api.usaspending.gov"
endpoint = "/api/v2/search/spending_by_award"
criteria = {
"filters": {
"award_type_codes": ["10"],
"agencies": [
{
"type": "awarding",
"tier": "toptier",
"name": "Social Security Administration"
},
{
"type": "awarding",
"tier": "subtier",
"name": "Social Security Administration"
}
],
"legal_entities": [779928],
"recipient_scope": "domestic",
"recipient_locations": [650597],
"recipient_type_names": ["Individual"],
"place_of_performance_scope": "domestic",
"place_of_performance_locations": [60323],
"award_amounts": [
{
"lower_bound": 1500000.00,
"upper_bound": 1600000.00
}
],
"award_ids": [1018950]
},
"fields": ["Award ID", "Recipient Name", "Start Date", "End Date", "Award Amount", "Awarding Agency", "Awarding Sub Agency", "Award Type", "Funding Agency", "Funding Sub Agency"],
"sort": "Recipient Name",
"order": "desc"
}
response = requests.post(f"{url}{endpoint}", params=criteria)
print(response.status_code)
You may modify the data type of several fields, i.e., the award_ids
should be a array[string]
, recipient_locations
consists of array[LocationObject]
For a working example:
import requests
import json
url = "https://api.usaspending.gov/api/v2/search/spending_by_award"
payload = json.dumps({
"filters": {
"award_type_codes": [
"10"
],
"agencies": [
{
"type": "awarding",
"tier": "toptier",
"name": "Social Security Administration"
},
{
"type": "awarding",
"tier": "subtier",
"name": "Social Security Administration"
}
],
"legal_entities": [
779928
],
"recipient_scope": "domestic",
"recipient_type_names": [
"Individual"
],
"place_of_performance_scope": "domestic",
"award_amounts": [
{
"lower_bound": 1500000,
"upper_bound": 1600000
}
],
"award_ids": [
"1018950"
]
},
"fields": [
"Award ID",
"Recipient Name",
"Start Date",
"End Date",
"Award Amount",
"Awarding Agency",
"Awarding Sub Agency",
"Award Type",
"Funding Agency",
"Funding Sub Agency"
],
"sort": "Recipient Name",
"order": "desc"
})
headers = {
'Content-Type': 'application/json',
'Cookie': 'cookiesession1=678A3E0DCDEFGHIJKLNOPQRSTUV08936'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
print(response.status_code)
Result:
{
"limit": 10,
"results": [],
"page_metadata": {
"page": 1,
"hasNext": false,
"last_record_unique_id": null,
"last_record_sort_value": "None"
},
"messages": [
[
"For searches, time period start and end dates are currently limited to an earliest date of 2007-10-01. For data going back to 2000-10-01, use either the Custom Award Download feature on the website or one of our download or bulk_download API endpoints as listed on https://api.usaspending.gov/docs/endpoints."
]
]
}