comtrade_get_data
Fetch UN Comtrade final trade data for one query, validating codes and checking counts first to avoid empty results or silent truncation above 100,000 records.
Instructions
Fetch UN Comtrade final trade data for one query, up to 100,000 records.
Counts first (one extra call, as get_counts): a count of 0 returns an empty list
without fetching, since no matching data is an answer, not an error; a count above
100,000 raises QueryTooLarge with the count before anything is fetched, because
upstream would cut the answer at 100,000 rows without saying so. Each row carries
every field of upstream's TradeDataRecord under its snake_case name;
primary_value is upstream's primary trade value (taken from the CIF or FOB value,
in cifvalue/fobvalue). Leaving out partner2_code,
customs_code and mot_code returns every breakdown upstream holds (32 rows for
Switzerland's 2023 exports to the World); the row with partner2_code 0,
customs_code "C00" and mot_code 0 is the total, and pinning those three
(or breakdown_mode="classic") returns that row alone. Arguments are checked
before any call; an unknown reporter, partner, customs or transport code raises
UnknownCode, an InvalidRequestError naming the packaged list and the closest
code, another mistake InvalidRequestError naming the closest valid value.
Args:
type_code: "C" goods or "S" services.
freq_code: "A" annual or "M" monthly.
cl_code: Product classification: "HS" (all HS editions as reported; prefer
it), an edition "H0" to "H5", SITC "SS", "S1" to "S4", BEC
"B4", "B5", for services EBOPS "EB", "EB02", "EB10",
"EB10S"; also "BE" and "TM", which the spec lists and upstream
accepts. "H6" is valid but upstream answered HTTP 500 to all five tries on
2026-10-23 (two reporters, two product codes); "HS" rows carry the edition
the reporter used, e.g. classification_code "H6" for Switzerland 2023.
reporter_code: Reporting areas as UN Comtrade's numeric codes, comma-separated,
e.g. "757" Switzerland, "842" USA, "276" Germany, "156" China,
"757,842", as list_reporters lists them. Some differ from ISO numeric
codes: Switzerland is 757, not 756. Omit for every reporter.
partner_code: Partner areas, comma-separated, as list_partners lists them:
"0" World (the total over all partners), "276" Germany. Omit for every
partner, one row each.
partner2_code: Second partner (consignment) areas, comma-separated, codes of
list_partners; "0" is the total.
period: Years "2023" (annual) or months "202305" (monthly), at most 12
comma-separated, e.g. "2021,2022,2023".
cmd_code: Product codes of cl_code, comma-separated, as
list_commodities(cl_code) lists them: "TOTAL" for all goods,
"AG2", "AG4", "AG6" for every code at that level, codes such as
"0901" or "090111", wildcards such as "27??" (every four-digit code
under 27) or "27*". For services the classification files name "200"
(EB, EB02, EB10) or "S" (EB10S) as the total; recorded counts confirm
"200" on EB10 and EB02, where "TOTAL" counted 0 (EB10). A code
upstream does not have counts 0 too, so a 0 does not prove that a code exists.
flow_code: Trade flows, comma-separated: "X" exports, "M" imports,
"RX" re-exports, "RM" re-imports, also "DX", "FM", "MIP",
"MOP", "XIP", "XOP", "MIF", "XIF"; e.g. "M,X".
mot_code: Modes of transport, comma-separated: "0" the total, "1000" air,
"2100" sea, "3100" railway, "3200" road, "9100" pipelines and
cables. Omit for every mode, one row each.
customs_code: Customs procedures, comma-separated: "C00" the total, "C01"
clearance for home use, "C03" outright exportation. Omit for every
procedure, one row each.
aggregate_by: Upstream parameters to aggregate the results over, comma-separated
from cmdCode, period, partnerCode, partner2Code,
reporterCode, customsCode, motCode; e.g. "cmdCode" sums the
products of "AG2" into one row per remaining breakdown. The aggregated
parameters come back as -2 (cmd_code "-2", partner_code -2)
with classification_code "GG"; the count matches the rows.
breakdown_mode: "plus", upstream's default (omitting it gives the same rows):
every customs procedure, mode of transport and second partner;
"classic": only the total row (partner2_code 0, customs_code
"C00", mot_code 0).
include_desc: Upstream's includeDesc, "Include descriptions of data
variables": fills reporter_iso/reporter_desc, the partner and second
partner ones, flow_desc, cmd_desc, customs_desc, mot_desc, the
quantity unit abbreviations, aggr_level and is_leaf; without it they
are None.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"type_code": "<type_code>",
"freq_code": "<freq_code>",
"cl_code": "<cl_code>"
},
"name": "comtrade_get_data"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| period | No | ||
| cl_code | Yes | ||
| cmd_code | No | ||
| mot_code | No | ||
| flow_code | No | ||
| freq_code | Yes | ||
| type_code | Yes | ||
| aggregate_by | No | ||
| customs_code | No | ||
| include_desc | No | ||
| partner_code | No | ||
| partner2_code | No | ||
| reporter_code | No | ||
| breakdown_mode | No |