Ruby SDK · Feature

IP Geolocation in Ruby

Turn any IP address into geolocation, network, and threat intelligence with one call. The lookup_ip method on the official ip-api-io client returns the country, city, coordinates, timezone, ISP, and ASN of an IP — plus the suspicious_factors predicates used for fraud screening — in a single response.

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

Geolocation without browser GPS

IP-based geolocation works entirely server-side — no permission prompt, no client SDK, 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

  • Ruby 3.0 or higher
  • The ip-api-io gem installed (gem install ip-api-io)
  • 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. 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
Behind a proxy? In Rails, pass request.remote_ip to lookup_ip(ip) rather than relying on the socket address. See the SDK overview for a controller example.
2

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
Reference

Response reference

lookup_ip returns a typed IPInfo. Nullable fields return nil for private or unrecognized addresses.

MethodTypeDescription
ipStringThe looked-up address
ispString, nilInternet service provider
asnString, nilAutonomous system the IP belongs to
locationLocationcountry, country_code, city, latitude, longitude, zip, timezone, local_time, local_time_unix, daylight_savings?
suspicious_factorsSuspiciousFactorsproxy?, vpn?, tor_node?, datacenter?, spam?, crawler?, threat?
FAQ

Frequently asked questions

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

Install ip-api-io, construct a client with your API key, and call client.lookup_ip(ip). The returned IPInfo has a location object with country, city, latitude, longitude and timezone, plus a suspicious_factors object with VPN/proxy/Tor predicates.

How do I geolocate the caller's own IP?

Call client.lookup with no argument and the API resolves the IP from the request. In Rails behind a proxy, pass request.remote_ip to client.lookup_ip(ip) so you geolocate the real client, not your load balancer.

How do I look up many IP addresses at once?

Use client.lookup_batch(ips) with up to 100 addresses in one request. It returns results (a hash of IP to IPInfo) plus total_processed, successful_lookups and failed_lookups, and raises ArgumentError if the array is empty or longer than 100.

Why do some fields return nil?

Nullable location fields such as country and city return nil for private ranges or unrecognized addresses — guard with the safe-navigation operator &.. Boolean signals like vpn? are predicate methods that always return true or false.

Start building with the ip-api.io Ruby SDK

Run gem install ip-api-io, drop in your free API key, and ship in minutes — one client object 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 ip-api-io gem is open source. Open an issue or pull request on GitHub and we'll take a look.

Open on GitHub