How to geolocate an IP in Node.js

To find the location of an IP address, first we need to add the library to our package.json:

"dependencies": {
"cloudmersive-validate-api-client": "^1.1.2"
}

From here, we need to call the iPAddressPost method on the IP address in question:

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');
var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveValidateApiClient.IPAddressApi();var value = "value_example"; // String | IP address to geolocate, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.iPAddressPost(value, callback);

This will then return back a response like the following:

{
"CountryCode": "US",
"CountryName": "United States",
"City": "Everett",
"RegionCode": "WA",
"RegionName": "Washington",
"ZipCode": "98201",
"TimezoneStandardName": "America/Los Angeles",
"Latitude": 47.9884,
"Longitude": -122.2006
}

That’s it! We have geolocated an IP in Node.js!

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store