Skip to contents

Make a call to the USASpending API

Usage

usasp(
  endpoint,
  ...,
  .method = "GET",
  .api_url = "https://api.usaspending.gov",
  .api_version = "v2",
  .page_all = FALSE,
  .limit = 10,
  .as_tibble = TRUE,
  .flatten = FALSE
)

Arguments

endpoint

API endpoint to call

...

Name-value pairs giving API parameters

.method

HTTP method to use ("GET" or "POST")

.api_url

The base URL for the API

.api_version

The API version to use

.page_all

Logical, whether to automatically fetch all pages

.limit

Number of results per page (max 100)

.as_tibble

Logical, whether to return response as a tibble

.flatten

Logical, whether to flatten nested structures in tibble output

Value

Response from the API, either as a tibble or raw response

Examples

if (FALSE) { # interactive()
# Get single page of results as a tibble
usasp("/agency/awards/count", fiscal_year = 2023)

# Get all pages of flattened results
usasp("/agency/awards/count", fiscal_year = 2023, 
      .page_all = TRUE, .flatten = TRUE)
      
# Send POST request with request body
usasp("/search/spending_by_award",
      filters = list(
        award_type = c("contracts"),
        time_period = list(list(
          start_date = "2023-01-01",
          end_date = "2023-12-31"
        ))
      ),
      fields = c("recipient_name", "total_obligation"),
      .method = "POST")
      
# Get raw JSON response
usasp("/agency/awards/count", fiscal_year = 2023, .as_tibble = FALSE)
}