Simple JavaScript examples using fetch to integrate with the ViaCEP webservice and map the response to a typed Address object.
address.js: sharedAddressobject used by both integrations.via-cep-json.js: searches an address by ZIP code using JSON and maps the response toAddress.via-cep-xml.js: searches an address by ZIP code using XML and maps the response toAddress.
Use Node.js 18 or newer, because fetch is already available natively.
Run the JSON example:
node via-cep-json.jsRun the XML example:
node via-cep-xml.jsBoth examples use the official ViaCEP endpoint:
https://viacep.com.br/ws/01001000/json/
https://viacep.com.br/ws/01001000/xml/
- Official ViaCEP website: https://viacep.com.br/
- JSON response format: https://viacep.com.br/ws/01001000/json/
- XML response format: https://viacep.com.br/ws/01001000/xml/
- ViaCEP documentation explains that the CEP must have 8 digits and that the response format can be
jsonorxml.