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
Optional settings:
timeout- milliseconds to wait for an answer (default10000;0waits indefinitely)retries- how many times to retry a single lookup that failed for a passing reason: a network error, a timeout, the API being briefly unavailable, or the rate limit (default0). Bulk lookups are never retried, because they use your quota when the request arrives.retryDelayMs- milliseconds before the first retry, doubling for each further one (default250)
Both lookup and bulkLookup accept a signal (an AbortSignal) to cancel a request; a cancelled request returns an ABORTED error.
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. Results come back in the order of the addresses you asked for, one per address, so data[i] is the answer for ips[i]. An address that is not a valid IP address gets its own error result and is not sent, so it does not use your quota; the other addresses are looked up as usual.
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 providedRATE_LIMIT_EXCEEDED- Too many requests a minute from your address; retry shortly (your quota is not spent)NETWORK_ERROR- The API could not be reached (the message says why)TIMEOUT- The API did not answer withintimeoutABORTED- The request was cancelled through itssignalSERVICE_UNAVAILABLE- The API was briefly unavailable (502, 503 or 504); try again shortlyUNKNOWN_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 API key"- HTTP 401 response, when the API sends no message of its own"Quota exceeded"- HTTP 429 response, when the API sends no message of its own