-1

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)

1 Answers1

0

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."
        ]
    ]
}


Dharman
  • 30,962
  • 25
  • 85
  • 135
Tony
  • 557
  • 1
  • 4
  • 17
  • Thank you so much for the quick response. The code is running now! Only downside is that I'm trying to get the data from that site to report in pandas but the only response I'm getting after running the code is this: {"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, etc. Sorry if this is far fetched question but any idea how to get the full data records for the org? – user16022131 May 29 '21 at 04:00
  • The ```"results":[]``` means there is no data with your current parameters, you may try another combined parameters – Tony May 29 '21 at 04:03
  • Oh I understand now, I'll try changing the parameter and hopefully it'll work! You're the best! – user16022131 May 29 '21 at 04:08