/api-distancep

An easy-to-use and free API to calculate distance between multiple CEPs.

Primary LanguageJavaScriptMIT LicenseMIT

An easy-to-use and free API to calculate distance between multiple CEPs.

How to use

GET

https://distancep.herokuapp.com/distance/CEP_ORIGIN/CEP_DESTINATION
const origin = '04335-000';
const destination = '01311000';

const response = await axios.get(`https://api-get-distance.herokuapp.com/distance/${origin}/${destination}`);
const data = response.data;

//do whatever you want

Response

{
  "distance": 14.4 //always in km,
  "cepsInfo": [
    {
      "cep": "04335-000",
      "logradouro": "Rua Leno",
      "complemento": "",
      "bairro": "Americanópolis",
      "localidade": "São Paulo",
      "uf": "SP",
      "ibge": "3550308",
      "gia": "1004",
      "ddd": "11",
      "siafi": "7107"
    },
    {
      "cep": "01311-000",
      "logradouro": "Avenida Paulista",
      "complemento": "até 609 - lado ímpar",
      "bairro": "Bela Vista",
      "localidade": "São Paulo",
      "uf": "SP",
      "ibge": "3550308",
      "gia": "1004",
      "ddd": "11",
      "siafi": "7107"
    }
  ]
}



POST

https://distancep.herokuapp.com/distance/
const addresses = ['04335000', '01311-000', '05606010', '23812310'];
const mode = 'driving';

const response = await axios.post(`https://api-get-distance.herokuapp.com/distance/`, {addresses,mode});
const data = response.data;

//do whatever you want

Response

{
  "distance": 2.677 //always in km,
  "cepsInfo": [
    {
      "cep": "04335-000",
      "logradouro": "Rua Leno",
      "complemento": "",
      "bairro": "Americanópolis",
      "localidade": "São Paulo",
      "uf": "SP",
      "ibge": "3550308",
      "gia": "1004",
      "ddd": "11",
      "siafi": "7107"
    },
    {
      "cep": "01311-000",
      "logradouro": "Avenida Paulista",
      "complemento": "até 609 - lado ímpar",
      "bairro": "Bela Vista",
      "localidade": "São Paulo",
      "uf": "SP",
      "ibge": "3550308",
      "gia": "1004",
      "ddd": "11",
      "siafi": "7107"
    },
    {
      "cep": "05606-010",
      "logradouro": "Avenida Morumbi",
      "complemento": "até 1000 - lado par",
      "bairro": "Morumbi",
      "localidade": "São Paulo",
      "uf": "SP",
      "ibge": "3550308",
      "gia": "1004",
      "ddd": "11",
      "siafi": "7107"
    },
    {
      "cep": "23812-310",
      "logradouro": "Rua São João",
      "complemento": "",
      "bairro": "São José",
      "localidade": "Itaguaí",
      "uf": "RJ",
      "ibge": "3302007",
      "gia": "",
      "ddd": "21",
      "siafi": "5839"
    }
  ]
}



Technologies and APIs used in this project

Stack

  • Node.js
  • Express.js
  • Puppeteer
  • Puppeteer-cluster
  • Redis
  • axios

APIs

  • viacep API
  • google maps (direct query)

Setup

Running Localy

Clone this repository and then run:

npm i
npm start

Deploying to Heroku

How to deploy a puppeteer API to Heroku

Remember to add the "Heroku Redis" add-on