API reference¶
Auto-generated from the package docstrings.
Query functions¶
comex_query ¶
comex_query(flow: str = 'export', start_period: str = None, end_period: str = None, details: Details = None, filters: Filters = None, month_detail: bool = True, metric_fob: bool = True, metric_kg: bool = True, metric_statistic: bool = False, metric_freight: bool = False, metric_insurance: bool = False, metric_cif: bool = False, language: str = 'en', verbose: bool = True) -> pd.DataFrame
Query general Brazilian foreign trade data (1997-present).
Supports filtering and grouping by multiple classifications such as NCM, Harmonized System, countries, states, etc. Data is available monthly from 1997 to the most recent complete month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
str
|
Trade flow: |
'export'
|
start_period
|
str
|
Period bounds in |
None
|
end_period
|
str
|
Period bounds in |
None
|
details
|
str or sequence of str
|
Detail/grouping fields. The names below are user-friendly aliases;
the package translates each to the underlying API name. The API names
returned by
|
None
|
filters
|
mapping
|
Mapping of filter name to value(s). Names match the detail fields.
Example: |
None
|
month_detail
|
bool
|
If |
True
|
metric_fob
|
bool
|
Metrics to include. FOB (US$) and net weight (kg) default to |
True
|
metric_kg
|
bool
|
Metrics to include. FOB (US$) and net weight (kg) default to |
True
|
metric_statistic
|
bool
|
Metrics to include. FOB (US$) and net weight (kg) default to |
True
|
metric_freight
|
bool
|
Metrics to include. FOB (US$) and net weight (kg) default to |
True
|
metric_insurance
|
bool
|
Metrics to include. FOB (US$) and net weight (kg) default to |
True
|
metric_cif
|
bool
|
Metrics to include. FOB (US$) and net weight (kg) default to |
True
|
language
|
str
|
Response language: |
'en'
|
verbose
|
bool
|
Show progress messages (default |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The query results. |
Examples:
Source code in src/comexpy/query.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
comex_export ¶
comex_export(start_period: str, end_period: str, details: Details = None, filters: Filters = None, month_detail: bool = True, metric_fob: bool = True, metric_kg: bool = True, metric_statistic: bool = False, metric_freight: bool = False, metric_insurance: bool = False, metric_cif: bool = False, language: str = 'en', verbose: bool = True) -> pd.DataFrame
Query exports — shortcut for :func:comex_query with flow="export".
Source code in src/comexpy/query.py
comex_import ¶
comex_import(start_period: str, end_period: str, details: Details = None, filters: Filters = None, month_detail: bool = True, metric_fob: bool = True, metric_kg: bool = True, metric_statistic: bool = False, metric_freight: bool = False, metric_insurance: bool = False, metric_cif: bool = False, language: str = 'en', verbose: bool = True) -> pd.DataFrame
Query imports — shortcut for :func:comex_query with flow="import".
Source code in src/comexpy/query.py
comex_query_city ¶
comex_query_city(flow: str = 'export', start_period: str = None, end_period: str = None, details: Details = None, filters: Filters = None, month_detail: bool = True, metric_fob: bool = True, metric_kg: bool = True, language: str = 'en', verbose: bool = True) -> pd.DataFrame
Query city-level Brazilian foreign trade data.
City-level data is more aggregated than general data, with fewer available details and metrics. City information is based on the declarant of exports/imports, not the producer or buyer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
str
|
Trade flow: |
'export'
|
start_period
|
str
|
Period bounds in |
None
|
end_period
|
str
|
Period bounds in |
None
|
details
|
str or sequence of str
|
Detail/grouping fields. The city endpoint accepts only a subset of the general fields:
|
None
|
filters
|
mapping
|
Mapping of filter name to value(s). Accepts the same names as
|
None
|
month_detail
|
bool
|
If |
True
|
metric_fob
|
bool
|
Only FOB (US$) and net weight (kg) are supported at city level. |
True
|
metric_kg
|
bool
|
Only FOB (US$) and net weight (kg) are supported at city level. |
True
|
language
|
str
|
Response language: |
'en'
|
verbose
|
bool
|
Show progress messages (default |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The query results. |
Notes
City-level data differs from general data: full NCM and HS6 are not available (product detail goes only to HS4); CGCE, SITC and ISIC are not available; transport mode and customs unit are not available; and only FOB and KG metrics are supported.
Source code in src/comexpy/query_city.py
comex_historical ¶
comex_historical(flow: str = 'export', start_period: str = None, end_period: str = None, details: Details = None, filters: Filters = None, month_detail: bool = True, metric_fob: bool = True, metric_kg: bool = True, language: str = 'en', verbose: bool = True) -> pd.DataFrame
Query historical Brazilian foreign trade data (1989-1996).
Retrieves export and import data from before the SISCOMEX system was implemented. Historical data uses the NBM (Brazilian Nomenclature of Goods) classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
str
|
Trade flow: |
'export'
|
start_period
|
str
|
Period bounds in |
None
|
end_period
|
str
|
Period bounds in |
None
|
details
|
str or sequence of str
|
Detail/grouping fields. The historical endpoint supports only:
|
None
|
filters
|
mapping
|
Mapping of filter name to value(s). Accepts the same names as
|
None
|
month_detail
|
bool
|
If |
True
|
metric_fob
|
bool
|
Only FOB (US$) and net weight (kg) are supported. |
True
|
metric_kg
|
bool
|
Only FOB (US$) and net weight (kg) are supported. |
True
|
language
|
str
|
Response language: |
'en'
|
verbose
|
bool
|
Show progress messages (default |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The query results. |
Notes
Historical data is available for 1989 to 1996 only, with limited
details ("country", "state", "nbm"), NBM (not NCM) product
classification, and only FOB and KG metrics.
Source code in src/comexpy/historical.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
API metadata¶
comex_last_update ¶
Date of the last data update in the API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
Data type: |
'general'
|
verbose
|
bool
|
Show progress messages (default |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Last-update information. |
Source code in src/comexpy/tables.py
comex_available_years ¶
First and last years available for queries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type
|
str
|
Data type: |
'general'
|
verbose
|
bool
|
Show progress messages (default |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Mapping with |
Source code in src/comexpy/tables.py
comex_filters ¶
List of filter types available for API queries.
Source code in src/comexpy/tables.py
comex_filter_values ¶
comex_filter_values(filter: str, type: str = 'general', language: str = 'en', verbose: bool = False) -> pd.DataFrame
Possible values for a given filter.
The filter argument is passed verbatim to the API and is
case-sensitive — use the exact name returned by :func:comex_filters
(e.g. "economicBlock", "BECLevel1", "SITCSection",
"ISICSection", "subHeading", "heading", "chapter").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filter
|
str
|
Filter name as returned by :func: |
required |
type
|
str
|
Data type: |
'general'
|
language
|
str
|
Language: |
'en'
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables.py
comex_details ¶
Detail/grouping fields that can be used to group query results.
Source code in src/comexpy/tables.py
comex_metrics ¶
Metrics (values) available for API queries.
Source code in src/comexpy/tables.py
Auxiliary tables — geography¶
comex_countries ¶
Countries table with codes and names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
search
|
str
|
Search term to filter results (e.g. |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables.py
comex_country_detail ¶
Details for a specific country by its code (e.g. 105 for Brazil).
comex_blocs ¶
comex_blocs(language: str = 'en', search: Optional[str] = None, add: Optional[str] = None, verbose: bool = False) -> pd.DataFrame
Economic blocs table (trade agreements between countries/regions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language: |
'en'
|
search
|
str
|
Search term to filter results. |
None
|
add
|
str
|
Related table to include (e.g. |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables.py
comex_states ¶
comex_state_detail ¶
Details for a specific Brazilian state (e.g. 26 for Pernambuco).
comex_cities ¶
comex_city_detail ¶
Details for a specific Brazilian city (e.g. 5300050).
comex_transport_modes ¶
comex_transport_mode_detail ¶
Details for a specific transport mode (e.g. 5 for maritime).
comex_customs_units ¶
Customs units (URF) table.
The Federal Revenue Service administrative units (Unidades da Receita Federal) responsible for overseeing foreign trade operations.
Source code in src/comexpy/tables.py
comex_customs_unit_detail ¶
Details for a specific customs unit (URF) (e.g. 8110000).
Auxiliary tables — products¶
comex_ncm ¶
comex_ncm(language: str = 'en', search: Optional[str] = None, add: Optional[str] = None, page: Optional[int] = None, per_page: Optional[int] = None, verbose: bool = False) -> pd.DataFrame
NCM (Mercosur Common Nomenclature) table with descriptions.
NCM is the 8-digit product classification used by Mercosur countries, based on the Harmonized System (HS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language: |
'en'
|
search
|
str
|
Search term to filter results (e.g. |
None
|
add
|
str
|
Related table to include: |
None
|
page
|
int
|
Pagination controls (default returns all results). |
None
|
per_page
|
int
|
Pagination controls (default returns all results). |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables_products.py
comex_ncm_detail ¶
Details for a specific NCM code (8 digits, e.g. "02042200").
comex_nbm ¶
comex_nbm(language: str = 'en', search: Optional[str] = None, add: Optional[str] = None, page: Optional[int] = None, per_page: Optional[int] = None, verbose: bool = False) -> pd.DataFrame
NBM (Brazilian Nomenclature of Goods) table with descriptions.
NBM was used in Brazil before NCM adoption and applies only to historical data (1989-1996).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language: |
'en'
|
search
|
str
|
Search term to filter results. |
None
|
add
|
str
|
Related table to include (e.g. |
None
|
page
|
int
|
Pagination controls. |
None
|
per_page
|
int
|
Pagination controls. |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables_products.py
comex_nbm_detail ¶
Details for a specific NBM code (e.g. "2924101100").
comex_hs ¶
comex_hs(language: str = 'en', add: Optional[str] = None, page: Optional[int] = None, per_page: Optional[int] = None, verbose: bool = False) -> pd.DataFrame
Harmonized System (HS) classification tables.
The HS is an international product nomenclature developed by the World Customs Organization, organised hierarchically: Section, Chapter (HS2), Heading (HS4) and Subheading (HS6). NCM adds two more digits to HS6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language: |
'en'
|
add
|
str
|
Related table to include (e.g. |
None
|
page
|
int
|
Pagination controls. |
None
|
per_page
|
int
|
Pagination controls. |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables_products.py
Auxiliary tables — classifications¶
comex_cgce ¶
comex_cgce(language: str = 'en', search: Optional[str] = None, add: Optional[str] = None, page: Optional[int] = None, per_page: Optional[int] = None, verbose: bool = False) -> pd.DataFrame
CGCE (Classification by Broad Economic Categories) table.
CGCE groups products by use or economic purpose (e.g. capital goods,
intermediate goods, consumer goods). Served by /tables/classifications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language: |
'en'
|
search
|
str
|
Search term to filter results. |
None
|
add
|
str
|
Related table to include (e.g. |
None
|
page
|
int
|
Pagination controls. |
None
|
per_page
|
int
|
Pagination controls. |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables_classifications.py
comex_sitc ¶
comex_sitc(language: str = 'en', search: Optional[str] = None, add: Optional[str] = None, page: Optional[int] = None, per_page: Optional[int] = None, verbose: bool = False) -> pd.DataFrame
SITC/CUCI (Standard International Trade Classification) table.
CUCI is the Portuguese name for SITC. Served by the
/tables/product-categories endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
Language: |
'en'
|
search
|
str
|
Search term to filter results (e.g. |
None
|
add
|
str
|
Related table to include (e.g. |
None
|
page
|
int
|
Pagination controls. |
None
|
per_page
|
int
|
Pagination controls. |
None
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables_classifications.py
comex_isic ¶
ISIC (International Standard Industrial Classification) values.
Retrieves ISIC values at a chosen hierarchical level via the
/general/filters/{filter} endpoint, which is the only place the
ComexStat API exposes ISIC codes (there is no /tables/isic endpoint).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Hierarchical level: |
'section'
|
language
|
str
|
Language: |
'en'
|
verbose
|
bool
|
Show progress messages (default |
False
|
Source code in src/comexpy/tables_classifications.py
Configuration¶
set_options ¶
set_options(*, timeout_get: Optional[int] = None, timeout_post: Optional[int] = None, max_tries: Optional[int] = None, retry_time: Optional[int] = None, ssl_verify: Optional[bool] = None) -> None
Configure HTTP retry/timeout behaviour (equivalent to the R options).
The ComexStat API frequently returns rate-limit errors (HTTP 429, "Você excedeu o limite de solicitações...") or times out. Adjust these settings to work around such errors without overloading the servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout_get
|
int
|
Seconds to wait for a response on GET requests (default 60). |
None
|
timeout_post
|
int
|
Seconds to wait for a response on POST requests (default 120). |
None
|
max_tries
|
int
|
Maximum number of attempts for a failing request (default 3).
Adjusting |
None
|
retry_time
|
int
|
Seconds to wait between retries after a transient failure (default 10, matching the API's recommended back-off). |
None
|
ssl_verify
|
bool
|
Whether to verify SSL certificates. Set to |
None
|
Source code in src/comexpy/_client.py
get_options ¶
set_verbose ¶
Enable or disable informational messages (success/step/info).
Warnings are always shown. Errors are raised as exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If |
required |
Source code in src/comexpy/_msg.py
ComexError ¶
Bases: RuntimeError
Raised when a ComexStat API request fails.