Skill Tracer
Skills › Scientific Agent Skills › usfiscaldata › parameters.md

parameters.md

191 lines · skills/usfiscaldata/references/parameters.md at 5f2bc03

Open on GitHub
1# Query Parameters — U.S. Treasury Fiscal Data API
2
3All parameters are optional. Combine them with `&` in the URL query string.
4

Query Parameters — U.S. Treasury Fiscal Data API

All parameters are optional. Combine them with & in the URL query string.

Raw lines 1 to 4
5## `fields=` — Select Columns
6
7Returns only the specified fields. Accepts a comma-separated list of field names.
8
9```
10?fields=record_date,tot_pub_debt_out_amt
11?fields=country_currency_desc,exchange_rate,record_date
12```
14- If omitted, all fields are returned
15- Invalid field names cause an error
16- Omitting some fields can trigger **automatic aggregation** (see below)

fields= — Select Columns

Returns only the specified fields. Accepts a comma-separated list of field names.

?fields=record_date,tot_pub_debt_out_amt
?fields=country_currency_desc,exchange_rate,record_date
  • If omitted, all fields are returned
  • Invalid field names cause an error
  • Omitting some fields can trigger automatic aggregation (see below)
Raw lines 5 to 17
18### Aggregation / Auto-Sum
20In some endpoints, omitting fields aggregates non-unique rows and sums numeric values. This is not a universal SQL-style grouping guarantee. Keep the dimensions identifying each observation when working with rates, balances, totals or hierarchical statements.
22```python
23# Demonstrates API aggregation; this is NOT a validated cash-flow total.
24# Statement subtotals and overlapping categories must be resolved first.
25params = {
26 "fields": "record_date,transaction_type,transaction_today_amt"
28```

Aggregation / Auto-Sum

In some endpoints, omitting fields aggregates non-unique rows and sums numeric values. This is not a universal SQL-style grouping guarantee. Keep the dimensions identifying each observation when working with rates, balances, totals or hierarchical statements.

# Demonstrates API aggregation; this is NOT a validated cash-flow total.
# Statement subtotals and overlapping categories must be resolved first.
params = {
    "fields": "record_date,transaction_type,transaction_today_amt"
}
Raw lines 18 to 29
30## `filter=` — Filter Records
32Narrow results by field values. Multiple field filters are **comma-separated in a single `filter=` parameter**.

filter= — Filter Records

Narrow results by field values. Multiple field filters are **comma-separated in a single filter= parameter**.

Raw lines 30 to 33
34### Filter Syntax
36```
37filter=<field>:<operator>:<value>
38filter=<field>:<operator>:<value>,<field>:<operator>:<value>
39```

Filter Syntax

filter=<field>:<operator>:<value>
filter=<field>:<operator>:<value>,<field>:<operator>:<value>
Raw lines 34 to 40
41### Operators
43| Operator | Meaning | Example |
44|----------|---------|---------|
45| `eq` | Equal to | `filter=record_date:eq:2024-03-31` |
46| `lt` | Less than | `filter=exchange_rate:lt:1.5` |
47| `lte` | Less than or equal | `filter=record_date:lte:2024-12-31` |
48| `gt` | Greater than | `filter=record_fiscal_year:gt:2010` |
49| `gte` | Greater than or equal | `filter=record_date:gte:2024-01-01` |
50| `in` | Contained in set | `filter=country_currency_desc:in:(Canada-Dollar,Mexico-Peso)` |

Operators

OperatorMeaningExample
eqEqual tofilter=record_date:eq:2024-03-31
ltLess thanfilter=exchange_rate:lt:1.5
lteLess than or equalfilter=record_date:lte:2024-12-31
gtGreater thanfilter=record_fiscal_year:gt:2010
gteGreater than or equalfilter=record_date:gte:2024-01-01
inContained in setfilter=country_currency_desc:in:(Canada-Dollar,Mexico-Peso)
Raw lines 41 to 51
52### Date Filters
54Use `YYYY-MM-DD` format for dates:
56```
57filter=record_date:gte:2024-01-01
58filter=record_date:gte:2023-01-01,record_date:lte:2023-12-31
59```

Date Filters

Use YYYY-MM-DD format for dates:

filter=record_date:gte:2024-01-01
filter=record_date:gte:2023-01-01,record_date:lte:2023-12-31
Raw lines 52 to 60
61### Multi-Field Filters
63```
64filter=country_currency_desc:in:(Canada-Dollar,Mexico-Peso),record_date:gte:2024-01-01
65```

