You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Node.js, it queries the DNS records of OpenDNS, Google DNS, and HTTPS services to determine your IP address. In browsers, it uses the excellent icanhaz and ipify services through HTTPS.
Install
npm install public-ip
Usage
import{publicIp,publicIpv4,publicIpv6}from'public-ip';console.log(awaitpublicIp());// Falls back to IPv4//=> 'fe80::200:f8ff:fe21:67cf'console.log(awaitpublicIpv6());//=> 'fe80::200:f8ff:fe21:67cf'console.log(awaitpublicIpv4());//=> '46.5.21.123'
API
publicIp(options?)
Returns a Promise<string> with your public IPv4 or IPv6 address. Rejects on error or timeout.
A .cancel() method is available on the promise, which can be used to cancel the request.
publicIpv6(options?)
Returns a Promise<string> with your public IPv6 address. Rejects on error or timeout.
A .cancel() method is available on the promise, which can be used to cancel the request.
publicIpv4(options?)
Returns a Promise<string> with your public IPv4 address. Rejects on error or timeout.
A .cancel() method is available on the promise, which can be used to cancel the request.
options
Type: object
onlyHttps
Type: boolean
Default: false
Use a HTTPS check using the icanhazip.com service instead of the DNS query. ipify.org is used as a fallback if icanhazip.com fails. This check is much more secure and tamper-proof, but also a lot slower. This option is only available in the Node.js version. The default behaviour is to check against DNS before using HTTPS fallback. If set to true, it will only check against HTTPS.
fallbackUrls
Type: string[]
Default: []
Add your own custom HTTPS endpoints to get the public IP from. They will only be used if everything else fails. Any service used as fallback must return the IP as a plain string.