Resolve any IP to a country
A free database optimized for fast IP address lookups that supports both IPv4 and IPv6.
What is ip2nation?
ip2nation is a free SQLite database that offers a quick way to map an IP to a country. The database is optimized to ensure fast lookups and is based on information from ARIN, APNIC, RIPE etc. You can download the raw database or query it using the API.
How accurate is it?
The database is based primarily on registration data (the location stated by the holder of each IP range). We estimate that the accuracy is around 98-99% for a randomly generated IP, but the accuracy may vary depending on your visitor base.
Subscribe to updates
If you would like an e-mail whenever we update ip2nation, fill in the form below.
Download
We have moved away from a MySQL datbase dump to SQLite, which is likely faster and easier to set up. The file is compressed as a standard ZIP archive to save bandwidth and is updated regularly.
Examples
If you have downloaded the SQLite database, you can query it locally for maximum performance. Below are examples for querying the data using PHP and SQL.
PHP (PDO with SQLite)
<?php
// query.php
$ip = '8.8.8.8'; // Replace with the IP you want to look up
$db = new PDO('sqlite:' . __DIR__ . '/ip2nation.sqlite', null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// Determine IP version and format the value to match the database schema
$isV4 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
$table =$isV4 ? 'ip2nation_v4' : 'ip2nation_v6';
$ipFormatted =$isV4 ? sprintf('%u', ip2long($ip)) : str_pad(bin2hex(inet_pton($ip)), 32, '0', STR_PAD_RIGHT);
// Query the flat table using the highly optimized <= boundary check
$stmt =$db->prepare("
SELECT t.country AS iso_code, COALESCE(c.country_name, 'Unknown') AS country_name
FROM {$table} t
LEFT JOIN countries c ON t.country = c.iso_code
WHERE t.ip_start <= ?
ORDER BY t.ip_start DESC
LIMIT 1
");
$stmt->execute([$ipFormatted]);
$result =$stmt->fetch();
if ($result &&$result['iso_code'] !== 'ZZ') {
echo "IP: {$ip} belongs to {$result['country_name']} ({$result['iso_code']})\n";
} else {
echo "IP: {$ip} is in an unallocated or reserved gap.\n";
}
?>
API
You can fetch country data directly via our live REST API endpoint, api.php. The API returns lightweight JSON responses containing the 2-letter ISO 3166 country_code and country_name.
Please do not abuse the API
The API is provided for free and without any need for credentials. It is not intended for large-volume websites. If you need to use the API on a large-volume website, please contact me.
JavaScript (Fetch)
const userIp = '73.213.110.172';
fetch(`https://ip2nation.com/api.php?ip=${userIp}`)
.then(response => response.json())
.then(data => {
if (data.status === 'error') {
console.error(data.message);
} else {
console.log(data.country_name); // Outputs: "United States"
}
})
.catch(error => console.error('Lookup failed:', error));
Contact Us
Have a question about integrating the database, found a discrepancy in our data, or just want to say hello? Send us a message using the form below.