TypeScript Library
The ipflare npm package offers a simple and efficient way to retrieve geolocation information for IP addresses using the IP Flare API. The library provides comprehensive TypeScript support, robust error handling, and advanced features for production applications.
Notice!
X-API-Key
header of your requests.Features
- Result-based Error Handling: All methods return a Result type with explicit success/failure states
- IPv4 & IPv6 Support: Comprehensive validation for both IPv4 and IPv6 addresses
- Type Safety: Full TypeScript support with type guards and discriminated unions
- Input Validation: Client-side validation prevents invalid requests and provides immediate feedback
- Structured Error Types: Specific error messages for different scenarios with structured error types
- Bulk Operations: Process up to 500 IP addresses in a single request
- Configurable: Custom timeout, base URL, and other configuration options
- Production Ready: Comprehensive testing (96%+ coverage) and enterprise-grade reliability
Installation
Quick Start
After installing the package, you can use it in your JavaScript or TypeScript projects to fetch geolocation data with comprehensive error handling and type safety. The library uses a Result-based approach that eliminates the need for try-catch blocks.
Initialize the Client
Examples
The library uses a Result-based API that eliminates try-catch blocks and provides structured error handling with explicit success/failure states.
Single IP Lookup
The following example demonstrates IP lookups with automatic validation for both IPv4 and IPv6 addresses and structured error information.
Bulk IP Lookup
Bulk lookup for multiple IP addresses (up to 500 per request) with structured error handling:
Lookup with Additional Fields
To retrieve additional fields like ASN and ISP information, pass the include options. Visit the Geolocation page for more information about available fields.
Error Handling
The library provides comprehensive error handling with specific error types for different scenarios. No try-catch blocks are needed:
Type Guards
The library provides type guard functions to safely work with results and discriminate between successful and error responses:
TypeScript Types
The ipflare library provides comprehensive TypeScript types for all operations:
Advanced Features
Input Validation
The library performs comprehensive client-side validation to prevent invalid requests and provide immediate feedback:
Error Reference
Error Types
The library provides structured error types that match the actual API responses:
INVALID_IP_ADDRESS
- Invalid IP address formatRESERVED_IP_ADDRESS
- IP address is reserved (private/local)GEOLOCATION_NOT_FOUND
- Geolocation data not available for this IPINTERNAL_SERVER_ERROR
- Server-side error occurredINVALID_INPUT
- Invalid input parametersUNAUTHORIZED
- Invalid or missing API keyQUOTA_EXCEEDED
- API quota exceededNO_API_KEY_PROVIDED
- No API key was providedNETWORK_ERROR
- Network connectivity issuesVALIDATION_ERROR
- Client-side input validation errorsUNKNOWN_ERROR
- Unexpected errors
Result Type Structure
All methods return a Result type with the following structure:
Common Error Messages
The library provides specific error messages for different scenarios:
"API key is required"
- No API key provided"API key must be a string"
- Invalid API key type"API key cannot be empty"
- Empty API key"IP address is required"
- No IP address provided"IP address must be a string"
- Invalid IP type"Invalid IP address format: [ip]"
- Invalid IP format"IPs must be an array"
- Invalid bulk IPs type"At least one IP address is required"
- Empty IP array"Maximum of 500 IPs per request allowed"
- Too many IPs"Invalid IP addresses found: [ips]"
- Invalid IPs in bulk request request"Invalid API key"
- HTTP 401 response"Quota exceeded"
- HTTP 429 response (usage quota exceeded)