Get client IP Location in Python with pify and ipapi

User IP is being used normally to check where the client location is and customize the website for a better user experience, for example, if the customer is located in Malaysia, then we can just show all the promotions that related to the particular country, or if your product only available in Singapore, when the client from Malaysia accessing the website, we can just hide up that product.

In this post, I will share a code on Python how to get the client locations base on the IP address.

We will use the following 2 API to accomplish the features:

  1. pify: This API will help us know the IP address from where the request is coming.
  2. ipapi: This API will help us fetch location information for a particular IP address.

To interact with these APIs, we will be using the requests library from Python.

There are 2 steps that we need to get the client location, first is we get their IP address using the ipify.org API, then pass in that IP address to ipapi.co to get the detail location for that IP.

import requests


def get_ip():
    response = requests.get('https://api64.ipify.org?format=json').json()
    return response["ip"]


def get_location():
    ip_address = get_ip()
    response = requests.get(f'https://ipapi.co/{ip_address}/json/').json()
    location_data = {
        "ip": ip_address,
        "city": response.get("city"),
        "region": response.get("region"),
        "country": response.get("country_name")
    }
    return location_data


print(get_location())

get_ip() function

We make the GET request on https://api.ipify.org?format=json to get a JSON response that looks like this:

$ http https://api.ipify.org?format=json
{
    "ip": "61.6.123.147"
}

We store this response in a response variable which is nothing but a sort of Python dictionary with one key-value pair. So we returned the value of the key ip as response["ip"].

get_location() function

As per the API documentation of ipapi, we need to make a GET request on https://ipapi.co/{ip}/{format}/ to get location information for a particular IP address. {ip} is replaced by the IP address and {format} can be replaced with any of these – jsonjsonpxmlcsvyaml.

This function internally calls the get_ip() function to get the IP address and then makes a GET request on the URL with the IP address. This API returns a JSON response that looks like this:

{
    "asn": "AS9930",
    "city": "Kuala Lumpur",
    "continent_code": "AS",
    "country": "MY",
    "country_area": 329750.0,
    "country_calling_code": "+60",
    "country_capital": "Kuala Lumpur",
    "country_code": "MY",
    "country_code_iso3": "MYS",
    "country_name": "Malaysia",
    "country_population": 31528585,
    "country_tld": ".my",
    "currency": "MYR",
    "currency_name": "Ringgit",
    "in_eu": false,
    "ip": "61.6.123.147",
    "languages": "ms-MY,en,zh,ta,te,ml,pa,th",
    "latitude": 3.0604,
    "longitude": 101.6894,
    "org": "TIME dotCom Berhad No. 14, Jalan Majistret U126 Hicom Glenmarie Industrial Park 40150 Shah Al",
    "postal": "57000",
    "region": "Kuala Lumpur",
    "region_code": "14",
    "timezone": "Asia/Kuala_Lumpur",
    "utc_offset": "+0800",
    "version": "IPv4"
}

By default, the API returned lot of data, but we only interested on certain information such as city, region and country. So we need to create a dictionary call location_data to store the data that we interested on.

Lastly, we will call to get_location() to print out the data we get from the ipapi.co and the output will look like this:

{
    'ip': '61.6.123.147',
    'city': 'Kuala Lumpur',
    'region': 'Kuala Lumpur',
    'country': 'Malaysia'
}

Leave a Reply

google.com, pub-3772983857049267, DIRECT, f08c47fec0942fa0
%d bloggers like this: