Disclosure: I represent IP-API.io. The examples below use its public API contract. The design applies to other metered enrichment APIs too. No authenticated lookup was made while preparing this example.
A free API plan still has a request budget. If an application enriches an IP on every page load, the integration can exhaust a small allowance before there is enough traffic to learn whether the enrichment is useful.
For a concrete example, IP-API.io's current Free plan includes 1,000 IP requests and 100 email validations per monthly billing period. Those are separate allowances. Four IP lookups for each of 250 sessions consume the full IP allowance. A batch request is not a reason to assume that 100 addresses count as one lookup: check the provider's billing rules before estimating cost.
The first decision is where enrichment changes a useful outcome. An account creation event may need network context; a stylesheet request usually does not. Write that decision down before putting the API call into shared request middleware.
Make the call on your server
Keep the API key in server configuration. The documented explicit-address endpoint is GET /api/v1/ip/{ip}, with api_key as a query parameter. An illustrative request is:
import { isIP } from 'node:net';
export async function lookupIP(ip, apiKey, fetchImpl = fetch) {
if (!isIP(ip)) throw new TypeError('Expected an IPv4 or IPv6 address');
if (!apiKey) throw new TypeError('Missing API key');
const url = new URL(`/api/v1/ip/${encodeURIComponent(ip)}`, 'https://ip-api.io');
url.searchParams.set('api_key', apiKey);
try {
const response = await fetchImpl(url, {
signal: AbortSignal.timeout(1500),
});
if (!response.ok) {
return { state: 'unavailable', status: response.status };
}
return { state: 'available', data: await response.json() };
} catch {
return { state: 'unavailable' };
}
}
This function only validates address syntax. Resolve the visitor address at your trusted proxy boundary and apply your private/reserved-address policy before calling it. Do not trust an arbitrary X-Forwarded-For value supplied by a client. Normalize equivalent IPv6 representations before using an address as a cache key.
The explicit address matters: when your backend calls a “detect my IP” endpoint, the provider can see your backend's connection. Sending the address you actually intend to enrich avoids confusing your server's network with your visitor's network.
Query-string keys also need attention in access logs, tracing and exception reports. Redact api_key and avoid recording the full outbound URL. The example returns a sanitized unavailable result instead of returning an exception that may include it.
Reuse work and set a budget
Cache successful results for a deliberately chosen period, and bound the cache size. There is a tradeoff: a longer cache lifetime reduces calls but can retain stale network context. An ASN lookup and a time-sensitive security signal may need different policies.
Also coalesce concurrent requests for the same address. Ten simultaneous requests can all miss an ordinary cache before the first result arrives. An in-flight promise map, removed in a finally block, lets them share one provider request. For multiple workers, both cache and admission decisions need shared coordination if a global limit matters.
Caching is not a monthly quota guarantee. Track actual outbound lookups and use a shared budget before admitting new ones. Leave headroom for testing and operations. The public contract documents /api/v1/ratelimit for quota and renewal information; read the actual renewal date rather than assuming a calendar-month reset. Do not add a quota request to every visitor request.
Treat unavailable as its own state
A timeout, a 429, and an invalid key all mean that enrichment is unavailable. They do not mean that an address is safe. Decide separately whether the application can continue without the signal, needs a later review, or must stop a particular operation. A background analytics job and an account-recovery flow can reasonably make different decisions.
For a 429, avoid an immediate retry loop. Respect any documented retry guidance and wait for budget to become available. For authentication errors, alert the operator without logging the key. If you retry transient failures, cap attempts and count them in the budget; an aborted HTTP request may already have reached the provider.
Finally, an address shared by a VPN, company gateway or mobile network can represent many people. Network enrichment is context. Keep it separate from a conclusion that a person is fraudulent, and avoid using one flag as an automatic rejection rule.
Public references: IP-API.io API documentation, machine-readable OpenAPI contract, and Free plan and current allowances.
Validation: the JavaScript example passed six checks with a simulated fetch transport: invalid IP, missing key, IPv6 URL/auth construction, 429, network failure and invalid JSON. No network calls or real API key were used for those checks.
Top comments (0)