HOWTO · JavaScript
How to Get Client IP Address in JavaScript
Learn how browser JavaScript can retrieve a service-observed public IP address with ipify, ipinfo, or Abstract.
On this page
Browser JavaScript cannot read a device’s public IP address directly. It can send a request to an IP lookup service, which reports the public address it observes for that request. This is different from a private address used on a local network. The request also shares the visitor’s IP address with the third-party provider, so choose a service whose data practices suit your application.
The examples below use fetch, which is available in modern browsers. They cover three providers: ipify for an IP-only response, ipinfo for IP and location fields, and Abstract for geolocation data. Choose based on the fields your application needs, whether the provider requires credentials, and how you will protect any credentials. These requests run from the visitor’s browser, so the provider sees the visitor’s public IP. Provider availability, response fields, and access rules can change; consult the provider’s current documentation before deployment.
Get a public IP address with ipify
ipify provides a JSON endpoint at https://api.ipify.org?format=json. The response contains an ip property. This example reads that value and displays it. The ipify API repository contains the project source.
// Use a representative response so the parsing path is deterministic.
globalThis.fetch = async () => ({
ok: true,
json: async () => ({ ip: '203.0.113.42' }),
});
(async () => {
try {
const response = await fetch('https://api.ipify.org?format=json');
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
console.log(data.ip);
} catch (error) {
console.log('Could not retrieve the public IP:', error.message);
}
})();
203.0.113.42
The request checks the HTTP status before parsing JSON, then reads the ip field. If it fails, inspect the browser’s network panel for connectivity, service, or content-security-policy details. This method returns the address observed by ipify; it does not reveal the device’s private LAN address. Use it when an IP-only response is enough and no extra location fields are needed.
Get IP and location data with ipinfo
ipinfo returns an ip field and may provide location-related fields such as city, region, country, and loc. The example uses a representative response to show how to read those fields; its local fetch substitute keeps the demonstration independent of network access. For a live request, use the JSON endpoint shown below. Available fields and access can depend on the request and current provider requirements, so review the provider’s documentation for credentials and plan limits instead of relying on fixed quota figures.
// Replace this local response with the provider's live response in your app.
globalThis.fetch = async () => ({
ok: true,
json: async () => ({
ip: '203.0.113.42',
city: 'Austin',
region: 'Texas',
country: 'US',
loc: '30.2672,-97.7431',
}),
});
(async () => {
try {
const response = await fetch('https://ipinfo.io/json');
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
console.log('IP:', data.ip);
console.log('Location:', data.city, data.region, data.country);
} catch (error) {
console.log('Could not retrieve IP information:', error.message);
}
})();
IP: 203.0.113.42
Location: Austin Texas US
Use the ip field when you only need the observed address. The additional fields can support a broad regional display, but location data is approximate and depends on the provider’s data; it does not identify a precise device location. If the request fails, check the network response and whether the endpoint’s current access requirements apply to your use.
Get IP and geolocation data with Abstract
Abstract provides an IP geolocation endpoint. Its request uses an API key, so create a key through the provider and replace the placeholder below. The URL API encodes the key as a query parameter. This example uses a representative response and prints its IP and location fields; the local fetch substitute makes the parsing example deterministic. For a live request, check the provider’s current documentation for account access, quotas, and the response schema before selecting fields for your interface. A key embedded in browser JavaScript is visible to visitors; use a server-side proxy if the key must remain private.
// Replace this local response with the provider's live response in your app.
globalThis.fetch = async () => ({
ok: true,
json: async () => ({
ip_address: '203.0.113.42',
city: 'Austin',
region: 'Texas',
country: 'United States',
latitude: 30.2672,
longitude: -97.7431,
}),
});
(async () => {
const apiKey = 'YOUR_ABSTRACT_API_KEY';
const url = new URL('https://ipgeolocation.abstractapi.com/v1/');
url.searchParams.set('api_key', apiKey);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
console.log('IP:', data.ip_address);
console.log('Location:', data.city, data.region, data.country);
console.log('Coordinates:', data.latitude, data.longitude);
} catch (error) {
console.log('Could not retrieve geolocation data:', error.message);
}
})();
IP: 203.0.113.42
Location: Austin Texas United States
Coordinates: 30.2672 -97.7431
The example reads the IP and location fields from the returned object. The local sample demonstrates parsing only; it does not verify a live service response. A missing, rejected, or restricted API key can lead to an error response; network and service failures can also prevent a result. Handle those cases in the application and inspect the response and provider documentation when adapting the example to display particular fields. Choose Abstract when its current response and access terms fit the location data your application needs.