How to get a client's IP address using JavaScript
This guide will show you how to get your local, public and IPv6 IP Addresses in pure Javascript without making server side requests as well as an example with our API.

Pure Javascript with WebRTC

This is based on https://github.com/diafygi/webrtc-ips. You can copy & paste the example below into your Dev console to see it in action.
Firefox and Chrome have implemented WebRTC that allow requests to STUN servers be made that will return the local and public IP addresses for the user. These request results are available to javascript, so you can now obtain a users local and public IP addresses in javascript. This demo is an example implementation of that.
Additionally, these STUN requests are made outside of the normal XMLHttpRequest procedure, so they are not visible in the developer console or able to be blocked by plugins such as AdBlockPlus or Ghostery. This makes these types of requests available for online tracking if an advertiser sets up a STUN server with a wildcard domain.
1
//get the IP addresses associated with an account
2
function getIPs(callback){
3
var ip_dups = {};
4
//compatibility for firefox and chrome
5
var RTCPeerConnection = window.RTCPeerConnection
6
|| window.mozRTCPeerConnection
7
|| window.webkitRTCPeerConnection;
8
var useWebKit = !!window.webkitRTCPeerConnection;
9
//bypass naive webrtc blocking using an iframe
10
if(!RTCPeerConnection){
11
//NOTE: you need to have an iframe in the page right above the script tag
12
//
13
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
14
//<script>...getIPs called in here...
15
//
16
var win = iframe.contentWindow;
17
RTCPeerConnection = win.RTCPeerConnection
18
|| win.mozRTCPeerConnection
19
|| win.webkitRTCPeerConnection;
20
useWebKit = !!win.webkitRTCPeerConnection;
21
}
22
//minimal requirements for data connection
23
var mediaConstraints = {
24
optional: [{RtpDataChannels: true}]
25
};
26
var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
27
//construct a new RTCPeerConnection
28
var pc = new RTCPeerConnection(servers, mediaConstraints);
29
function handleCandidate(candidate){
30
//match just the IP address
31
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
32
var ip_addr = ip_regex.exec(candidate)[1];
33
//remove duplicates
34
if(ip_dups[ip_addr] === undefined)
35
callback(ip_addr);
36
ip_dups[ip_addr] = true;
37
}
38
//listen for candidate events
39
pc.onicecandidate = function(ice){
40
//skip non-candidate events
41
if(ice.candidate)
42
handleCandidate(ice.candidate.candidate);
43
};
44
//create a bogus data channel
45
pc.createDataChannel("");
46
//create an offer sdp
47
pc.createOffer(function(result){
48
//trigger the stun server request
49
pc.setLocalDescription(result, function(){}, function(){});
50
}, function(){});
51
//wait for a while to let everything done
52
setTimeout(function(){
53
//read candidate info from local description
54
var lines = pc.localDescription.sdp.split('\n');
55
lines.forEach(function(line){
56
if(line.indexOf('a=candidate:') === 0)
57
handleCandidate(line);
58
});
59
}, 1000);
60
}
61
//log IP addresses
62
getIPs(function(ip){
63
//local IPs
64
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/))
65
console.log("Local IPs: "+ip);
66
//IPv6 addresses
67
else if (ip.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/))
68
console.log("IPv6 Addresses: "+ip);
69
//assume the rest are public IPs
70
else
71
console.log("Public IPs: "+ip);
72
});
Copied!

With the ipdata API

The advantage of using our API is you can expect this method to work reliably across all browsers and it is super-fast. We run out of 11 AWS data centers around the world for millisecond-optimized performance.
1
$.get("https://api.ipdata.co?api-key=test", function(response) {
2
console.log(response.ip);
3
}, "jsonp");
Copied!
Last modified 2yr ago