Geolocate an IP address using Web Services

Geolocating an IP address using GeoIP2 or GeoLite2 web services consists of configuring a web service client, creating a request, and handling the response.

Implementation

MaxMind offers and highly recommends using official client libraries to access our geolocation services. If you cannot or do not wish to use our client libraries, please review our GeoIP2 API Documentation page for details on our REST API.

1. Install the GeoIP2 client library

We have a collection of officially supported libraries for you to interact with the GeoIP2 and GeoLite2 APIs:

1// Install via NuGet
2Install-Package MaxMind.GeoIP2
 1// Install via Maven, recommended
 2<dependency>
 3  <groupId>com.maxmind.geoip2</groupId>
 4  <artifactId>geoip2</artifactId>
 5  <version>2.15.0</version>
 6</dependency>
 7
 8// Or install via Gradle
 9repositories {
10  mavenCentral()
11}
12dependencies {
13  compile 'com.maxmind.geoip2:geoip2:2.15.0'
14}
1// Install via npm
2npm install @maxmind/geoip2-node
3
4// Or install via yarn
5yarn add @maxmind/geoip2-node
1# Install via Composer
2composer require geoip2/geoip2:~2.0
1# Install via pip
2pip install geoip2
1# Install as a gem
2gem install maxmind-geoip2
3
4# Or add this to your Gemfile
5gem 'maxmind-geoip2'

2. Create and configure a GeoIP2 client object

To interact with our API, you need to create a new client object. For this you will need your MaxMind account ID and license key. Our clients also allow you to interact with our GeoLite2 API, but this requires additional configuration as demonstrated below:

1var client = new WebServiceClient(10, "LICENSEKEY");
2
3// To query the GeoLite2 web service, you must set the optional `host` parameter
4to `geolite.info`
5var client = new WebServiceClient(10, "LICENSEKEY", host: "geolite.info");
1WebServiceClient client = new WebServiceClient.Builder(10, "LICENSEKEY").build();
2
3// To query the GeoLite2 web service, you must call the `host` method on the
4// builder with "geolite.info"
5WebServiceClient client = new WebServiceClient.Builder(10, "LICENSEKEY").host("geolite.info").build();
1const client = new WebServiceClient('10', 'LICENSEKEY');
2
3// To query the GeoLite2 web service, you must set the optional `host` parameter
4const client = new WebServiceClient('10', 'LICENSEKEY', {host: 'geolite.info'});
1$client = new Client(10, 'LICENSEKEY');
2
3// To query the GeoLite2 web service, you must set the optional `host` argument.
4// The third argument specifies the language preferences when using the `->name`
5// method on the model classes that this client creates.
6$client = new Client(10, 'LICENSEKEY', ['en'], ['host' => 'geolite.info']);
 1# If you want to use synchronous requests
 2client = Client(10, 'LICENSEKEY');
 3# To query the GeoLite2 web service, you must set the "host" keyword argument
 4# to "geolite.info"
 5client = Client(10, 'LICENSEKEY', host='geolite.info');
 6
 7# Or if you want to use asynchronous requests
 8async_client = AsyncClient(10, 'LICENSEKEY');
 9
10# To query the GeoLite2 web service, you must set the "host" keyword argument
11# to "geolite.info"
12async_client = AsyncClient(10, 'LICENSEKEY', host='geolite.info');
1Minfraud.configure do |c|
2  c.account_id = 10
3  c.license_key = 'LICENSEKEY'
4
5  # To use the GeoLite2 web service instead of GeoIP2, set the host
6  # parameter to "geolite.info", eg:
7  # host: 'geolite.info'
8end

3. Query the desired geolocation service

