USAspending API ReferenceIndependent
Try the API

How Advanced Search works

The filter panel, the charts, the results table, and the shareable URL, call by call.

Advanced Search is the busiest page on usaspending.gov and the best map of the search API. Everything on it is driven by one filter object. The page builds that object as you pick filters, then sends the same object to several endpoints at once: one for each chart and one for each results tab.

The pattern to copy

  1. Build a single filters object. The filter object page covers every key.
  2. Ask for counts first with spending_by_award_count. The site uses the counts to label its tabs and skip empty ones.
  3. Request results one award group at a time with spending_by_award, since contracts, grants, and loans return different columns.
  4. Send the same filters to spending_over_time, spending_by_category, and spending_by_geography for charts.

A search URL on the site ends in a short hash. When you share a search, the page stores the filter object with references/filter and gets the hash back. Opening that URL later calls references/hash to turn it back into filters. You can use the same pair to save searches in your own app.

The calls, in order

  1. On load, if the URL has a search hash

    POSTRestore Filters From URL Hash/api/v2/references/hash/

    Turns the hash back into the saved filter object.

  2. On load

    GETAwards Last Updated/api/v2/awards/last_updated/

    The "data as of" date above the results.

  3. While typing in the Agency filter

    POSTAwarding Agency Autocomplete/api/v2/autocomplete/awarding_agency/
  4. While typing in the Recipient filter

    POSTRecipient Autocomplete/api/v2/autocomplete/recipient/
  5. When the Product and Service Code tree opens

    GETToptier Search/api/v2/references/filter_tree/psc/

    Each level of the tree is its own call.

  6. After filters are applied

    POSTNumber of awards for each type/api/v2/search/spending_by_award_count/

    The counts on the Contracts, Grants, Loans, and other tabs.

  7. For the results table

    POSTSpending by Award/api/v2/search/spending_by_award/

    One call per tab, one page at a time.

  8. For the Time chart

    POSTSpending Over Time/api/v2/search/spending_over_time/
  9. For the Categories chart

    POSTSpending By Category/api/v2/search/spending_by_category/

    The site calls a category-specific path, such as spending_by_category/awarding_agency/.

  10. For the Map

    POSTSpending by Geography/api/v2/search/spending_by_geography/
  11. When you share or bookmark a search

    POSTGenerated Filter Hash for URL/api/v2/references/filter/

    Stores the filter object and returns the hash for the URL.

Sources

Traced from these files in the public usaspending-website repo:

Next: How an agency profile is built

Loading…