Prerequisites
- Go 1.21 or higher
- The
ipapi-gomodule installed (go get github.com/ip-api-io/ipapi-go) - A free ip-api.io API key
Geolocate one IP with LookupIP
Pass any IPv4 or IPv6 address, or use Lookup(ctx) to geolocate the
caller's own IP. Nullable fields are pointers — guard before dereferencing.
client := ipapi.NewClient(ipapi.WithAPIKey(os.Getenv("IP_API_IO_KEY")))
ctx := context.Background()
info, err := client.LookupIP(ctx, "8.8.8.8")
if err != nil {
log.Fatal(err)
}
fmt.Println(info.IP) // "8.8.8.8"
if info.ISP != nil {
fmt.Println(*info.ISP) // "Google LLC"
}
if info.Location.Country != nil {
fmt.Println(*info.Location.Country) // "United States"
}
fmt.Println(info.SuspiciousFactors.IsDatacenter) // true
Resolve the IP of the machine making the request by calling Lookup(ctx):
me, _ := client.Lookup(ctx)
fmt.Println(me.IP) X-Forwarded-For header to LookupIP(ctx, ip) rather than
relying on the socket address. See the
SDK overview for an HTTP handler example.
Geolocate up to 100 IPs with LookupBatch
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK returns an error if the slice is empty or longer than 100.
batch, err := client.LookupBatch(ctx, []string{"8.8.8.8", "1.1.1.1", "9.9.9.9"})
if err != nil {
log.Fatal(err)
}
fmt.Println(batch.TotalProcessed) // 3
fmt.Println(batch.SuccessfulLookups) // 3
fmt.Println(batch.FailedLookups) // 0
for ip, info := range batch.Results {
fmt.Println(ip, info.SuspiciousFactors.IsVPN)
}