Get country from IP address using MaxMind GeoLite2 binary database.
$ npm install @risan/ip-to-country
const ipToCountry = require("@risan/ip-to-country");
(async () => {
try {
const result = await ipToCountry("123.45.6.7");
if (result) {
console.log(result);
} else {
console.log("IP address not found.");
}
} catch(error) {
console.error(error.message);
}
})();
ipToCountry(ip)
ip
(String
): The IP address to lookup.
It returns a Promise
which when resolved contains undefined
or an Object
. undefined
means the given IP address is not found in the database. Here's an example of the resolved country object:
{
code: "ID",
name: "Indonesia",
flag_emoji: "🇮🇩",
geoname_id: 1643084,
names: {
de: "Indonesien",
en: "Indonesia",
es: "Indonesia",
fr: "Indonésie",
ja: "インドネシア共和国",
"pt-BR": "Indonésia",
ru: "Индонезия",
"zh-CN": "印度尼西亚"
},
continent: {
name: "Asia",
code: "AS",
geoname_id: 6255147,
names: {
de: "Asien",
en: "Asia",
es: "Asia",
fr: "Asie",
ja: "アジア",
"pt-BR": "Ásia",
ru: "Азия",
"zh-CN": "亚洲"
}
}
}
code
: Two-letter country code in ISO 3166-1 alpha-2 format.name
: The country name.flag_emoji
: The country flag emoji.geoname_id
: The GeoNames id.names
: The country name in various language.continent
: The continent data.
CC-BY-SA 4.0 · Risan Bagja Pradana
This product includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com. GeoLite2 Database and Contents Copyright (c) 2018 MaxMind, Inc.
The MaxMind GeoLite2 database incorporates GeoNames geographical data, which is made available under the Creative Commons Attribution 3.0 License.