GeoIP2 offers 3 services: Insights, City Plus, and Country. GeoLite2 offers 2 services: City and Country. Each client library has an appropriately named method for accessing the desired geolocation service.

 1// If you are making multiple requests, a single WebServiceClient
 2// should be shared across requests to allow connection reuse. The
 3// class is thread safe.
 4
 5// Sync
 6using (var client = new WebServiceClient(10, "license_key"))
 7{
 8    // You can also use `client.City` or `client.Insights`
 9    // `client.Insights` is not available to GeoLite2 users
10    var response = client.Country("128.101.101.101");
11
12    Console.WriteLine(response.Country.IsoCode);        // 'US'
13    Console.WriteLine(response.Country.Name);           // 'United States'
14    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
15}
16
17// Async
18using (var client = new WebServiceClient(10, "license_key"))
19{
20    // You can also use `client.CityAsync` or `client.InsightsAsync`
21    // `client.InsightsAsync` is not available to GeoLite2 users
22    var response = await client.CountryAsync("128.101.101.101");
23
24    Console.WriteLine(response.Country.IsoCode);        // 'US'
25    Console.WriteLine(response.Country.Name);           // 'United States'
26    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
27}
 1try (WebServiceClient client = new WebServiceClient.Builder(42, "license_key")
 2        .build()) {
 3
 4    InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
 5
 6    // You can also use `client.city` or `client.insights`
 7    // `client.insights` is not available to GeoLite2 users
 8    CountryResponse response = client.country(ipAddress);
 9
10    Country country = response.getCountry();
11    System.out.println(country.getIsoCode());            // 'US'
12    System.out.println(country.getName());               // 'United States'
13    System.out.println(country.getNames().get("zh-CN")); // '美国'
14}
1const WebServiceClient = require('@maxmind/geoip2-node').WebServiceClient;
2// Typescript:
3// import { WebServiceClient } from '@maxmind/geoip2-node';
4
5// You can also use `client.city` or `client.insights`
6// `client.insights` is not available to GeoLite2 users
7client.country('142.1.1.1').then(response => {
8  console.log(response.country.isoCode); // 'CA'
9});
 1<?php require_once 'vendor/autoload.php'
 2use GeoIp2\WebService\Client;
 3
 4$client = new Client(10, 'LICENSEKEY');
 5
 6// You can also use `$client->city` or `$client->insights`
 7// `$client->insights` is not available to GeoLite2 users
 8$record = $client->country('128.101.101.101');
 9
10print($record->country->isoCode . "\n");
 1# Sync
 2import geoip2.webservice
 3
 4with geoip2.webservice.Client(10, 'license_key') as client:
 5  # You can also use `client.city` or `client.insights`
 6  # `client.insights` is not available to GeoLite2 users
 7  response = client.country('128.101.101.101)
 8
 9  print(response.country.iso_code)
10
11# Async
12import asyncio
13import geoip2.webservice
14
15async def main():
16  async with geoip2.webservice.AsyncClient(10, 'license_key') as client:
17    # You can also use `client.city` or `client.insights`
18    # `client.insights` is not available to GeoLite2 users
19    response = await client.country('128.101.101.101)
20
21    print(response.country.iso_code)
22
23asyncio.run(main())
 1require 'maxmind/geoip2'
 2
 3Minfraud.configure do |c|
 4  c.account_id = 10
 5  c.license_key = 'LICENSEKEY'
 6end
 7
 8# You can also use `client.city` or `client.insights`
 9# `client.insights` is not available to GeoLite2 users
10record = client.country('128.101.101.101')
11
12puts record.country.iso_code

Client APIs

You can find a complete list of official and third-party client APIs on the web services documentation page.

Command Line (curl) Examples

The web service may be accessed using curl, a simple command-line HTTP client. The -u flag is used to pass the HTTP basic authentication header that provides the web service with your credentials.

For the following examples, replace {account_id} and {license_key} (including the brackets) with your account ID and license key, and replace {ip_address} with the IP address you wish to look up.

GeoIP Country

1# Retrieve data for your IP address.
2curl -u "{account_id}:{license_key}" \
3  "https://geoip.maxmind.com/geoip/v2.1/country/me?pretty"
4
5# Retrieve data for an arbitrary IP address.
6curl -u "{account_id}:{license_key}" \
7  "https://geoip.maxmind.com/geoip/v2.1/country/{ip_address}?pretty"

GeoIP City Plus

1# Retrieve data for your IP address.
2curl -u "{account_id}:{license_key}" \
3  "https://geoip.maxmind.com/geoip/v2.1/city/me?pretty"
4
5# Retrieve data for an arbitrary IP address.
6curl -u "{account_id}:{license_key}" \
7  "https://geoip.maxmind.com/geoip/v2.1/city/{ip_address}?pretty"

GeoIP Insights

1# Retrieve data for your IP address.
2curl -u "{account_id}:{license_key}" \
3  "https://geoip.maxmind.com/geoip/v2.1/insights/me?pretty"
4
5# Retrieve data for an arbitrary IP address.
6curl -u "{account_id}:{license_key}" \
7  "https://geoip.maxmind.com/geoip/v2.1/insights/{ip_address}?pretty"

GeoLite2 Country

1# Retrieve data for your IP address.
2curl -u "{account_id}:{license_key}" \
3  "https://geolite.info/geoip/v2.1/country/me?pretty"
4
5# Retrieve data for an arbitrary IP address.
6curl -u "{account_id}:{license_key}" \
7  "https://geolite.info/geoip/v2.1/country/{ip_address}?pretty"

GeoLite2 City

1# Retrieve data for your IP address.
2curl -u "{account_id}:{license_key}" \
3  "https://geolite.info/geoip/v2.1/city/me?pretty"
4
5# Retrieve data for an arbitrary IP address.
6curl -u "{account_id}:{license_key}" \
7  "https://geolite.info/geoip/v2.1/city/{ip_address}?pretty"