Multi-Field Filters

filter=country_currency_desc:in:(Canada-Dollar,Mexico-Peso),record_date:gte:2024-01-01
Raw lines 61 to 66
67### Common Filter Fields
69Most endpoints have these standard date fields:
70- `record_date` — The date of the record (YYYY-MM-DD)
71- `record_fiscal_year` — Fiscal year (e.g., `2024`)
72- `record_fiscal_quarter` — Fiscal quarter (1-4)
73- `record_calendar_year` — Calendar year
74- `record_calendar_month` — Calendar month (01-12)

Common Filter Fields

Most endpoints have these standard date fields:

  • record_date — The date of the record (YYYY-MM-DD)
  • record_fiscal_year — Fiscal year (e.g., 2024)
  • record_fiscal_quarter — Fiscal quarter (1-4)
  • record_calendar_year — Calendar year
  • record_calendar_month — Calendar month (01-12)
Raw lines 67 to 75
76## `sort=` — Sort Results
78Sort by one or more fields. Prefix `-` for descending order.
80```
81?sort=-record_date # Most recent first
82?sort=record_date # Oldest first
83?sort=-record_fiscal_year,-record_fiscal_quarter # Nested sort
84```
86**Default:** Sorted by the first column (usually `record_date` ascending).

sort= — Sort Results

Sort by one or more fields. Prefix - for descending order.

?sort=-record_date           # Most recent first
?sort=record_date            # Oldest first
?sort=-record_fiscal_year,-record_fiscal_quarter  # Nested sort

Default: Sorted by the first column (usually record_date ascending).

Raw lines 76 to 87
88## `format=` — Output Format
90```
91?format=json # Default
92?format=csv # Comma-separated values
93?format=xml # XML
94```
96When using CSV or XML format, the response is the raw file content rather than JSON.

format= — Output Format

?format=json    # Default
?format=csv     # Comma-separated values
?format=xml     # XML

When using CSV or XML format, the response is the raw file content rather than JSON.

Raw lines 88 to 97
98## `page[size]=` and `page[number]=` — Pagination
100Controls how many records per page and which page to return.
102```
103?page[size]=100&page[number]=1 # Default (100 records, page 1)
104?page[size]=10000 # Large page to reduce requests
105?page[number]=5&page[size]=50 # 50 records starting at page 5
106```
108- Default page size: **100**
109- Default page number: **1**
110- Use `meta.total-pages` in the response to know how many pages exist
111- Use `meta.total-count` for total record count

page[size]= and page[number]= — Pagination

Controls how many records per page and which page to return.

?page[size]=100&page[number]=1    # Default (100 records, page 1)
?page[size]=10000                  # Large page to reduce requests
?page[number]=5&page[size]=50     # 50 records starting at page 5
  • Default page size: 100
  • Default page number: 1
  • Use meta.total-pages in the response to know how many pages exist
  • Use meta.total-count for total record count
Raw lines 98 to 112
113### Fetch All Records
115For small result sets where `meta.total-pages` is 1, a single request with `page[size]=10000` is enough. Use `fetch_all()` below when pagination is required.
117```python
118import time
119import requests
120import pandas as pd
122def fetch_all(endpoint, params=None, max_pages=50, max_records=500_000):
123 """Return all matching rows or raise on a bound or changed result size."""
124 if max_pages < 1 or max_records < 1:
125 raise ValueError("Bounds must be positive")
126 params = dict(params or {})
127 allowed = {"fields", "filter", "sort", "format", "page[size]", "page[number]"}
128 if set(params) - allowed:
129 raise ValueError(f"Unknown query parameters: {set(params) - allowed}")
130 if params.get("format", "json") != "json":
131 raise ValueError("fetch_all requires JSON")
132 size = int(params.get("page[size]", 1000))
133 if size < 1:
134 raise ValueError("page[size] must be positive")
135 params["page[size]"] = min(size, 10000) # local request-size policy
136 params["page[number]"] = 1
137 base = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service"
138 rows = []
139 expected = None
140 for page in range(1, max_pages + 1):
141 params["page[number]"] = page
142 for attempt in range(3):
143 resp = requests.get(base + endpoint, params=params, timeout=30)
144 if resp.status_code in (429, 502, 503, 504) and attempt < 2:
145 delay = resp.headers.get("Retry-After", "")
146 time.sleep(min(float(delay), 60) if delay.isdigit() else 2 ** attempt)
147 continue
148 resp.raise_for_status()
149 break
150 result = resp.json()
151 if "error" in result:
152 raise ValueError(result)
153 total = int(result["meta"]["total-count"])
154 pages = int(result["meta"]["total-pages"])
155 if total > max_records or pages > max_pages:
156 raise RuntimeError("Result exceeds bounds; narrow filters or raise limits")
157 if expected is not None and total != expected:
158 raise RuntimeError("Result count changed during pagination; rerun snapshot")
159 expected = total
160 rows.extend(result["data"])
161 if len(rows) > max_records:
162 raise RuntimeError("Result exceeds max_records")
163 if page >= pages:
164 if len(rows) != expected:
165 raise RuntimeError("Incomplete result")
166 return pd.DataFrame(rows)
167 raise RuntimeError("Reached max_pages before completion")
168```
170This detects count changes, not every possible revision with unchanged row counts.
171Use explicit date bounds and a deterministic sort (including row identifiers when
172needed), and retain the raw download for a reproducible snapshot.

