• Stars
    star
    109
  • Rank 312,879 (Top 7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go library for IPinfo API (IP geolocation and other types of IP data)

IPinfo IPinfo Go Client Library

License Go Reference

This is the official Go client library for the IPinfo.io IP address API, allowing you to look up your own IP address, or get any of the following details for other IP addresses:

  • IP to Geolocation (city, region, country, postal code, latitude, and longitude)
  • IP to ASN (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
  • IP to Company (the name and domain of the business that uses the IP address)
  • IP to Carrier (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)

Check all the data we have for your IP address here.

Getting Started

You'll need an IPinfo API access token, which you can get by signing up for a free account at https://ipinfo.io/signup.

The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see https://ipinfo.io/pricing

You can find the full package-level documentation here: https://pkg.go.dev/github.com/ipinfo/go/v2/ipinfo

Installation

go get github.com/ipinfo/go/v2/ipinfo

Quickstart

Basic usage of the package.

package main

import (
	"fmt"
	"log"
	"net"
	"github.com/ipinfo/go/v2/ipinfo"
)

func main() {
	const token = "YOUR_TOKEN"
	
	// params: httpClient, cache, token. `http.DefaultClient` and no cache will be used in case of `nil`.
	client := ipinfo.NewClient(nil, nil, token)

	const ip_address = "8.8.8.8"
	info, err := client.GetIPInfo(net.ParseIP(ip_address))

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(info)
	// Output: {8.8.8.8 dns.google false true Mountain View California US United States...
}

This data is available even on our free tier which includes up to 50,000 IP geolocation requests per month.

Authentication

The IPinfo Go library can be authenticated with your IPinfo API access token, which is passed as the third positional argument of the ipinfo.NewClient() method. Your IPInfo access token can be found in the account section of IPinfo's website after you have signed in: https://ipinfo.io/account/token

const token = "YOUR_TOKEN"
// params: httpClient, cache, token. `http.DefaultClient` and no cache will be used in case of `nil`.
client := ipinfo.NewClient(nil, nil, token)

Internationalization

Country Name

info.Country returns the ISO 3166 country code and info.CountryName returns the entire conuntry name:

fmt.Println(info.Country)
// Output: US
fmt.Println(info.CountryName)
// Output: United States

European Union (EU) Country

info.IsEU returns a boolean response to see if a country is a European Union country or not.

fmt.Println(info.IsEU)
// Output: false

Country Flag

Get country flag as an emoji and its Unicode value with info.CountryFlag.Emoji and info.CountryFlag.Unicode respectively.

fmt.Println(info.CountryFlag.Emoji)
// Output: πŸ‡³πŸ‡Ώ 
fmt.Println(info.CountryFlag.Unicode)
// Output: "U+1F1F3 U+1F1FF"

Country Flag URL

Get the link of a country's flag image.

fmt.Println(info.CountryFlagURL)
// Output: https://cdn.ipinfo.io/static/images/countries-flags/US.svg"

Country Currency

Get country's currency code and its symbol with info.CountryCurrency.Code and info.CountryCurrency.Symbol respectively.

fmt.Println(info.CountryCurrency.Code)
// Output: USD 
fmt.Println(info.CountryCurrency.Symbol)
// Output: $

Continent

Get the IP's continent code and its name with info.Continent.Code and info.Continent.Name respectively.

fmt.Println(info.Continent.Code)
// Output: NA 
fmt.Println(info.Continent.Name)
// Output: North America

Map IP Address

You can map up to 500,000 IP addresses all at once using the GetIPMap command. You can input:

  • IP addresses (IPV4 and IPV6 both)
  • IP Ranges or Netblock
  • ASN

After the operation, you will be presented with a URL to a map generated on the IPinfo website.

IP Map Code:

package main

import (
	"fmt"
	"log"
	"net"

	"github.com/ipinfo/go/v2/ipinfo"
)

func main() {
	client := ipinfo.NewClient(nil, nil, "YOUR_TOKEN")
	result, err := client.GetIPMap(
		[]net.IP{
			net.ParseIP("136.111.157.61"),
			net.ParseIP("231.163.78.134"),
			// ...
			net.ParseIP("228.128.213.179"),
			net.ParseIP("103.172.175.76"),
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)
}

Result:

See the output example map: https://ipinfo.io/tools/map/f27c7d40-3ff0-4ac2-878f-8d953dbcd3c8

Summarize IP Address

Summarize IP addresses with GetIPSummary and output a report.

package main

import (
	"fmt"
	"log"
	"net"

	"github.com/ipinfo/go/v2/ipinfo"
)

func main() {
	client := ipinfo.NewClient(nil, nil, "YOUR_TOKEN")
	result, err := client.GetIPSummary(
		[]net.IP{
			net.ParseIP("171.164.236.38"),
			net.ParseIP("206.132.224.214"),
			// ....
			net.ParseIP("208.191.89.104"),
			net.ParseIP("81.216.14.76"),
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)
	// Ouptut: {100 100 map[CN:7 DE:4 JP:12 MX:3 US:32] map[Columbus, ...

}

Batch Operations / Bulk Lookup

You can do batch lookups or bulk lookups quite easily as well. The inputs supported:

  • IP addresses. IPV4 and IPV6 both
  • ASN
  • Specific field endpoint of an IP address e.g. 8.8.8.8/country
package main

import (
	"fmt"
	"log"
	"time"
	"github.com/ipinfo/go/v2/ipinfo"
	"github.com/ipinfo/go/v2/ipinfo/cache"
)

func main() {
	client := ipinfo.NewClient(
		nil,
		ipinfo.NewCache(cache.NewInMemory().WithExpiration(5*time.Minute)),
		"YOUR_TOKEN",
	)

	// batchResult will contain all the batch lookup data
	batchResult, err := client.GetBatch(
		[]string{
			"104.193.114.182",                    // you can pass IPV4 address
			"8.8.8.8/country",                    // you can get specific information
			"AS36811",                            // you can lookup ASN details
			"2a03:2880:f10a:83:face:b00c:0:25de", // IPV6 address
		},
		ipinfo.BatchReqOpts{
			BatchSize:       2,
			TimeoutPerBatch: 0,
			TimeoutTotal:    5,
		},
	)
	if err != nil {
		log.Fatal(err)
	}
	for k, v := range batchResult {
		fmt.Printf("k=%v v=%v\n", k, v)
	}
}

Examples of Batch / Bulk Lookup:

The loop declaration in the batch lookup showcases the "caching" capability of the IPinfo package.

Other Libraries

There are official IPinfo client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.

About IPinfo

Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for 100,000 businesses and developers.

image

More Repositories

1

cli

Official Command Line Interface for the IPinfo API (IP geolocation and other types of IP data)
Go
1,655
star
2

python

Official Python Library for IPinfo API (IP geolocation and other types of IP data)
Python
444
star
3

php

Official PHP library for IPinfo (IP geolocation and other types of IP data)
PHP
231
star
4

laravel

Official Laravel client library for IPinfo API (IP geolocation and other types of IP data)
PHP
153
star
5

node

Official Node client library for IPinfo API (IP geolocation and other types of IP data)
TypeScript
149
star
6

java

Official Java library for IPinfo API (IP geolocation and other types of IP data)
Java
98
star
7

mmdbctl

mmdbctl is an MMDB file management CLI supporting various operations on MMDB database files.
Go
74
star
8

django

Official Django Library for IPinfo API (IP geolocation and other types of IP data)
Python
55
star
9

rust

IPinfo Rust library for IPinfo API (IP geolocation and other types of IP data)
Rust
51
star
10

ruby

Official Ruby client library for IPinfo API (IP geolocation and other types of IP data)
Ruby
48
star
11

csharp

Official C# Library for IPinfo API (IP geolocation and other types of IP data)
C#
35
star
12

sample-database

IPinfo database and API sample repository
26
star
13

perl

Official Perl client library for IPinfo API (IP geolocation and other types of IP data)
Perl
26
star
14

csharp-old

Old C# Library for IPinfo API (IP geolocation and other types of IP data)
C#
24
star
15

rails

Official Rails Library for IPinfo API (IP geolocation and other types of IP data)
Ruby
19
star
16

spring

Official Spring Client Library for IPinfo API (IP geolocation and other types of IP data)
Java
18
star
17

libraries

Documentation for IPinfo official client libraries and integrations
15
star
18

python-db

Python
15
star
19

swift

Swift
10
star
20

node-express

Official Node ExpressJS client library for IPinfo API (IP geolocation and other types of IP data)
TypeScript
8
star
21

erlang

Official Erlang library for IPinfo API
Erlang
7
star
22

docker

Shell
4
star
23

splunk-docs

2
star