Prerequisites
- Ruby 3.0 or higher
- The
ip-api-iogem installed (gem install ip-api-io) - 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. Absent fields return nil — guard with &..
client = Ipapi::Client.new(api_key: ENV.fetch("IP_API_IO_KEY"))
info = client.lookup_ip("8.8.8.8")
puts info.ip # "8.8.8.8"
puts info.isp # "Google LLC"
puts info.location&.country # "United States"
puts info.suspicious_factors.datacenter? # true
Resolve the IP of the machine making the request by calling lookup:
me = client.lookup
puts me.ip request.remote_ip to
lookup_ip(ip) rather than relying on the socket address. See the
SDK overview for a controller example.
Geolocate up to 100 IPs with lookup_batch
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK
raises ArgumentError if the array is empty or longer than 100.
batch = client.lookup_batch(["8.8.8.8", "1.1.1.1", "9.9.9.9"])
puts batch.total_processed # 3
puts batch.successful_lookups # 3
puts batch.failed_lookups # 0
batch.results.each do |ip, info|
puts "#{ip} #{info.suspicious_factors.vpn?}"
end