IBGE City Code Validator
Validate Brazilian IBGE city code format: 7 digits, first 2 = state code.
IBGE municipality code: the 7-digit identifier of every Brazilian city
The codigo IBGE de municipio is the 7-digit numeric identifier that the Instituto Brasileiro de Geografia e Estatistica assigns to each of the 5,570 Brazilian municipalities (count from the 2022 Census) plus the Distrito Federal. It is the canonical key used by virtually every public dataset, fiscal layout and federal program that needs to disambiguate a Brazilian city from another with the same name โ and there are dozens of such collisions, like Santa Cruz (RN, MG, PB), Nova Esperanca or Sao Jose.
Unlike postal codes (CEP), which group blocks of streets and change frequently, the IBGE municipality code is geopolitical: it changes only when a city is created, merged or dismembered. Once issued, a code is never reassigned to another municipality, even if the original one disappears. This stability makes it the preferred join key for census data, electoral results (TSE), public health (DATASUS), education (INEP) and tax (RFB) databases.
Anatomy of the 7 digits
The code follows a fixed layout that lets you do partial validation by inspection:
- Digits 1 to 2: state (UF) code, ranging from 11 (Rondonia) to 53 (Distrito Federal), matching the SIAFI/RFB UF table.
- Digits 3 to 6: sequential microcodigo assigned by the IBGE when the municipality is recognized.
- Digit 7: legacy verification digit originally computed by IBGE; today most validators only enforce the 7-digit length and the UF prefix.
Real examples from the canonical table: 3550308 Sao Paulo capital (SP), 3304557 Rio de Janeiro capital (RJ), 5300108 Brasilia (DF), 3106200 Belo Horizonte (MG), 4205407 Florianopolis (SC), 4314902 Porto Alegre (RS), 2304400 Fortaleza (CE), 2611606 Recife (PE), 2927408 Salvador (BA), 1302603 Manaus (AM), 1501402 Belem (PA), 5208707 Goiania (GO), 5103403 Cuiaba (MT), 5002704 Campo Grande (MS).
UF prefix table
The 2-digit UF code is the same across all federal layouts (NF-e, eSocial, IRRF). Use it as a fast sanity check:
- 11 RO, 12 AC, 13 AM, 14 RR, 15 PA, 16 AP, 17 TO
- 21 MA, 22 PI, 23 CE, 24 RN, 25 PB, 26 PE, 27 AL, 28 SE, 29 BA
- 31 MG, 32 ES, 33 RJ, 35 SP
- 41 PR, 42 SC, 43 RS
- 50 MS, 51 MT, 52 GO, 53 DF
Note the deliberate gaps (34, 44, 54 do not exist). The numbering was set in the 1970s and preserves the original federation hierarchy.
Mandatory uses in Brazilian e-government layouts
The IBGE code is required in dozens of mandatory submissions:
- NF-e (Nota Fiscal Eletronica) โ fields
cMun(municipality of operation),cMunFG(taxable event municipality) andcMunDescarga. - NFS-e โ service ISS is collected for the municipality identified by the IBGE code, not by CEP.
- eSocial โ layout S-1010 (Estabelecimento) and S-2200 (Worker Admission) require
codMunicfor the workplace. - RAIS, CAGED, DIRF, IRRF โ geo segmentation always uses the IBGE code.
- SUS DATASUS โ hospital and procedure datasets keyed by code.
- SIOPS, FINBRA โ public-finance reports.
- Civil registry, MapBiomas, INPE deforestation alerts โ geographic crosswalks.
Free public APIs and the BR developer stack
The IBGE publishes a free public REST API at servicodados.ibge.gov.br/api/v1/localidades/municipios. It supports filters by UF (/estados/SP/municipios), by region and by mesoregion. The endpoint requires no API key and is rate-limited at a few thousand requests per hour per IP. Response is JSON with fields id (the 7-digit code), nome, microrregiao, mesorregiao, UF.
The community-maintained BrasilAPI (brasilapi.com.br/api/ibge/municipios/v1/{UF}) wraps the same data with extra caching and CORS open by default โ handy for e-commerce checkouts that need to populate a city dropdown after the customer selects the UF. Together, the IBGE endpoint and BrasilAPI cover ~99% of the geo-lookup needs of a Brazilian fintech, marketplace or logistics product.
Curiosities and edge cases
A few facts worth knowing when building Brazilian geo features:
- Largest municipality by area: Altamira (PA), code
1502400, covering 159,696 kmยฒ โ larger than the entire state of Acre and many European countries. - Smallest by area: Santa Cruz de Minas (MG), code
3158300, only 3.565 kmยฒ. - Most populated: Sao Paulo capital with ~11.5 million people.
- Least populated: Serra da Saudade (MG) with ~770 inhabitants (Census 2022).
- Distrito Federal is special: a single code
5300108for the whole DF, although administratively divided into Regioes Administrativas (RAs) that are not municipalities. - CNEFE (Cadastro Nacional de Enderecos para Fins Estatisticos) cross-references each IBGE code to every street address used in the census.
- Codes for extinct municipalities stay in the historical IBGE table forever โ never recycled.
FAQ
Is the IBGE API really free?
Yes. servicodados.ibge.gov.br is a public service maintained with taxpayer money and does not require registration or API key. It is rate-limited to prevent abuse but generous for production use.
Does the code ever change for an existing city?
No. Once issued, the 7-digit code is permanent. If a city merges or is dismembered, a new code is created for the resulting entity, but the original code is preserved in historical tables. This is critical for longitudinal census analysis.
How many municipalities exist today?
5,570, per the IBGE 2022 Census reference table. The number had been stable since 2013 because most state constitutions tightened the rules for creating new municipalities after the 1988 federal constitution allowed near-free splitting.
Is the code the same as the SIAFI code or the TSE code?
No, they are different. SIAFI uses a 4-digit code for fiscal cost centers; TSE uses its own 5-digit code for electoral zones. The IBGE 7-digit code is the most widely adopted, but federal layouts may require explicit conversion tables when integrating with TSE or SIAFI data.
Can two cities share the same name?
Yes, very frequently. Santa Cruz, Nova Esperanca, Sao Jose and Cruzeiro appear dozens of times across the federation. The IBGE code is the only way to disambiguate them reliably in software.
Related Tools
CPF Validator
Validate Brazilian CPF numbers instantly using the official algorithm. Useful for testing document validation in applications. No data sent to servers.
Batch CPF Validator
Validate a list of CPFs (one per line) and see which are valid and which are not. No data sent to servers.
Batch CNPJ Validator
Validate a list of CNPJs (one per line) with a summary of valid, invalid and total. No data sent to servers.