IP Lookup in JavaScript: Fetch IP Address & Location
Learn how to get a user's IP address and geolocation in JavaScript. Includes browser fetch API and Node.js examples with free IP lookup APIs.
Finding a user's IP address from a client-side JavaScript application is not directly possible — browsers do not expose this information through any JavaScript API. However, you can retrieve it by making a request to an external service that reflects the caller's IP back in its response. This guide shows how to fetch IP address and location data in JavaScript, both in the browser and in Node.js.
How to Use the IP Lookup Tool
- Open the tool — It automatically detects your current IP address and shows geolocation data.
- Enter a specific IP — Type any IPv4 or IPv6 address to look up details for that IP.
- Copy the results — Use the data to understand what your JavaScript code will return for similar IP addresses.
- Implement in your code — Use the examples below to add IP lookup to your own application.
Get Visitor IP in the Browser (JavaScript)
// Fetch your own IP using a public API
async function getMyIP() {
const res = await fetch('https://api.ipify.org?format=json');
const data = await res.json();
return data.ip; // e.g., \"203.0.113.42\"
}
// Get IP + location data in one request
async function getIPInfo() {
const res = await fetch('https://ipapi.co/json/');
const data = await res.json();
return {
ip: data.ip,
country: data.country_name,
city: data.city,
region: data.region,
timezone: data.timezone,
isp: data.org,
};
}
getIPInfo().then(info => console.log(info));
Look Up Another IP Address in JavaScript
async function lookupIP(ipAddress) {
const res = await fetch(`https://ipapi.co/${ipAddress}/json/`);
if (!res.ok) throw new Error(`HTTP error: ${res.status}`);
const data = await res.json();
if (data.error) throw new Error(data.reason);
return data;
}
lookupIP('8.8.8.8')
.then(data => {
console.log(`Country: ${data.country_name}`);
console.log(`City: ${data.city}`);
console.log(`ISP: ${data.org}`);
})
.catch(console.error);
IP Lookup in Node.js Without a Library
const https = require('https');
function lookupIP(ip) {
return new Promise((resolve, reject) => {
https.get(`https://ipapi.co/${ip}/json/`, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve(JSON.parse(data)));
}).on('error', reject);
});
}
lookupIP('1.1.1.1').then(console.log);
Why Look Up IP Addresses in JavaScript?
- Personalization — Pre-select a user's country in a form or show content in their local language.
- Analytics — Enrich client-side analytics events with country or city data without a server round-trip.
- Fraud detection — Flag logins from unexpected countries and trigger additional verification steps.
- Rate limit awareness — Display \"Service not available in your region\" before an API call fails.
Rate Limits and Caching
Most free IP geolocation APIs have rate limits (45–1000 requests/day or per minute). For production use, cache IP lookup results in localStorage or a server-side cache (Redis, APCu) with a TTL of 24 hours. The same IP address rarely changes geolocation within a day, so caching dramatically reduces API calls and latency.
Look up any IP address manually using the IP Lookup tool to verify data before integrating it into your JavaScript application.