cover-img

How to Get Location Information of an IP Address Using Python

A tutorial on how to fetch location information of an IP address using Requests and Python.

Draft

29 April, 2022

We often require to know the location for an IP address or maybe even ours. One other use-case for this is when you want to send login information to users for your website. In this blog, we’re going to see how we can know the location of an IP address using Python.

Get your tools ready

To accomplish the goal, we’ll be using two APIs mentioned below:

  1. ipify: 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’ll be using requests library in Python. If you're new to APIs, make sure you check out this tutorial to learn about them.

You can install this library using the pip command like this:

Once the library is installed, we’re good to go!

Get Location Information

As we discussed, we’ll first fetch our IP address from the first API. Then we’ll make use of this IP address to fetch location information for this particular IP address. So, we’ll have two functions.

In the above code, we have two functions- get_ip() and get_location(). Let's discuss each of them separately.

get_ip() function

As per the API documentation of ipify, we need to make a GET request on https://api.ipify.org?format=json to get a JSON response that looks like this:

We store this response in a variable which is nothing but a sort of Python dictionary with one key-value pair. Hence 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 - json, jsonp, xml, csv, yaml. 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 JSON response that looks like this:

We get a whole lot of data in the response. You can use whatever works for you. For this tutorial, we’ll just be using city, region and country. That's why we created a dictionary called location_data and stored all the data inside it and returned the same.

At last, we call the get_location() function and print the output. Our output will look like this:

Conclusion

In this blog, we learned how we can interact with web services to get location information for a particular IP address.

Thanks for reading!

img
Ashutosh Krishna
Learning something new daily

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.