The ComexStat API provides a rich set of auxiliary tables that serve two purposes: looking up the codes you need for filters, and enriching query results with human-readable labels.
library(comexr)
# Full list (281 entries)
countries <- comex_countries()
countries
#> # A tibble: 281 × 2
#> id text
#> <chr> <chr>
#> 1 994 A Designar
#> 2 132 Afeganistão
#> 3 175 Albânia
#> ...
# Search by name
comex_countries(search = "United")
#> # A tibble: 5 × 2
#> id text
#> <chr> <chr>
#> 1 249 Estados Unidos
#> 2 ...Note: The /tables/countries endpoint
does not accept a language parameter.
Country names are returned in Portuguese.
Get detailed info about a specific country:
# In Portuguese
comex_blocs(language = "pt")
#> # A tibble: 12 × 2
#> id text
#> <chr> <chr>
#> 1 105 América Central e Caribe
#> 2 107 América do Norte
#> 3 48 América do Sul
#> ...
# In English
comex_blocs(language = "en")The add parameter can request additional columns
(API-dependent):
states <- comex_states()
states
#> # A tibble: 34 × 3
#> text id uf
#> <chr> <chr> <chr>
#> 1 Acre 12 AC
#> 2 Alagoas 27 AL
#> 3 Amapá 16 AP
#> ...
# Detail for a specific state
comex_state_detail(26)
#> $coUf
#> [1] "26"
#> $sgUf
#> [1] "PE"
#> $noUf
#> [1] "Pernambuco"
#> $noRegiao
#> [1] "REGIAO NORDESTE"# Full list (5,570 municipalities)
cities <- comex_cities()
cities
#> # A tibble: 5,570 × 3
#> id text noMunMin
#> <chr> <chr> <chr>
#> 1 5300050 Abadia de Goiás - GO Abadia de Goiás
#> ...
# Detail for a specific city
comex_city_detail(2611606)
#> $coMunGeo
#> [1] "2611606"
#> $noMun
#> [1] "RECIFE"
#> $noMunMin
#> [1] "Recife"
#> $sgUf
#> [1] "PE"The NCM is the primary product classification for Brazilian trade since 1997. It is an 8-digit code based on the Harmonized System (HS).
# Paginated — use page and per_page
ncm <- comex_ncm(language = "en", page = 1, per_page = 10)
ncm
#> # A tibble: 10 × 3
#> noNCM unit coNcm
#> <chr> <chr> <chr>
#> 1 Adhesives based on rubber KILOGRAM 35069110
#> ...
# Search by keyword
comex_ncm(language = "en", search = "soybean")
# Get detail for a specific NCM code
comex_ncm_detail("02042200")
#> $id
#> [1] "02042200"
#> $text
#> [1] "Outras peças não desossadas de ovino, frescas ou refrigeradas"The HS table provides the international product classification hierarchy: subheading (6-digit), heading (4-digit), and chapter (2-digit).
hs <- comex_hs(language = "en", page = 1, per_page = 5)
hs
#> # A tibble: 5 × 6
#> subHeadingCode subHeading headingCode heading
#> <chr> <chr> <chr> <chr>
#> 1 010110 Pure-bred breeding horses ... 0101 Live horses, ...
#> ...Note: The HS table does not have a
search parameter — only language,
add, page, and per_page.
sitc <- comex_sitc(language = "en", page = 1, per_page = 3)
sitc
#> # A tibble: 3 × 10
#> coSITCBasicHeading SITCBasicHeading coSITCSubGroup SITCSubGroup ...
#> <chr> <chr> <chr> <chr> ...
#> 1 I Gold, monetary 9710 Gold, non-m... ...
#> ...The SITC table has 5 levels of hierarchy: Section → Division → Group → Subgroup → Basic Heading.
The main purpose of these tables is to find the numeric codes to use
in filters:
# 1. Find country code for Argentina
countries <- comex_countries(search = "Argentina")
# id = "021"
# 2. Find state code for Rio Grande do Sul
states <- comex_states()
# RS = id "43"
# 3. Query: Exports from RS to Argentina, grouped by HS4
result <- comex_export(
start_period = "2024-01",
end_period = "2024-12",
details = c("state", "country", "hs4"),
filters = list(
country = 21,
state = 43
)
)Some product tables have thousands of entries. Use page
and per_page to paginate:
If you are unsure which filters or details are valid for a given endpoint, use the discovery functions at runtime:
# What filters can I use for city queries?
comex_filters("city")
# What values does filter "state" accept for city queries?
comex_filter_values("state", type = "city")
# What metrics are available for historical queries?
comex_metrics("historical")This is particularly useful when the API is updated with new fields or metrics.