The IP Lookup API provides location information for any valid IP address. It works with both IPv4 and IPv6 addresses.
HTTP GET
Returns the location of the ip address specified. The response contains both the geographical coordinates (latitude/longitude) as well as the city and country.
address
(required) - IP Address to query. Must be in the format A.B.C.D
. For example: 73.9.149.180
X-Api-Key
(required) - API Key associated with your account.
Live Demo!
https://api.api-ninjas.com/v1/iplookup?address=
{
"is_valid": true,
"country": "United States",
"country_code": "US",
"region_code": "IL",
"region": "Illinois",
"city": "Chicago",
"zip": "60620",
"lat": 41.7405,
"lon": -87.6587,
"timezone": "America/Chicago",
"isp": "Comcast Cable Communications, LLC",
"address": "73.9.149.180"
}
import requests
ip_addr = '73.9.149.180'
api_url = 'https://api.api-ninjas.com/v1/iplookup?address={}'.format(ip_addr)
response = requests.get(api_url, headers={'X-Api-Key': 'YOUR_API_KEY'})
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)
var ip_addr = '73.9.149.180'
$.ajax({
method: 'GET',
url: 'https://api.api-ninjas.com/v1/iplookup?address=' + ip_addr,
headers: { 'X-Api-Key': 'YOUR_API_KEY'},
contentType: 'application/json',
success: function(result) {
console.log(result);
},
error: function ajaxError(jqXHR) {
console.error('Error: ', jqXHR.responseText);
}
});
const request = require('request');
var ip_addr = '73.9.149.180';
request.get({
url: 'https://api.api-ninjas.com/v1/iplookup?address=' + ip_addr,
headers: {
'X-Api-Key': 'YOUR_API_KEY'
},
}, function(error, response, body) {
if(error) return console.error('Request failed:', error);
else if(response.statusCode != 200) return console.error('Error:', response.statusCode, body.toString('utf8'));
else console.log(body)
});
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.fasterxml.jackson.databind.ObjectMapper;
URL url = new URL("https://api.api-ninjas.com/v1/iplookup?address=73.9.149.180");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept", "application/json");
InputStream responseStream = connection.getInputStream();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(responseStream);
System.out.println(root.path("fact").asText());
let ip_addr = "73.9.149.180".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: "https://api.api-ninjas.com/v1/iplookup?address="+ip_addr!)!
var request = URLRequest(url: url)
request.setValue("YOUR_API_KEY", forHTTPHeaderField: "X-Api-Key")
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()