Learn R Programming

comexr

The comexr package provides a complete R interface to the ComexStat API from the Brazilian Ministry of Development, Industry, Trade and Services (MDIC). It allows programmatic access to detailed Brazilian export and import data.

Features

  • 30 functions covering all API endpoints
  • 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
  • Only 2 dependencies: httr2 + cli
  • Multilingual: Portuguese, English, Spanish
  • SSL auto-fallback: handles ICP-Brasil certificate issues transparently

Installation

# Install from GitHub
# install.packages("remotes")
remotes::install_github("StrategicProjects/comexr")

Quick Start

library(comexr)

# Exports by country in January 2024 (monthly detail by default)
exports <- comex_export(
  start_period = "2024-01",
  end_period = "2024-01",
  details = "country"
)

# Imports with CIF value
imports <- comex_import(
  start_period = "2024-01",
  end_period = "2024-12",
  details = "country",
  metric_cif = TRUE
)

# Filter: exports to China (160), grouped by HS4
soy <- comex_export(
  start_period = "2024-01",
  end_period = "2024-12",
  details = c("country", "hs4"),
  filters = list(country = 160)
)

Discover available options

# What grouping fields are available?
comex_details("general")

# What filters can I use?
comex_filters("general")

# Look up country codes
countries <- comex_countries()
countries[grepl("China", countries$text, ignore.case = TRUE), ]

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

API Coverage

Query Functions

FunctionDescription
comex_query()General foreign trade query
comex_export()Shortcut for export queries
comex_import()Shortcut for import queries
comex_query_city()City-level data query
comex_historical()Historical data (1989-1996)

Metadata Functions

FunctionDescription
comex_last_update()Last data update date
comex_available_years()Available years for queries
comex_filters()Available filters
comex_filter_values()Values for a specific filter
comex_details()Available detail/grouping fields
comex_metrics()Available metrics

Auxiliary Tables

FunctionDescription
comex_countries() / comex_country_detail()Countries
comex_blocs()Economic blocs
comex_states() / comex_state_detail()Brazilian states
comex_cities() / comex_city_detail()Brazilian cities
comex_transport_modes() / comex_transport_mode_detail()Transport modes
comex_customs_units() / comex_customs_unit_detail()Customs units
comex_ncm() / comex_ncm_detail()NCM codes
comex_nbm() / comex_nbm_detail()NBM codes (historical)
comex_hs()Harmonized System
comex_cgce()CGCE (BEC) classification
comex_sitc()SITC classification
comex_isic()ISIC classification

SSL Certificate Issues

On some systems the API's ICP-Brasil certificate chain is not recognized. The package handles this automatically — on the first failure it retries without SSL verification and issues a warning. To suppress:

options(comex.ssl_verifypeer = FALSE)

References

  • ComexStat — Brazilian foreign trade statistics
  • ComexStat API Docs — Official API documentation
  • MDIC — Ministry of Development, Industry, Trade and Services

License

MIT © comexr authors

Copy Link

Version

Install

install.packages('comexr')

Version

0.2.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Andre Leite

Last Published

March 17th, 2026

Functions in comexr (0.2.1)

comex_filters

Get available filters
comex_historical

Query historical foreign trade data (1989-1996)
comex_isic

Get ISIC (International Standard Industrial Classification) table
comex_last_update

Get last data update date
comex_export

Query exports
comex_hs

Get Harmonized System (HS) tables
comex_import

Query imports
comex_filter_values

Get values for a specific filter
comex_nbm_detail

Get NBM code details
comex_state_detail

Get state details
comex_query_city

Query city-level foreign trade data
comex_states

Get Brazilian states (UF) table
comex_metrics

Get available metrics
comex_ncm_detail

Get NCM code details
comex_query

Query general foreign trade data
comex_ncm

Get NCM (Mercosur Common Nomenclature) table
comex_nbm

Get NBM (Brazilian Nomenclature of Goods) table
comex_sitc

Get SITC/CUCI (Standard International Trade Classification) table
comexr-package

comexr: R Client for the ComexStat API
comex_transport_modes

Get transport modes table
comex_transport_mode_detail

Get transport mode details
comex_city_detail

Get city details
comex_cgce

Get CGCE (Classification by Broad Economic Categories) table
comex_countries

Get countries table
comex_customs_units

Get customs units (URF) table
comex_blocs

Get economic blocs table
comex_details

Get available detail/grouping fields
comex_available_years

Get available years for queries
comex_customs_unit_detail

Get customs unit details
comex_cities

Get Brazilian cities table
comex_country_detail

Get country details