Prerequisites
- A Swift 5.9+ project with an async context
- The
ipapi-swiftpackage added via SPM - A free ip-api.io API key
Geolocate one IP with lookup(ip:)
Pass any IPv4 or IPv6 address, or use lookup() to geolocate the caller's
own IP. Nullable fields are optionals — unwrap before use.
let client = IpApiClient(apiKey: ProcessInfo.processInfo.environment["IP_API_IO_KEY"]!)
let info = try await client.lookup(ip: "8.8.8.8")
print(info.ip) // "8.8.8.8"
print(info.isp ?? "") // "Google LLC"
print(info.location?.country ?? "") // "United States"
print(info.suspiciousFactors.isDatacenter) // true
Resolve the IP of the machine making the request by calling lookup():
let me = try await client.lookup()
print(me.ip) X-Forwarded-For header to lookup(ip:) rather than relying
on the socket address. See the
SDK overview for a Vapor handler example.
Geolocate up to 100 IPs with lookupBatch(ips:)
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK throws if the array is empty or longer than 100.
let batch = try await client.lookupBatch(ips: ["8.8.8.8", "1.1.1.1", "9.9.9.9"])
print(batch.totalProcessed) // 3
print(batch.successfulLookups) // 3
print(batch.failedLookups) // 0
for (ip, info) in batch.results {
print(ip, info.suspiciousFactors.isVpn)
}