See it on GitHub
Beta

Treasury.io

A daily data feed for the U.S. Treasury

$ About

⚠️ Under renovation ⚠️

Hey there! We're in the process of porting this website over to Engima's instance of this database and revamping our query builder, if possible. In the meantime, you can access the data directly via their query interface here: https://public.enigma.com/browse/1ee6ce88-f5f8-4a2c-8190-445739beea31

The data

Every day at 4pm, the United States Treasury publishes data tables summarizing the cash spending, deposits, and borrowing of the Federal government. These files catalog all the money taken in that day from taxes, the programs, and how much debt the government took out to make it happen. It comes from a section of the U.S. Treasury called the Bureau of the Fiscal Service

At a time of record fiscal deficits and continual debates over spending, taxation, and the debt, this daily accounting of our government's main checking account is an essential data point that the public should have ready access to.

Original data format

The Treasury told us in response to a Freedom of Information Act Request that it does not store this data in any format other than inconsistently structured text files that don't lend themselves to programmatic analysis.

New data format

We have created the first-ever electronically-searchable database of the Federal government's daily cash spending and borrowing. It updates daily and the data can be exported in various formats and loaded into a variety of systems. It is also 100% open source and free to use. We started the project at the Columbia and Stanford Bi-Coastal Datafest Hackathon and continued the it with support from a Knight-Mozilla OpenNews Code Sprint Grant.

We want to make it easy for people to search, explore, and visualize how the government spends their tax dollars.

$ Documentation

We created a fully-documented data dictionary that details the structure of these tables as well as descriptions of many programs and classifications. If you're the kind to prefer your data dictionaries in hard-copies, here's the full documentation as a PDF. We also have an FAQ or any other questions you might have.

The data is complicated, however, and we recommend that you reach out to the Bureau of the Fiscal Service at the U.S. Treasury directly via that page or at DTS.Questions@fms.treas.gov. Note that the units of the data are in millions e.g. 1000 equals $1 billion.

Always query responsibly.

This API provides eight tables. You can access them with the short names in parentheses. Click "preview" to download the first ten rows of the table as CSV, and click "full" to download the full table as CSV.

You can also download the full SQLite3 database, with all of the tables. It's about 50mb in total.

To see the table schema and column types, you can use this query:

http://api.treasury.io/cc7znvq/47d80ae900e04f2/sql/?q=SELECT "name","sql" FROM sqlite_master

$ Usage

The eight tables are stored in one SQLite database and the API. To get the data back as JSON, query it from a URL like this:

http://api.treasury.io/cc7znvq/47d80ae900e04f2/sql/?q=YOUR_QUERY

where YOUR_QUERY is a URL-encoded SQL command. This query will show the ten most recent withdrawals.

http://api.treasury.io/cc7znvq/47d80ae900e04f2/sql/?q=SELECT * FROM t2 WHERE transaction_type = 'withdrawal' ORDER BY date DESC LIMIT 10

Note: We're submitting a plain-text query string because the browser is doing the URL-encoding here. The libraries we've written either do that for you or expect it a URL-encoded query, as shown in the examples.

Host it yourself

If you want to install the database locally, follow the instructions on the GitHub page

Citation

You can cite this data as "U.S. Treasury data via Treasury.io." Or, with links:

<a href="http://www.fms.treas.gov/index.html" target="_blank">U.S. Treasury data</a> via <a href="http://treasury.io/" target="_blank">Treasury.io</a>.

$ Libraries

  • Python

    Install with pip.

    pip install treasuryio

    Alternatively, copy the query function from here.

    Send SQL, and get a pandas DataFrame.

    import treasuryio
    treasuryio.query('SELECT * FROM sqlite_master')

    This package lives here.

  • R

    library('devtools')
    install_github('Rtreasuryio', 'csvsoundsystem')
    library('Rtreasuryio')

    Alternatively, copy the treasuryio function from here.

    Send SQL, and get a data.frame.

    treasuryio('SELECT * FROM sqlite_master')

    This package lives here.

  • JavaScript

    There isn't a separate library for JavaScript (if you want one, let us know), you can just call it through jQuery's $.ajax().

    Include jQuery :

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

    Create a function treasuryIo() that holds the API endpoint and accepts a query string. Pass it a query and get the JSON response.

    function treasuryIo(query){
      return $.ajax({
        url: 'http://api.treasury.io/cc7znvq/47d80ae900e04f2/sql/?q='+query
      })
    }
    treasuryIo('SELECT * FROM t1 LIMIT 10')
      .done(function(response){
        console.log(response);
      }).fail(function(err){
        console.log(err)
      });

    If you want a CSV response instead of JSON, you can include the dsv.js library, which is the CSV parser and formatter from d3.js.

    <script src="js/thirdparty/dsv.min.js"></script>

    Then, when you call treasuryIo(), convert into a CSV like so :

    treasuryIo('SELECT * FROM t1 LIMIT 10')
      .done(function(response){
        var csv_response = dsv.csv.format(response);
        console.log(csv_response);
      }).fail(function(err){
        console.log(err)
      });
  • Node.js

    Install with npm

    npm install treasuryio

    Send SQL, and get a csv or json representation of the data, depending on what string you pass as the second argument.

    var treasuryio = require('treasuryio')
    treasuryio('SELECT * FROM t1 LIMIT 10', 'csv', function(err, response){
        if (err) console.log(err)
        console.log(response);
    });

    This module lives here.

  • Ruby

    Install the gem

    gem install treasuryio

    Send SQL, and get a list of hashes.

    require 'treasuryio'
    puts (treasuryio 'SELECT * FROM t1 LIMIT 10')

    This module lives here.

  • Google Docs

    Paste this function into the script editor. (Or wait a bit; we've submitted it to the script gallery.)

    function treasuryio(sql) {
      var response = UrlFetchApp.fetch("http://api.treasury.io/cc7znvq/47d80ae900e04f2/sql/?q=" + encodeURIComponent(sql));
      var data = JSON.parse(response.getContentText());
    
      if (data.length == 0){
        return [];
      } else {
        // Unfortunately, this is ordered arbitrarily and is not guaranteed to be consistant.
        var keys = Object.keys(data[0]);
        return [keys].concat(data.map(function(row){return keys.map(function(key){return row[key]})}))
      }
    }

    Then use it like so

    =treasuryio("SELECT * FROM t1 limit 10")

    Here's an example.

$ The Robot

The twitter account @TreasuryIO is a python Twitter bot we made that tweets out daily analyses of the data.

It runs SQL queries on the database and then turns the results into tweetable messages. You can make your own bot by using our PyTreasuryIO package on GitHub, which includes instructions on how to make your own bot based on a SQL query of your choosing. The example bot calculates the total outstanding federal debt.

Some sample tweets the bot makes:

  • The distance to the debt ceiling.
  • Notifications of income from taxes.
  • How much the money spent on different programs like:
    • Food Stamps
    • Veterans Affairs Benefits Programs
    • Medicare

$ Contact

For help with accessing the API or running it yourself, contact us via any of these means.

You should also contact us if you've done something cool with the API because we'd love to hear about it.

$ Testimonials

.@TreasuryIO is just awesome. Simple to use, clear docs, data dictionary, client libraries and code on @GitHub. #opendata done right.

Mark Headd

Well [this] is literally bad ass as fuck. I didn't even know that data was publicly available!

TanyIshsar