'Finding Location through IP address Nodejs mongodb

I'm trying to find the location of an IP address. The IP address will be sent to the API through a front end android/iOS application(I'm currently using a static IP address of my computer). I want to use the client IP address to then determine their, likely, physical location and send the location to MongoDB to be stored. Any help regarding this will be appreciated. Thanks in advance.



Solution 1:[1]

You can utilize GeoIP-lite to get the geodata from the ip address.

var geoip = require('geoip-lite');

var ip = "207.97.227.239";
var geo = geoip.lookup(ip);

console.log(geo);
/*{ range: [ 3479297920, 3479301339 ],
  country: 'US',
  region: 'TX',
  city: 'San Antonio',
  ll: [ 29.4889, -98.3987 ],
  metro: 641,
  zip: 78218 }*/

Solution 2:[2]

As of March 31 2018 the old freegeoip API is deprecated and a completely re-designed API is now accessible at http://api.ipstack.com. While the new API offers the same capabilities as the old one and also has the option of returning data in the legacy format, the API URL has now changed and all users are required to sign up for a free API Access Key to use the service.

source


You can use freegeoip.net :

freegeoip.net/{format}/{IP_or_hostname}

Sample GET http://freegeoip.net/json/207.97.227.239 :

{
  ip: "207.97.227.239",
  country_code: "US",
  country_name: "United States",
  region_code: "TX",
  region_name: "Texas",
  city: "San Antonio",
  zip_code: "78218",
  time_zone: "America/Chicago",
  latitude: 29.4889,
  longitude: -98.3987,
  metro_code: 641
}

Limitations :

You're allowed up to 10,000 queries per hour by default. Once this limit is reached, all of your requests will result in HTTP 403, forbidden, until your quota is cleared.

The freegeoip web server is free and open source so if the public service limit is a problem for you, download it and run your own instance.


If you only need country precision, you can use cloudflare ip geoloc, it ads an header to the request, so no extra API call.

Solution 3:[3]

You can use Apiip.net package for Node.js:

The usage is really simple:

const apiip = require('apiip.net')('your_api_key');

apiip
  .getLocation({
     ip: '67.250.186.196'
   })
  .then((results) => console.log(results))
  .catch((error) => console.error(error));

Or if you want, just use simple GET call:

https://apiip.net/api/check?ip=67.250.186.196&accessKey={your_api_key}

They provide a lot of info about the IP, not only the location:

{
  "ip": "67.250.186.196",
  "continentCode": "NA",
  "continentName": "North America",
  "countryCode": "US",
  "countryName": "United States",
  "countryNameNative": "United States",
  "city": "New York",
  "postalCode": "10001",
  "latitude": 40.8271,
  "longitude": -73.9359,
  "capital": "Washington D.C.",
  "phoneCode": "1",
  "countryFlagEmoj": "??",
  "countryFlagEmojUnicode": "U+1F1FA U+1F1F8",
  "isEu": false,
  "languages": {
    "en": {
      "code": "en",
      "name": "English",
      "native": "English"
    }
  },
  "currency": {
    "code": "USD",
    "name": "US Dollar",
    "symbol": "$",
    "number": "840",
    "rates": {
      "EURUSD": 1.11
     }
  },
  "timeZone": {
    "id": "America/New_York",
    "currentTime": "10/26/2021, 2:54:10 PM",
    "code": "EDT",
    "timeZoneName": "EDT",
    "utcOffset": -14400
  },
  "connection": {
    "asn": 12271,
    "isp": "Charter Communications Inc"
  },
  "security": {
    "isPublicProxy": false,
    "isResidentialProxy": false,
    "isTorExitNode": false,
    "network": "67.250.176.0/20"
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriel Bleu
Solution 2
Solution 3