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.
Use IPQuery in Dashy
Dashy’s Public IP widget supports IPQuery without an API key. Add this entry to the widgets list in one of your Dashy sections:
- type: public-ip
options:
provider: ipqueryWith Dashy’s proxy settings off, the browser sends the request and the widget shows that connection’s public IP and approximate location. Setting useProxy: true at the top level of the widget, or enabling appConfig.widgetsAlwaysUseProxy, sends the request through Dashy’s server and shows the server’s public IP instead. Setting useProxy: false at the top level of the widget does not override the dashboard-wide setting.
The widget uses IPQuery’s own-IP endpoint. To inspect a different address, use the single-IP JavaScript request above.
Dashy Public IP widget optionsAccuracy 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