Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/public-ip
Get your public IP address - very fast!
https://github.com/sindresorhus/public-ip
Last synced: 27 days ago
JSON representation
Get your public IP address - very fast!
- Host: GitHub
- URL: https://github.com/sindresorhus/public-ip
- Owner: sindresorhus
- License: mit
- Created: 2014-09-14T22:02:04.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2024-03-18T07:00:50.000Z (8 months ago)
- Last Synced: 2024-05-22T18:23:54.464Z (6 months ago)
- Language: JavaScript
- Size: 84 KB
- Stars: 1,009
- Watchers: 19
- Forks: 64
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome - sindresorhus/public-ip - Get your public IP address - very fast! (JavaScript)
- awesome-nodejs-cn - public-ip - 获取你的公网 IP 地址 (包 / 命令行程序)
- awesome-nodejs - public-ip - Get your public IP address - very fast! ![](https://img.shields.io/github/stars/sindresorhus/public-ip.svg?style=social&label=Star) (Repository / Network)
- awesome-nodejs-cn - public-ip - **star:1009** 获取你的公共IP地址 (包 / 命令行程序)
- awesome-nodejs - public-ip - Get your public IP address. (Packages / Command-line apps)
- awesome-nodejs - public-ip - Get your public IP address - very fast! - ★ 341 (Command-line apps)
- awesome-node - public-ip - Get your public IP address. (Packages / Command-line apps)
- awesome-nodejs-cn - public-ip - 获取你的公共IP地址. (目录 / 命令行应用)
- awesome-nodejs - public-ip - 获取公网 IP 地址 (Uncategorized / Uncategorized)
- awesome-starts - sindresorhus/public-ip - Get your public IP address - very fast! (others)
README
# 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](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.
## Install
```sh
npm install public-ip
```## Usage
```js
import {publicIp, publicIpv4, publicIpv6} from 'public-ip';console.log(await publicIp()); // Falls back to IPv4
//=> 'fe80::200:f8ff:fe21:67cf'console.log(await publicIpv6());
//=> 'fe80::200:f8ff:fe21:67cf'console.log(await publicIpv4());
//=> '46.5.21.123'
```## API
### publicIp(options?)
Returns a `Promise` 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` 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` 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](https://github.com/major/icanhaz) service instead of the DNS query. [ipify.org](https://www.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.
```js
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
- [Sindre Sorhus](https://github.com/sindresorhus)
- [silverwind](https://github.com/silverwind)## Related
- [public-ip-cli](https://github.com/sindresorhus/public-ip-cli) - CLI for this module
- [internal-ip](https://github.com/sindresorhus/internal-ip) - Get your internal IP address