How to geolocate an IP in Node.js
1 min readMay 25, 2019
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!