DEV Community

CP
CP

Posted on • Edited on

8 6

How To Get Client IP with Javascript / React

With VPN and Tor, IP may not be a reliable data anymore. Still there are times when we want to log that info. To my surprise, there is no built-in ReactJS (please correct me if I'm wrong about this) method that displays the client IP address. Definitely, you can retrieve that info from the request object "manually".

After trying a few popular packages, the simplest one that does the job is public-ip, super simple, and it provides support for fallback URLs when the IP is not found in the request.

import publicIp from "public-ip";

export const getClientIp = async () => await publicIp.v4({
  fallbackUrls: [ "https://ifconfig.co/ip" ]
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay