Swift SDK · Feature

IP Geolocation in Swift

Turn any IP address into geolocation, network, and threat intelligence with one call. The lookup method on the official ipapi-swift client returns the country, city, coordinates, timezone, ISP, and ASN of an IP — plus the suspiciousFactors flags used for fraud screening — in a single typed response.

Trusted by thousands of businesses
Fast JSON API responses
Real-time validation
Simple integration, SDKs & examples
What it does

Geolocation without device GPS

IP-based geolocation works entirely server-side — no permission prompt, no Core Location, and it works for every visitor and every log line you already have. ip-api.io resolves an IP to a physical location and, uniquely, returns security signals (VPN, proxy, Tor, datacenter, threat) in the same response, so you can localize content and screen for fraud in one round trip.

Prerequisites

  • A Swift 5.9+ project with an async context
  • The ipapi-swift package added via SPM
  • A free ip-api.io API key
1

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)
Behind a proxy? Pass the real client IP from the X-Forwarded-For header to lookup(ip:) rather than relying on the socket address. See the SDK overview for a Vapor handler example.
2

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)
}
Reference

Response reference

lookup(ip:) resolves to a typed IpInfo. Nullable fields are Swift optionals — nil for private or unrecognized addresses.

PropertyTypeDescription
ipStringThe looked-up address
ispString?Internet service provider
asnString?Autonomous system the IP belongs to
locationLocation?country, countryCode, city, latitude, longitude, zip, timezone, localTime, localTimeUnix, isDaylightSavings (nullable)
suspiciousFactorsSuspiciousFactorsisProxy, isVpn, isTorNode, isDatacenter, isSpam, isCrawler, isThreat
FAQ

Frequently asked questions

How do I get the location of an IP address in Swift?

Add ipapi-swift, construct an IpApiClient with your API key, and try await client.lookup(ip: ip). The returned IpInfo has a location struct with country, city, latitude, longitude and timezone, plus a suspiciousFactors struct with VPN/proxy/Tor flags.

How do I geolocate the caller's own IP?

Call try await client.lookup() with no IP argument and the API resolves the IP from the request. Behind a proxy, pass the real client IP from the X-Forwarded-For header to client.lookup(ip:) instead.

How do I look up many IP addresses at once?

Use try await client.lookupBatch(ips:) with up to 100 addresses in one request. It returns results (a [String: IpInfo]) plus totalProcessed, successfulLookups and failedLookups.

Why are some fields optional?

Nullable location fields such as country and city are optionals because they're absent for private ranges or unrecognized addresses — unwrap with if let or ??. Boolean flags like isVpn are plain Bool.

Start building with the ip-api.io Swift SDK

Add ipapi-swift via Swift Package Manager, drop in your free API key, and ship in minutes — one async client for geolocation, fraud detection, and email validation.

Need support?

Explore how IP-API.io can enhance your security, provide robust bot protection, and improve IP geolocation accuracy for your applications.

Contact Support

Found a bug in the SDK?

The ipapi-swift package is open source. Open an issue or pull request on GitHub and we'll take a look.

Open on GitHub