Skip to content
Get your public IP address - very fast!
JavaScript TypeScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Create funding.yml May 28, 2019
mocks Add redundancy, remove `https` option, add `onlyHttps` option… (#34) Nov 22, 2019
.editorconfig Require Node.js 8 Dec 14, 2018
.gitattributes Require Node.js 8 Dec 14, 2018
.gitignore Require Node.js 8 Dec 14, 2018
.npmrc Require Node.js 8 Dec 14, 2018
.travis.yml Meta tweaks Apr 24, 2019
browser.js Cleanup Nov 22, 2019
index.d.ts Cleanup Nov 22, 2019
index.js Add redundancy, remove `https` option, add `onlyHttps` option… (#34) Nov 22, 2019
index.test-d.ts Add redundancy, remove `https` option, add `onlyHttps` option… (#34) Nov 22, 2019
license
package.json 4.0.0 Nov 22, 2019
readme.md Cleanup Nov 22, 2019
test-browser.js
test.js Cleanup Nov 22, 2019

readme.md

public-ip Build Status

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

const publicIp = require('public-ip');

(async () => {
	console.log(await publicIp.v4());
	//=> '46.5.21.123'

	console.log(await publicIp.v6());
	//=> 'fe80::200:f8ff:fe21:67cf'
})();

API

publicIp.v4(options?)

publicIp.v6(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.

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 aginst 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.

const publicIp = require('public-ip');

(async () => {
	await publicIp.v6({
		fallbackUrls: [
			'https://ifconfig.co/ip'
		]
	});
})();
timeout

Type: number
Default: 5000

The time in milliseconds until a request is considered timed out.

Maintainers

Related

You can’t perform that action at this time.