Tutorialsteacher

Follow Us

How to get Geolocation from IP address

Sometimes you may need to get the real-world geographic location information from the visitor's IP address in your web application. IP Geolocation API ipgeolocationapi.com is a free real-time IP to Geolocation JSON API, which provides you the real-world geographic location of an Internet-connected computer terminal or device.

IP Geolocation API is free and easy to use and comes under MIT license. Just send HTTP GET request to https://api.ipgeolocationapi.com with different parameters and get the JSON result. For example, the following HTTP GET request will give you a location of the specified IP address.

HTTP GET Request
https://api.ipgeolocationapi.com/geolocate/91.213.103.0 HTTP/1.1
JSON result:
{
    "continent": "Europe",
    "address_format": "{{recipient}}
{{street}}
{{postalcode}} {{city}}
{{country}}",
    "alpha2": "DE",
    "alpha3": "DEU",
    "country_code": "49",
    "international_prefix": "00",
    "ioc": "GER",
    "gec": "GM",
    "name": "Germany",
    "national_destination_code_lengths": [
        2,
        3,
        4,
        5
    ],
    "national_number_lengths": [
        6,
        7,
        8,
        9,
        10,
        11
    ],
    "national_prefix": "0",
    "number": "276",
    "region": "Europe",
    "subregion": "Western Europe",
    "world_region": "EMEA",
    "un_locode": "DE",
    "nationality": "German",
    "eu_member": true,
    "eea_member": true,
    "vat_rates": {
        "standard": 19,
        "reduced": [
            7
        ],
        "super_reduced": null,
        "parking": null
    },
    "postal_code": true,
    "unofficial_names": [
        "Germany",
        "Deutschland",
        "Allemagne",
        "Alemania",
        "ドイツ",
        "Duitsland"
    ],
        "languages_official": [
        "de"
    ],
        "languages_spoken": [
        "de"
    ],
        "geo": {
            "latitude": 51.165691,
            "latitude_dec": "51.20246505737305",
            "longitude": 10.451526,
            "longitude_dec": "10.382203102111816",
            "max_latitude": 55.0815,
            "max_longitude": 15.0418962,
            "min_latitude": 47.2701115,
            "min_longitude": 5.8663425,
            "bounds": {
            "northeast": {
                "lat": 55.0815,
                "lng": 15.0418962
            },
            "southwest": {
                "lat": 47.2701115,
                "lng": 5.8663425
            }
        }
    },
    "currency_code": "EUR",
    "start_of_week": "monday"
}

The following request returns the country information specified by the ISO country code.

HTTP GET Request:
https://api.ipgeolocationapi.com/countries/us HTTP/1.1

The above request will return the information about USA since we passed country code parameter "us".

JSON Result:
{
    "continent": "North America",
    "address_format": "{{recipient}}
{{street}}
{{city}} {{region_short}} {{postalcode}}
{{country}}",
    "alpha2": "US",
    "alpha3": "USA",
    "country_code": "1",
    "international_prefix": "011",
    "ioc": "USA",
    "gec": "US",
    "name": "United States of America",
    "national_destination_code_lengths": [
        3
    ],
    "national_number_lengths": [
        10
    ],
    "national_prefix": "1",
    "number": "840",
    "region": "Americas",
    "subregion": "Northern America",
    "world_region": "AMER",
    "un_locode": "US",
    "nationality": "American",
    "postal_code": true,
    "unofficial_names": [
        "United States",
        "Vereinigte Staaten von Amerika",
        "États-Unis",
        "Estados Unidos",
        "アメリカ合衆国",
        "Verenigde Staten"
    ],
    "languages_official": [
        "en"
    ],
    "languages_spoken": [
        "en"
    ],
    "geo": {
        "latitude": 37.09024,
        "latitude_dec": "39.44325637817383",
        "longitude": -95.712891,
        "longitude_dec": "-98.95733642578125",
        "max_latitude": 71.5388001,
        "max_longitude": -66.885417,
        "min_latitude": 18.7763,
        "min_longitude": 170.5957,
        "bounds": {
            "northeast": {
                "lat": 71.5388001,
                "lng": -66.885417
            },
            "southwest": {
                "lat": 18.7763,
                "lng": 170.5957
            }
        }
    },
    "currency_code": "USD",
    "start_of_week": "sunday"
}

The following request will return the estimated geolocation of the visitor without detecting their actual IP address.

HTTP GET Request:
https://api.ipgeolocationapi.com/geolocate HTTP/1.1
JSON Result:
{
    "continent": "Asia",
    "alpha2": "UZ",
    "alpha3": "UZB",
    "country_code": "998",
    "international_prefix": "810",
    "ioc": "UZB",
    "gec": "UZ",
    "name": "Uzbekistan",
    "national_destination_code_lengths": [
        2
    ],
    "national_number_lengths": [
        9
    ],
    "national_prefix": "8",
    "number": "860",
    "region": "Asia",
    "subregion": "Central Asia",
    "world_region": "EMEA",
    "un_locode": "UZ",
    "nationality": "Uzbekistani",
    "postal_code": true,
    "unofficial_names": [
        "Uzbekistan",
        "Usbekistan",
        "Ouzbékistan",
        "Uzbekistán",
        "ウズベキスタン",
        "Oezbekistan"
    ],
    "languages_official": [
        "uz",
        "ru"
    ],
    "languages_spoken": [
        "uz",
        "ru"
    ],
    "geo": {
        "latitude": 41.377491,
        "latitude_dec": "41.77239227294922",
        "longitude": 64.585262,
        "longitude_dec": "63.14588928222656",
        "max_latitude": 45.590075,
        "max_longitude": 73.148946,
        "min_latitude": 37.1722571,
        "min_longitude": 55.9982179,
        "bounds": {
            "northeast": {
                "lat": 45.590075,
                "lng": 73.148946
            },
            "southwest": {
                "lat": 37.1722571,
                "lng": 55.9982179
            }
        }
    },
    "currency_code": "UZS",
    "start_of_week": "monday"
}

Thus, it is free and easy to use IP Geolocation API. Contribute to IP Geolocation API source on Github.