How to get country code from IP using here.com API?

991 Views Asked by At

I am working on https://developer.here.com/documentation#services, to get Location of a user, trying to get country code from IP

2

There are 2 best solutions below

0
Laurent On

To my knowledge, Here.com does not provide an IP geolocation API. It mainly focus on forward and reverse geocoding.

However, there exists several IP geolocation providers: Ipregistry, IpInfo, IpData, Maxmind, Ip2location, etc.

Here is an example using Ipregistry (disclaimer, I am working for):

fetch('https://api.ipregistry.co/?key=tryout')
    .then(function (response) {
        return response.json();
    })
    .then(function (payload) {
        console.log(payload.location.country.code);
    });
0
alina On

Here.com does not provide location of the user from an IP address, for that you have to use an IP geolocation service, like our service, IPinfo.

If you have the IP address of the user it is quite easy to get the location information, such as:

  • City
  • Region
  • Country
  • Latitude and longitude

Here.com can take these information and process it as shown in the documentation.

If you chose to use IPinfo, you can make an API request to the ipinfo.io/<ip_address> endpoint. If you are going to lookup IP geolocations quite frequently, I recommend you sign up so that you can get 50,000 IP geolocation lookups for free per month.

If you want the full IP address location, try:

curl ipinfo.io/8.8.8.8

The 8.8.8.8 is a dummy IP Address. The response from the API lookup would be like so:

{
  "ip": "8.8.8.8",
  "hostname": "dns.google",
  "anycast": true,
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.4056,-122.0775",
  "org": "AS15169 Google LLC",
  "postal": "94043",
  "timezone": "America/Los_Angeles",
}

If you want just the country information, you can make an API call to the country endpoint like so:

curl ipinfo.io/8.8.8.8/country

This will return a string/text like:

US