Skip to main content

comexpy comexpy logo

PyPI Python versions Downloads License: MIT Docs Publish Ruff pandas R port: comexr

Access the Brazilian Foreign Trade Statistics API (ComexStat) from Python.

comexpy is a pandas-friendly interface to the ComexStat API of the Brazilian Ministry of Development, Industry, Trade and Services (MDIC). It provides programmatic access to detailed Brazilian export and import data — every query returns a pandas.DataFrame.

This is the Python port of the R package comexr; the public function names mirror the R API so knowledge transfers directly between the two.

Features

  • General trade data (1997–present), city-level data, and historical records (1989–1996)
  • Auxiliary tables: countries, economic blocs, NCM/NBM/HS product codes, CGCE/SITC/ISIC classifications, states, cities, transport modes, customs units
  • Friendly aliases — pass "hs4", "transport_mode", "cgce_n1" and the package translates them to the API's internal names
  • Multilingual responses: Portuguese, English, Spanish
  • SSL auto-fallback — handles ICP-Brasil certificate issues transparently
  • Configurable retry/timeout for the API's aggressive rate limiting

Installation

pip install comexpy

Requires Python 3.9+, requests and pandas.

Quick start

import comexpy

# Exports by country in January 2024
exports = comexpy.comex_export(
    start_period="2024-01",
    end_period="2024-01",
    details="country",
)

# Imports with CIF value, by country, for all of 2024
imports = comexpy.comex_import(
    start_period="2024-01",
    end_period="2024-12",
    details="country",
    metric_cif=True,
)

# Exports to China (160), grouped by HS4
# (the package translates "hs4" to the API's `heading`)
soy = comexpy.comex_export(
    start_period="2024-01",
    end_period="2024-12",
    details=["country", "hs4"],
    filters={"country": 160},
)

Discover available options

comexpy.comex_details("general")     # grouping fields available
comexpy.comex_filters("general")     # filters available
comexpy.comex_metrics("general")     # metrics available

# Look up country codes
countries = comexpy.comex_countries()
countries[countries["text"].str.contains("China", case=False)]

# Economic blocs in Portuguese
comexpy.comex_blocs(language="pt")

City-level and historical data

# Exports declared in Pernambuco (state 26) in 2023
comexpy.comex_query_city(
    flow="export",
    start_period="2023-01",
    end_period="2023-12",
    details=["country", "state"],
    filters={"state": 26},
)

# Historical exports 1995–1996 by country (NBM classification)
comexpy.comex_historical(
    flow="export",
    start_period="1995-01",
    end_period="1996-12",
    details="country",
)

Rate limiting and timeouts

The ComexStat API frequently returns rate-limit errors (HTTP 429, "Você excedeu o limite de solicitações...") or times out. Adjust the behaviour with set_options:

comexpy.set_options(retry_time=30)   # wait 30s between retries
comexpy.set_options(max_tries=5, timeout_post=180)

Silence progress/success messages with comexpy.set_verbose(False).

Public API

Function Purpose
comex_query / comex_export / comex_import General trade data (1997–present)
comex_query_city City-level data
comex_historical Historical data (1989–1996)
comex_last_update / comex_available_years API metadata
comex_filters / comex_filter_values / comex_details / comex_metrics Query option discovery
comex_countries / comex_blocs / comex_states / comex_cities / comex_transport_modes / comex_customs_units (+ _detail) Geography tables
comex_ncm / comex_nbm / comex_hs (+ _detail) Product nomenclatures
comex_cgce / comex_sitc / comex_isic Economic classifications
set_options / set_verbose Configuration

License

MIT — see LICENSE. Developed by the comexpy authors.

Release files for comexpy 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for comexpy 0.1.1
File Size Uploaded
comexpy-0.1.1.tar.gz 63.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for comexpy 0.1.1
File Interpreter ABI Platform
comexpy-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 85.4 kB

Release files / comexpy-0.1.1.tar.gz

Download URL comexpy-0.1.1.tar.gz
Size 63.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b12a56ae97b153764e7828201cc8fd1504961c39b40333599e8154aaf773d414
BLAKE2b-256 checksum
How to use checksums
59d8f4973be394ca006babfb8a09f08b0ec87ecd0090fbb61b9f9a045df2713b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 14, 2026.

Transparency log

Release files / comexpy-0.1.1-py3-none-any.whl

Download URL comexpy-0.1.1-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0766a458f9d1d5591e65ef6e47592ac7ecf5db6019d54cb3aabf23845a49cef7
BLAKE2b-256 checksum
How to use checksums
d1d8e13e394635089b5db779c3b9ea834ae650dc162a361899a012cc259849cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page