What is public-ip?
The public-ip npm package allows you to get your public IP address (IPv4 or IPv6) from different services. It supports both IPv4 and IPv6, and it can be used in Node.js environments.
What are public-ip's main functionalities?
Get IPv4 Address
This feature allows you to get your public IPv4 address. The code sample demonstrates how to use the public-ip package to fetch and log the IPv4 address.
const publicIp = require('public-ip');
(async () => {
console.log(await publicIp.v4());
})();
Get IPv6 Address
This feature allows you to get your public IPv6 address. The code sample demonstrates how to use the public-ip package to fetch and log the IPv6 address.
const publicIp = require('public-ip');
(async () => {
console.log(await publicIp.v6());
})();
Get IP Address with Options
This feature allows you to get your public IP address with additional options. The code sample demonstrates how to use the public-ip package to fetch the IPv4 address using only HTTPS services.
const publicIp = require('public-ip');
(async () => {
console.log(await publicIp.v4({ onlyHttps: true }));
})();
Other packages similar to public-ip
ipify
The ipify package is another tool to get your public IP address. It is similar to public-ip but focuses on using the ipify service to fetch the IP address. It supports both IPv4 and IPv6.
external-ip
The external-ip package provides a way to get your public IP address by querying multiple services and returning the first successful response. It offers more flexibility in terms of service selection compared to public-ip.
public-ip-cli
The public-ip-cli package is a command-line interface for the public-ip package. It allows you to fetch your public IP address directly from the command line, making it convenient for quick checks without writing code.
public-ip
Get your public IP address - very fast!
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(await publicIp());
console.log(await publicIpv6());
console.log(await publicIpv4());
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.
import {publicIpv6} from 'public-ip';
await publicIpv6({
fallbackUrls: [
'https://ifconfig.co/ip'
]
});
timeout
Type: number
Default: 5000
The time in milliseconds until a request is considered timed out.
IpNotFoundError
Error thrown when the public IP address could not be found.
CancelError
Error thrown when the operation was canceled.
Maintainers
Related