How to get client IP address using JavaScript?
Last Updated :
21 Jun, 2025
Knowing a client's IP address can be useful for various purposes like personalizing content, tracking user activity, or offering location-based services. However, JavaScript running in the browser doesn’t have direct access to this information due to security reasons.
Using External APIs to Get the Client's IP Address
To overcome the limitation of JavaScript not being able to directly access the client's IP address, we use external APIs. These APIs handle the request and return the IP address to the client.
Some popular APIs include ipify and IPinfo:
1. Using ipify to Get the Client's IP Address
We can easily retrieve the client's IP address by using the ipify API. The getJSON() method of jQuery allows you to fetch the user's IP address for websites served over HTTPS (SSL-secured).
HTML
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Getting Client's IP</title>
<style>
p, h1 {
color: green;
}
</style>
</head>
<body>
<center>
<h1>GeeksforGeeks</h1>
<h3>Public IP Address of user is:</h3>
<p id="ip-address"></p>
</center>
<script>
document.addEventListener("DOMContentLoaded", function() {
fetch("https://api.ipify.org?format=json")
.then(response => response.json())
.then(data => {
document.getElementById("ip-address").textContent = data.ip;
})
.catch(error => {
console.error("Error fetching IP address:", error);
});
});
</script>
</body>
</html>
Output:
Getting the user's Public IP addressIn this example
- The page sends a request to the ipify API to get the user’s public IP address.
- The IP address is displayed in the paragraph (<p>) when the page loads.
- If there’s an error, it logs the error in the console.
2. Using IPinfo to Get the Client's IP Address
Similarly, we can use the IPinfo API to get the client's IP address. Using jQuery's get() method, you can pass a callback function to handle the response.
HTML
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Getting Client's IP</title>
<style>
div {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body>
<div>
<h1>GeeksforGeeks</h1>
<h3>Getting Client IP Address</h3>
<p id="ip-address"></p>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
fetch("https://ipinfo.io/json")
.then(response => response.json())
.then(data => {
document.getElementById("ip-address").textContent =
`IP Address: ${data.ip}`;
})
.catch(error => {
console.error("Error fetching IP address:", error);
document.getElementById("ip-address").textContent =
"Unable to retrieve IP address.";
});
});
</script>
</body>
</html>
Output:
In this example
- The HTML page has a heading and a paragraph to display the IP address.
- JavaScript sends a request to IPinfo API to get the client’s IP address.
- If successful, the IP address is shown in the paragraph.
- If there’s an error, an error message is displayed instead.
- The content is updated once the page is fully loaded.
Note: Sometimes, it will not work on Google Chrome for the default setting. It supports Firefox and Microsoft Edge.
Why Can’t JavaScript Access the Client’s IP Address Directly?
Here is the some reasons :
- JavaScript runs in a sandbox with limited access.
- It protects against security vulnerabilities.
- Prevents unauthorized data collection.
- Safeguards user anonymity and privacy.
Security and Privacy Concerns
- Privacy Risks: Exposing a user’s IP address can compromise their privacy, as it can be used to track or profile them across different websites.
- Misuse Potential: Without proper controls, IP addresses can be misused for activities like targeted advertising or malicious attacks.
- Legal Requirements: Regulations like GDPR and CCPA require websites to obtain explicit consent from users before collecting personal information like their IP address.
- Use of VPNs/Proxies: Many users mask their real IP address using VPNs or proxies, which can make the IP address unreliable for accurate tracking or location identification.
Conclusion
JavaScript can't directly access the client's IP address due to privacy and security reasons. To get this information, developers use third-party APIs like ipify and IPinfo, which securely provide the client's IP. While these APIs are simple to use, it’s important to respect privacy regulations and ensure data protection when handling IP addresses.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read