Prerequisites
- A Swift 5.9+ project with
ipapi-swiftadded - A free ip-api.io API key
Autonomous system with asn
Returns the ASN, owning organization, network range, and country for an IP — and whether it belongs to a datacenter. Nullable fields are optionals.
let client = IpApiClient(apiKey: ProcessInfo.processInfo.environment["IP_API_IO_KEY"]!)
let asn = try await client.asn(ip: "8.8.8.8")
print(asn.asn ?? 0) // 15169
print(asn.organization ?? "") // "Google LLC"
print(asn.isDatacenter) // true Domain registration with whois
WHOIS record for a domain: registrar, registration/expiry/update dates, name servers, status codes, and the raw WHOIS text.
let whois = try await client.whois(domain: "example.com")
print(whois.registrar?.name ?? "")
print(whois.registeredOn ?? "") // "1995-08-14"
print(whois.nameServers) DNS with reverseDns, forwardDns & mxRecords
Resolve a PTR record, resolve a hostname to addresses, or list a domain's mail servers.
let rdns = try await client.reverseDns(ip: "8.8.8.8")
print(rdns.hostname ?? "") // "dns.google"
let fdns = try await client.forwardDns(hostname: "dns.google")
for record in fdns.addresses {
print(record.type, record.address, record.ttl) // "A" "8.8.8.8" 300
}
let mx = try await client.mxRecords(domain: "example.com")
for record in mx.mxRecords {
print(record.priority, record.hostname, record.ttl)
}