A CEP lookup on your own server
A Brazilian checkout or form asks for the CEP and fills the street, the town and the state. Most code calls ViaCEP for it; the same answer can come from a file beside the country file, with no outside call and no rate limit.
New Take addresses in Server, Docker Retail, e-commerce Websites, help desks
What you have
The country file of Brazil and its CEP layer br_cep.tsv.gz (23 MB), every eight-digit CEP of the data the file was built from. Both are free; the layer is among the files of the release.
Steps
1. Start the server with the layer:
$ gridpin serve brazil.bin --postcode-layer br_cep.tsv.gz --port 8080
2. Ask for a CEP on the route ViaCEP uses:
$ curl -s http://127.0.0.1:8080/ws/01310100/json/
{"bairro":"","cep":"01310-100","complemento":"","ddd":"","estado":"","gia":"","ibge":"","lat":-23.564526,"localidade":"São Paulo","logradouro":"Avenida Paulista","lon":-46.652504,"regiao":"","siafi":"","uf":"SP","unidade":""}
3. A CEP the layer does not have answers as ViaCEP does:
$ curl -s http://127.0.0.1:8080/ws/99999999/json/
{"erro":"true"}
What you get
logradouro, localidade and uf are the street, the town and the state; lat and lon are a house of that street with that CEP. Code that calls ViaCEP needs only the new base address.
Why it works
The layer lists every CEP of the addresses the file was built from, with its streets and the number of houses on each. A code several streets share answers the street with the most houses.
Limits
- No district (
bairro), IBGE code or area code: the source has none, the fields are empty. - Against ViaCEP on 99 street codes the street name was the same for 78; about half of the rest is spelling (“Rua 4” for “Rua Quatro”).
- Loading the layer takes about two seconds and 70 MB of memory.
The function: a street from a Brazilian CEP. Related: Brazil, WooCommerce.