How to geolocate an IP in Javascript

IP address geolocation is an excellent tool to have around. It opens up such possibilities as user data gathering, region filtering, ad tailoring, and more. This tutorial is going to show you how to get this done with minimal effort. Let’s dive in.

Add this script tag to your HTML file or page to import our client.

<script src=""></script>

Call this function and provide an IP address to be geolocated.

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveValidateApiClient.IPAddressApi();var value = "value_example"; // String | IP address to geolocate, e.g. \"\".  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);

Done. Yup, it’s just that simple. Here is the information that the function will send back to you for your target IP:

"CountryCode": "string",
"CountryName": "string",
"City": "string",
"RegionCode": "string",
"RegionName": "string",
"ZipCode": "string",
"TimezoneStandardName": "string",
"Latitude": 0,
"Longitude": 0
Image for post

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