Fetch All Records

For small result sets where meta.total-pages is 1, a single request with page[size]=10000 is enough. Use fetch_all() below when pagination is required.

import time
import requests
import pandas as pd

def fetch_all(endpoint, params=None, max_pages=50, max_records=500_000):
    """Return all matching rows or raise on a bound or changed result size."""
    if max_pages < 1 or max_records < 1:
        raise ValueError("Bounds must be positive")
    params = dict(params or {})
    allowed = {"fields", "filter", "sort", "format", "page[size]", "page[number]"}
    if set(params) - allowed:
        raise ValueError(f"Unknown query parameters: {set(params) - allowed}")
    if params.get("format", "json") != "json":
        raise ValueError("fetch_all requires JSON")
    size = int(params.get("page[size]", 1000))
    if size < 1:
        raise ValueError("page[size] must be positive")
    params["page[size]"] = min(size, 10000)  # local request-size policy
    params["page[number]"] = 1
    base = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service"
    rows = []
    expected = None
    for page in range(1, max_pages + 1):
        params["page[number]"] = page
        for attempt in range(3):
            resp = requests.get(base + endpoint, params=params, timeout=30)
            if resp.status_code in (429, 502, 503, 504) and attempt < 2:
                delay = resp.headers.get("Retry-After", "")
                time.sleep(min(float(delay), 60) if delay.isdigit() else 2 ** attempt)
                continue
            resp.raise_for_status()
            break
        result = resp.json()
        if "error" in result:
            raise ValueError(result)
        total = int(result["meta"]["total-count"])
        pages = int(result["meta"]["total-pages"])
        if total > max_records or pages > max_pages:
            raise RuntimeError("Result exceeds bounds; narrow filters or raise limits")
        if expected is not None and total != expected:
            raise RuntimeError("Result count changed during pagination; rerun snapshot")
        expected = total
        rows.extend(result["data"])
        if len(rows) > max_records:
            raise RuntimeError("Result exceeds max_records")
        if page >= pages:
            if len(rows) != expected:
                raise RuntimeError("Incomplete result")
            return pd.DataFrame(rows)
    raise RuntimeError("Reached max_pages before completion")

This detects count changes, not every possible revision with unchanged row counts. Use explicit date bounds and a deterministic sort (including row identifiers when needed), and retain the raw download for a reproducible snapshot.

Raw lines 113 to 173
174## Combining Parameters
176```python
177params = {
178 "fields": "country_currency_desc,exchange_rate,record_date,effective_date",
179 "filter": "country_currency_desc:in:(Canada-Dollar,Euro Zone-Euro),record_date:gte:2020-01-01",
180 "sort": "-record_date",
181 "format": "json",
182 "page[size]": 100,
183 "page[number]": 1
185resp = requests.get(
186 "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/rates_of_exchange",
187 params=params,
188 timeout=30,
190resp.raise_for_status()
191```

Combining Parameters

params = {
    "fields": "country_currency_desc,exchange_rate,record_date,effective_date",
    "filter": "country_currency_desc:in:(Canada-Dollar,Euro Zone-Euro),record_date:gte:2020-01-01",
    "sort": "-record_date",
    "format": "json",
    "page[size]": 100,
    "page[number]": 1
}
resp = requests.get(
    "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/od/rates_of_exchange",
    params=params,
    timeout=30,
)
resp.raise_for_status()
Raw lines 174 to 191