Method 1: Use new WebRTC method
Important: This method is now no longer supported by several browsers.
Continue the reading below to see another free alternative.
This method uses diafygi "STUN IP Address requests for WebRTC".
The good news is that these calls can't be blocked by ad blocking plugins and widgets because they are made outside of the XMLHttpRequest instance. Given that 20 to 30% of the internet users are browsing with Adblockers, this is perfect if you want to ensure aconsistent experience for all your users. Copy and paste the following code into your web page:
Now you can use the following function to get the IP as soon as it's found:
There are several downsides to using this method. Not only is it not supported by a few browsers, but it also lacks certain information that you can otherwise tell from an IP address, such as whether the IP address is using a proxy or VPN.
Method 2: Use Abstract's IP detection API
Abstract provides a free IP detection API that has a method to retrieve the IP of a visitor.
This simple to use, yet powerful API provides the following features:
- Retrieve the IP of a visitor
- Retrieve the location of a visitor
- Retrieve the location of an IP
The API is free to use and allow thousands of calls per month. Using it is pretty simple and only require an API key you can get for free by signing up here. The documentation can be found on the documentation page: ip geolocation documentation. In addition, you can see our review of the best IP geolocation API as well as try our "What is my IP address and location?" tool for non-technical users.