A lightweight, type-safe TypeScript package to easily detect and fetch the user's public IP address in browser and Node.js environments with minimal overhead.
Documentation β’ Installation β’ Usage β’ API
- π Lightweight - Minimal dependencies, optimized for performance
- π¦ Type-Safe - Full TypeScript support with proper type definitions
- π Browser & Node.js - Works seamlessly in both browser and server environments
- π§ Flexible - Supports custom API endpoints and User-Agent headers
- π‘οΈ Error Handling - Graceful error handling for network failures
- π± ESM Ready - Modern JavaScript module support
- π― Simple API - Clean, intuitive interface to get user's IP in seconds
npm install @iaceene/ip-address-getterOr with yarn:
yarn add @iaceene/ip-address-getterimport IP from '@iaceene/ip-address-getter';
// Create and fetch your IP address
const ip = await IP.New();
console.log(ip.GetIP()); // Output: XXX.XXX.XXX.XXXimport IP from '@iaceene/ip-address-getter';
// Create a new IP instance with default settings
const ip = await IP.New();
// Get the user's public IP address
const ipAddress = ip.GetIP();
console.log(ipAddress); // '203.0.113.42'import IP from '@iaceene/ip-address-getter';
// Detect user's IP with custom API endpoint and User-Agent
const ip = await IP.New({
URL: 'https://api.ipify.org?format=text',
UserAgent: 'Mozilla/5.0'
});
console.log(ip.GetIP()); // Get the user's detected IPFactory method to create and initialize an IP instance asynchronously.
Parameters:
args(optional) - Configuration options
Returns: Promise<IP> - An initialized IP instance
Example:
const ip = await IP.New();Retrieves the user's detected public IP address.
Returns: string - The user's public IP address
Example:
const addr = ip.GetIP();
console.log(addr); // User's IP: '203.0.113.42'The options object accepts the following properties:
| Property | Type | Default | Description |
|---|---|---|---|
URL |
string |
'http://ifconfig.me' |
API endpoint to detect and fetch the user's IP address from |
UserAgent |
string |
'curl/8.4.0' |
Custom User-Agent header for the request |
Example:
const options = {
URL: 'http://ifconfig.me',
UserAgent: 'curl/8.4.0'
};
const ip = await IP.New(options);The package includes built-in error handling that gracefully manages network failures when detecting user IP:
import IP from '@iaceene/ip-address-getter';
try {
const ip = await IP.New();
console.log(`User IP: ${ip.GetIP()}`);
} catch (error) {
console.error('Failed to fetch user IP address:', error);
}You can use any of these public IP APIs to detect user's IP with this package:
- ifconfig.me - Simple and fast (default)
- ipify.org - Returns IP in multiple formats
- ip.42.pl - Fast and reliable
- icanhazip.com - Returns just the IP address
- ident.me - Simple IP detection
import IP from '@iaceene/ip-address-getter';
// Detect user's IP using ipify.org
const ip = await IP.New({
URL: 'https://api.ipify.org?format=text'
});
console.log(`User's IP: ${ip.GetIP()}`);import IP from '@iaceene/ip-address-getter';
const ip = await IP.New({
UserAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
});
console.log(`User IP: ${ip.GetIP()}`);- Node.js 18.0.0 or higher
- TypeScript 5.0 or higher (for development)
Contributions are welcome! Please read our CONTRIBUTORS.md guide for details on our code of conduct and the process for submitting pull requests.
ISC Β© 2026 - iaceene