# public-ip

> Get your public IP address — very fast!

Latest version **8.0.0** (published 2025-09-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install public-ip
pnpm add public-ip
yarn add public-ip
bun add public-ip
```

## Health

**Score 58/100 (C)** — status: stable.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2025-09-21 |
| First published | 2014-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/public-ip) |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 2 |
| Unpacked size | 17.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1060 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | get, ip, ipv4, ipv6, address, external, public, machine, fast, opendns, dns |

## Links

- npm: https://www.npmjs.com/package/public-ip
- Repository: https://github.com/sindresorhus/public-ip
- Homepage: https://github.com/sindresorhus/public-ip#readme
- Issues: https://github.com/sindresorhus/public-ip/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/public-ip

## Dependencies (2)

- [is-ip](https://npm.io/package/is-ip.md) ^5.0.1
- [dns-socket](https://npm.io/package/dns-socket.md) ^4.2.2

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 8.0.0 (latest) — 2025-09-21
- 7.0.1 — 2024-07-26
- 7.0.0 — 2024-07-26
- 6.0.2 — 2024-03-18
- 6.0.1 — 2022-06-24
- 6.0.0 — 2022-06-13
- 5.0.0 — 2021-12-27
- 4.0.4 — 2021-05-29
- 4.0.3 — 2020-12-07
- 4.0.2 — 2020-07-10
- 4.0.1 — 2020-03-30
- 4.0.0 — 2019-11-22
- 3.2.0 — 2019-08-14
- 3.1.0 — 2019-04-24
- 3.0.0 — 2018-12-14
- … 20 more at https://npm.io/package/public-ip/versions

## 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 [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()); // Tries IPv6 first, 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<string>` with your public IPv4 or IPv6 address. Tries IPv6 first, then falls back to IPv4. Rejects on error or timeout.

### publicIpv4(options?)

Returns a `Promise<string>` with your public IPv4 address. Rejects on error or timeout.

### publicIpv6(options?)

Returns a `Promise<string>` with your public IPv6 address. Rejects on error or timeout.

#### 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://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 the operation is considered timed out. This applies to the entire operation, not individual requests.

##### signal

Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)

An `AbortSignal` to cancel the operation. If both `timeout` and `signal` are provided, the operation will be aborted when either the timeout is reached or the signal is aborted.

### Error Handling

```js
import {publicIpv4, IpNotFoundError} from 'public-ip';

try {
	const ip = await publicIpv4({timeout: 5000});
	console.log(ip);
} catch (error) {
	if (error instanceof IpNotFoundError) {
		console.log('Could not determine public IP address');
	} else if (error.message.includes('aborted')) {
		console.log('Request was cancelled');
	} else {
		console.log('An error occurred:', error.message);
	}
}
```

### IpNotFoundError

Error thrown when the public IP address could not be found.

## 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

---
_Source: https://npm.io/package/public-ip · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
