Yesterday I started writing a few web scrapers in Node.js to gather some data for a personal project of mine (wait for it).

One of the things I had to deal, is how to convert addresses to geolocation (latitude and longitude, basically.)

So I started doing what we do best as Javascript developers:

Hm… there must be a package for that.

And of course, there is: https://www.npmjs.com/package/node-geocoder

And it's usage is fairly simple:

But, as we're using Google as a provider, of course, none of this is for free 😅

I was making a lot of tests and I would trigger a few thousands of requests because I was basically web scraping a lot of sources and also validating those results.

Google's Geocoder Logo

Google charges you 5 USD for every 1000 requests on their Geocoding API.

So I started looking for alternatives and I found this nice application provided by OpenStreetMaps called Nominatim. (For more information about it, click here)

And it actually works really well and really fast, so I started looking around for their API's and I found this really well-done documentation on how to consume the search endpoint (which was pretty much the only thing I needed).

So I started tweaking around and came out with this really small script that actually does exactly what I want, and it came out pretty simple!

As I was running it on node servers, node-fetch is my default way to go to trigger HTTP calls. If you’re running it on the browser, just make sure to use the native fetch method. (or whatever else you like)

This small module came out fairly simple and can be easily implemented in any language.

There is, of course, some restrictions. This API is public but is made mainly to support Open Street Maps, so make sure to follow their usage policy.

And a small highlight:

The module node-geocoder also offers support to Nominatim API.

I just went for my own implementation because it was literally everything I needed.

So there is also another learning from this: sometimes you might not fully need a module, even though it offers you what you want.