DEV Community

Alex Spinov
Alex Spinov

Posted on

US Treasury API: Track the National Debt in Real-Time with Python (No API Key)

The US national debt just crossed $36 trillion. I wanted to see exactly when it happened, so I built a tracker using the Treasury's free API.

Turns out, the US government has a surprisingly good API.

The API

FiscalData.Treasury.gov — free, no API key, no signup.

Track National Debt

import requests

def get_national_debt(days=10):
    url = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v2/accounting/od/debt_to_penny"
    params = {
        "sort": "-record_date",
        "page[size]": days,
        "fields": "record_date,tot_pub_debt_out_amt"
    }
    data = requests.get(url, params=params).json()

    for record in data["data"]:
        debt = float(record["tot_pub_debt_out_amt"])
        print(f"{record["record_date"]}: ${debt/1e12:.4f} trillion")

get_national_debt()
# 2025-03-20: $36.2184 trillion
# 2025-03-19: $36.2157 trillion
Enter fullscreen mode Exit fullscreen mode

Treasury Auction Results

Want to know what interest rate the government is paying on new debt?

def get_auctions(security_type="Bill", limit=5):
    url = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/auctions_query"
    params = {
        "filter": f"security_type:eq:{security_type}",
        "sort": "-auction_date",
        "page[size]": limit,
        "fields": "auction_date,security_term,high_investment_rate"
    }
    data = requests.get(url, params=params).json()

    for record in data["data"]:
        print(f"{record["auction_date"]} | {record["security_term"]} | Rate: {record["high_investment_rate"]}%")

get_auctions()
Enter fullscreen mode Exit fullscreen mode

Government Revenue vs Spending

def monthly_budget():
    url = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/mts/mts_table_5"
    params = {
        "sort": "-record_date",
        "page[size]": 12,
        "fields": "record_date,current_month_net,current_fytd_net"
    }
    data = requests.get(url, params=params).json()

    for record in data["data"][:6]:
        net = float(record["current_month_net"]) / 1e9
        print(f"{record["record_date"]}: Monthly deficit: ${abs(net):.1f}B")

monthly_budget()
Enter fullscreen mode Exit fullscreen mode

Exchange Rates

def exchange_rates(currency="Euro Zone-Euro"):
    url = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/rates_of_exchange"
    params = {
        "filter": f"country_currency_desc:eq:{currency}",
        "sort": "-record_date",
        "page[size]": 6,
        "fields": "record_date,exchange_rate"
    }
    data = requests.get(url, params=params).json()

    for record in data["data"]:
        print(f"{record["record_date"]}: 1 USD = {record["exchange_rate"]} EUR")

exchange_rates()
Enter fullscreen mode Exit fullscreen mode

All Available Datasets

The Treasury API has 30+ datasets:

Endpoint What it tracks
debt_to_penny Daily national debt
auctions_query Treasury auction results
mts_table_5 Monthly budget surplus/deficit
rates_of_exchange Exchange rates
avg_interest_rates Average interest on government debt
top_federal Federal spending by agency

This is Part 3 of My Government API Series

  1. SEC EDGAR API — company financials
  2. FRED API — economic data
  3. Treasury API — government debt and spending (this article)

All free. All without API keys (except FRED which takes 30 sec).

What would you track with this data?

I'm thinking about building a public dashboard. What metrics should it show?


Follow for more free API tutorials.

Top comments (0)