Prerequisites
- Go 1.21+ with
ipapi-goinstalled - 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 pointers.
client := ipapi.NewClient(ipapi.WithAPIKey(os.Getenv("IP_API_IO_KEY")))
ctx := context.Background()
asn, _ := client.ASN(ctx, "8.8.8.8")
if asn.ASN != nil {
fmt.Println(*asn.ASN) // 15169
}
if asn.Organization != nil {
fmt.Println(*asn.Organization) // "Google LLC"
}
fmt.Println(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.
whois, _ := client.Whois(ctx, "example.com")
if whois.Registrar != nil && whois.Registrar.Name != nil {
fmt.Println(*whois.Registrar.Name)
}
if whois.RegisteredOn != nil {
fmt.Println(*whois.RegisteredOn) // "1995-08-14"
}
fmt.Println(whois.NameServers) DNS with ReverseDNS, ForwardDNS & MXRecords
Resolve a PTR record, resolve a hostname to addresses, or list a domain's mail servers.
rdns, _ := client.ReverseDNS(ctx, "8.8.8.8")
if rdns.Hostname != nil {
fmt.Println(*rdns.Hostname) // "dns.google"
}
fdns, _ := client.ForwardDNS(ctx, "dns.google")
for _, record := range fdns.Addresses {
fmt.Println(record.Type, record.Address, record.TTL) // "A" "8.8.8.8" 300
}
mx, _ := client.MXRecords(ctx, "example.com")
for _, record := range mx.MXRecords {
fmt.Println(record.Priority, record.Hostname, record.TTL)
}