JavaScript IP location guide
Fetch the location of one IP address from a browser or Node.js.
Look up an IP address in JavaScript
The request accepts one IPv4 or IPv6 address and asks for JSON. Replace the example address with the address you need to inspect.
const ip = '8.8.8.8'; // Replace with an IPv4 or IPv6 address.
const response = await fetch(
'https://api.ipquery.io/' + encodeURIComponent(ip) + '?format=json',
{ signal: AbortSignal.timeout(8000) }
);
if (!response.ok) throw new Error('IPQuery returned HTTP ' + response.status);
const data = await response.json();
if (data.ip !== ip || !data.location || !data.isp) {
throw new Error('Unexpected IPQuery response');
}
console.log(data.location.country, data.location.city, data.isp.asn);Run in a modern browser or Node.js with fetch and AbortSignal.timeout. Browser requests identify the address you provide; the own-IP endpoint identifies the browser connection.
Read the location and network fields
location.country and location.country_code identify the approximate country. location.city can be empty or imprecise. isp.asn identifies the network, which can differ from the person or company using the address.
For the address making the request, call https://api.ipquery.io/?format=json. A server request returns the server’s connecting IP; a browser request returns its visible connection.
Accuracy and security
IP geolocation is a network estimate, not a device location. VPNs, proxies, mobile networks and carrier gateways may place the address far from the user. Treat missing values as unknown rather than filling in a guess.
If your application reads X-Forwarded-For, trust it only when a proxy you control supplies it. Client-supplied headers can be forged. Validate addresses before making lookup requests and handle timeouts and HTTP errors separately from a successful result.
API reference