Prerequisites
- Node.js 18 or higher
- The
ip-api-iopackage installed (npm install ip-api-io) - A free ip-api.io API key
Geolocate one IP with lookup
Pass any IPv4 or IPv6 address, or omit the argument to geolocate the caller's own IP.
import { IpApiClient } from "ip-api-io";
const client = new IpApiClient({ apiKey: process.env.IP_API_IO_KEY });
const info = await client.lookup("8.8.8.8");
console.log(info.ip); // "8.8.8.8"
console.log(info.isp); // "Google LLC"
console.log(info.location.country); // "United States"
console.log(info.location.city); // "Mountain View"
console.log(info.location.latitude,
info.location.longitude);
console.log(info.location.timezone); // "America/Los_Angeles"
console.log(info.suspicious_factors.is_datacenter); // true
Resolve the IP of the machine making the request by calling lookup() with
no argument:
const me = await client.lookup();
console.log(me.ip, me.location.country); X-Forwarded-For header to lookup(ip) rather than relying on
the socket address. See the
SDK overview for an Express
example.
Geolocate up to 100 IPs with lookupBatch
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK
validates the limit client-side and throws a RangeError if the array is
empty or longer than 100.
const batch = await client.lookupBatch(["8.8.8.8", "1.1.1.1", "9.9.9.9"]);
console.log(batch.total_processed); // 3
console.log(batch.successful_lookups); // 3
console.log(batch.failed_lookups); // 0
for (const [ip, info] of Object.entries(batch.results)) {
console.log(ip, info.location.country, info.suspicious_factors.is_vpn);
}