Description
The IP Geolocation API supports over 190+ countries. It allows you to submit an IP address and get its location data such as the country, city, region, etc.
This API is so powerful that you do not need to specify the IP address of your current location. It automatically detects the IP address the query came from.
In abstract-python
, the IpAPI
class is used to make requests to Abstract’s IP Geolocation API.
The IpAPI.ip_info()
method is used to get Ip address info as seen below:
IpAPI.ip_info(Optional[ip_address, fields])
Query Parameters
Below are the query parameters for this method:
The ip_address you want to get location data for. The IpAPI supports both IPv4 and IPv6 addresses.
If this field is left blank, the response will contain the location data of the IP address from which the request was made from.
This parameter is used to limit the response to a few fields. For instance, you may prefer the response to only contain the country, city and region.
You can add this in your query as a comma separated list like this: fields="country,city,region"
Usage
Below is a sample code to query the API:
from abstract_python import IpAPI
api_key = "your-ip-api-key"
api = IpAPI(IP_API_KEY)
print(ip.ip_info())
Below is the response object:
{
'ip_address': '105.113.58.23',
'city': 'Lagos',
'city_geoname_id': 2332459,
'region': 'Lagos',
'region_iso_code': 'LA',
'region_geoname_id': 2332453,
'postal_code': None,
'country': 'Nigeria',
'country_code': 'NG',
'country_geoname_id': 2328926,
'country_is_eu': False,
'continent': 'Africa',
'continent_code': 'AF',
'continent_geoname_id': 6255146,
'longitude': 3.3903,
'latitude': 6.4474,
'security': {'is_vpn': False},
'timezone': {
'name': 'Africa/Lagos',
'abbreviation': 'WAT',
'gmt_offset': 1,
'current_time': '00:56:55',
'is_dst': False
},
'flag': {
'emoji': '🇳�'',
'unicode': 'U+1F1F3 U+1F1EC',
'png': 'https://staticc.abstractapi.com/country-flags/NG_flag.png',
'svg': 'https://static.abstractapi.com/country-flags/NG_flag.svg'},
'currency': {'currency_name': 'Naira', 'currency_code': 'NGN'},
'connection': {
'autonomous_system_number': 36873,
'autonomous_system_organization': 'Celtel Nigeria Limited t.a ZAIN',
'connection_type': 'Corporate',
'isp_name': 'Airtel Networks Limited',
'organization_name': 'Airtel Networks Limited'
},
'status': 200
}
Response Fields
The ip_address of the response data.
The ip address city’s name
The state or province where the city is located
State or province’s ISO 3166-2 code.
State or province’s geoname ID.
The IP address ZIP or postal code
The IP address country’s name
Country’s ISO 3166-1 alpha-2 code.
A boolean value that confirms if the country is in the European Union (EU).
True if yes, and False is not.
The IP address continent’s name
A 2 letter continent code: AF, AS, EU, NA, OC, SA, AN.
A decimal value of the IP address longitude
A decimal value of the IP address latitude
A boolean value that confirms whether the IP address is using from a VPN or using a proxy
Timezone’s name from the IANA Time Zone Database.
Timezone’s abbreviation, also from the IANA Time Zone Database.
Timezone’s offset from Greenwich Mean Time (GMT).
Current time in the local time zone.
True if the location is currently in Daylight Savings Time (DST)
Link to a hosted version of the country’s flag in SVG format.
Link to a hosted version of the country’s flag in PNG format.
Link to a hosted version of the country’s flag in SVG format.
Country’s flag in unicode.
The currency’s code in ISO 4217 format.
connection.connection_type
Type of network connection: Dialup, Cable/DSL, Cellular, Corporate.
connection.autonomous_system_number
Autonomous System number.
The Internet Service Provider (ISP) name you used when sending the query.
connection.organization_name
Organization name.
Specify Ip address
You can also specify a unique IP address in your query to get its location data.
Below is a code snippet to query the API by Ip address:
from abstract_python import IpAPI
api_key = "your-ip-api-key"
ip_address = "105.113.58.23"
api = IpAPI(IP_API_KEY)
print(ip.ip_info(ip_address))
The code above uses the IP address in the previous response. Below is the response object for the code above:
{
'ip_address': '105.113.58.23',
'city': 'Lagos',
'city_geoname_id': 2332459,
'region': 'Lagos',
'region_iso_code': 'LA',
'region_geoname_id': 2332453,
'postal_code': None,
'country': 'Nigeria',
'country_code': 'NG',
'country_geoname_id': 2328926,
'country_is_eu': False,
'continent': 'Africa',
'continent_code': 'AF',
'continent_geoname_id': 6255146,
'longitude': 3.3903,
'latitude': 6.4474,
'security': {'is_vpn': False},
'timezone': {
'name': 'Africa/Lagos',
'abbreviation': 'WAT',
'gmt_offset': 1,
'current_time': '00:56:55',
'is_dst': False
},
'flag': {
'emoji': '🇳�'',
'unicode': 'U+1F1F3 U+1F1EC',
'png': 'https://staticc.abstractapi.com/country-flags/NG_flag.png',
'svg': 'https://static.abstractapi.com/country-flags/NG_flag.svg'},
'currency': {'currency_name': 'Naira', 'currency_code': 'NGN'}, 'connection': {'autonomous_system_number': 36873, 'autonomous_system_organization': 'Celtel Nigeria Limited t.a ZAIN', 'connection_type': 'Corporate', 'isp_name': 'Airtel Networks Limited', 'organization_name': 'Airtel Networks Limited'}, 'status': 200}
Limit response fields
The IpAPI also allows you to limit the response data to certain fields.
Below is an example code to limit response data using the fields
argument:
from abstract_python import IpAPI
api_key = "your-ip-api-key"
ip_address = "105.113.58.23"
fields = "ip_address, city, region, region_geoname_id, country"
api = IpAPI(IP_API_KEY)
print(ip.ip_info(ip_address, fields=fields))
Below is the response object:
{
'ip_address': '105.113.58.23',
'city': 'Lagos',
'region': 'Lagos',
'region_geoname_id': 2332453,
'country': 'Nigeria',
'status': 200
}
Error messages
Refer to this page for all possible error messages you may encounter while using the IpAPI
class.