xippios-comtrade
Typed Python client, MCP server and FastAPI routes for UN Comtrade, part of the xippios SDK family. Names and semantics follow the upstream documentation; every response carries release metadata (source, endpoint, retrieved_at, published_at, vintage).
Install
uv add xippios-comtrade # client only
uv add "xippios-comtrade[mcp]" # + MCP server
uv add "xippios-comtrade[http]" # + FastAPI router
Set COMTRADE_API_KEY in the environment. Nothing is read from files.
30-second example
from xippios.comtrade import get_counts, get_data
# Switzerland (reporter 757) exports to the World (partner 0) in 2023, all products.
query = {
"reporter_code": "757",
"partner_code": "0",
"period": "2023",
"cmd_code": "TOTAL",
"flow_code": "X",
}
get_counts("C", "A", "HS", **query).data # 32: every customs procedure and mode of transport
result = get_data("C", "A", "HS", **query)
total = next(r for r in result.data if (r.customs_code, r.mot_code) == ("C00", 0))
total.primary_value # Decimal('419922543423.509'); JSON: a number
result.meta.endpoint, result.meta.request # 'data/v1/get', the arguments above
Parameters are upstream's (typeCode, reporterCode, ...) in snake_case and take
upstream's codes, several of them comma-separated as upstream takes them ("757,842").
Reporter and partner codes are UN Comtrade's, not ISO numeric: Switzerland is 757.
Every argument is checked before the call: coded ones (type_code, cl_code,
flow_code, customs_code, mot_code, aggregate_by, breakdown_mode) against
the lists packaged from upstream's reference files and spec (xippios.comtrade.enums),
reporter_code against the packaged copy of upstream's Reporters.json
(reference/Reporters.csv) and partner_code and partner2_code against that of
partnerAreas.json, the others by shape; a mistake raises
InvalidRequestError naming the closest valid value (756 names 757, Switzerland).
from xippios.comtrade import list_commodities, list_partners, list_reporters
list_reporters().data # 255 reporters, packaged with this version: no call
list_reporters(search="switz").data # code, ISO alpha-3 or name, any case: 757
list_partners().data # 310 partner areas, 0 World among them
list_commodities("HS", search="coffee").data # from 8,262 HS codes, fetched keyless once
Four task-shaped slices answer the common questions in one call each (the series in one
call per 12 periods), at the total of every breakdown, one row per reporter × partner ×
product code × period. Areas are UN Comtrade's codes or ISO alpha-3 codes; YYYY periods
ask for annual data, YYYYMM for monthly:
from xippios.comtrade import (
get_bilateral_flows,
get_commodity_series,
get_top_partners,
get_world_trade,
)
get_bilateral_flows("CHE", "USA", "TOTAL,27", "2022,2023", "X").data # 4 rows
years = ",".join(str(year) for year in range(2010, 2024))
get_commodity_series("757", "27", "X", years).data # 14 years, two calls, by period
get_world_trade("27", "2023", "M").data[0].reporter_desc # 'China': largest first
[r.partner_desc for r in get_top_partners("CHE", "TOTAL", "2023", "X", 3).data]
# ['USA', 'Germany', 'China']: World, the total, is dropped
Release dates are per dataset, so they sit on the rows, not in meta.published_at:
from xippios.comtrade import get_data_availability, get_live_update, get_metadata
[dataset] = get_data_availability("C", "A", "HS", reporter_code="757", period="2023").data
dataset.first_released, dataset.last_released # first publication and latest revision
notes = get_metadata("C", "A", "HS", reporter_code="757", period="2023").data[0].notes
[note.publication_date for note in notes] # "New Data", then each "Revised Data"
get_live_update().data # the 50 most recent releases, newest first
get_data counts first with upstream's countOnly: a count of zero returns an empty
result without a second call, and 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. A free key allows 1 call per second and 500 calls per day, and
countOnly calls count; the client spaces calls 1.5 s apart (upstream's gateway refuses
some calls spaced exactly 1 s apart), the daily quota is yours to track.
MCP
{
"mcpServers": {
"comtrade": {
"command": "uvx",
"args": ["--from", "xippios-comtrade[mcp]", "xippios-comtrade-mcp"],
"env": { "COMTRADE_API_KEY": "..." }
}
}
}
Tools are read-only and named comtrade_<verb>_<noun>. Results are capped at
200 rows or 64 KB with a note on how to narrow the query. Unfiltered,
comtrade_list_reporters, comtrade_list_partners and comtrade_list_commodities show
the first rows of their table only (200 of 255 reporters, in upstream's alphabetical
order), so they take search: {"search": "switz"} returns 757 Switzerland. --http
serves streamable HTTP instead of stdio.
Status
| Upstream operation | Task function | MCP tool | Status |
|---|---|---|---|
data/v1/get/{typeCode}/{freqCode}/{clCode} with countOnly=true |
get_counts(type_code, freq_code, cl_code, *, reporter_code=, partner_code=, ...) |
comtrade_get_counts |
alpha |
data/v1/get/{typeCode}/{freqCode}/{clCode} |
get_data(type_code, freq_code, cl_code, *, reporter_code=, partner_code=, ...) |
comtrade_get_data |
alpha |
data/v1/getDa/{typeCode}/{freqCode}/{clCode} |
get_data_availability(type_code, freq_code, cl_code, *, reporter_code=, period=, published_date_from=, published_date_to=) |
comtrade_get_data_availability |
alpha |
data/v1/getMetadata/{typeCode}/{freqCode}/{clCode} |
get_metadata(type_code, freq_code, cl_code, *, reporter_code=, period=) |
comtrade_get_metadata |
alpha |
data/v1/getLiveUpdate |
get_live_update() |
comtrade_get_live_update |
alpha |
data/v1/get/C/{A,M}/{clCode}, total row, one reporter and partner |
get_bilateral_flows(reporter, partner, commodities, periods, flow, *, cl_code="HS") |
comtrade_get_bilateral_flows |
alpha |
data/v1/get/C/{A,M}/{clCode}, total row, partner World, 12 periods per call, at most 120 |
get_commodity_series(reporter, commodity, flow, periods, *, cl_code="HS") |
comtrade_get_commodity_series |
alpha |
data/v1/get/C/{A,M}/{clCode}, total row, every reporter, partner World |
get_world_trade(commodity, period, flow, *, cl_code="HS") |
comtrade_get_world_trade |
alpha |
data/v1/get/C/{A,M}/{clCode}, total row, every partner, ranked |
get_top_partners(reporter, commodity, period, flow, n, *, cl_code="HS") |
comtrade_get_top_partners |
alpha |
files/v1/app/reference/Reporters.json, packaged |
list_reporters(search=None) |
comtrade_list_reporters |
alpha |
files/v1/app/reference/partnerAreas.json, packaged |
list_partners(search=None) |
comtrade_list_partners |
alpha |
files/v1/app/reference/{clCode}.json, keyless |
list_commodities(cl_code, search=None) |
comtrade_list_commodities |
alpha |
All twelve are covered in 0.1.0, the first release. Alpha: the public surface may change
in a minor version before 1.0; CHANGELOG.md lists every change.
Upstream terms and attribution
Data comes from UN Comtrade (https://comtradeplus.un.org) under its own terms of use;
this package adds no rights to it. Documentation snapshot: docs/upstream/.
Licence
Apache-2.0. See LICENSE.
Metadata
Release files for xippios-comtrade 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| xippios_comtrade-0.1.0.tar.gz | 180.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| xippios_comtrade-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 254.9 kB
Release files / xippios_comtrade-0.1.0.tar.gz
| Download URL | xippios_comtrade-0.1.0.tar.gz |
|---|---|
| Size | 180.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86f799fe60f0936076aabd961fb381a370258e65ddf00fb300af7c3f999c2665
|
|
BLAKE2b-256 checksum How to use checksums |
319bcfe8b45b2c19dc700013047d9b9ab21f0792e01edc78c4f93e61d74a7be3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / xippios_comtrade-0.1.0-py3-none-any.whl
| Download URL | xippios_comtrade-0.1.0-py3-none-any.whl |
|---|---|
| Size | 74.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
91027535c355ccfcedc8bf4e0686e59d854203eff9af3ade81d7dd78ac139168
|
|
BLAKE2b-256 checksum How to use checksums |
571b683a82787f64b7a63fa67c0813dc69155b529c71fc8512a256220633640a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logSecurity for xippios-comtrade 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for xippios_comtrade-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.