Geolocate an IP address using Web Services

Geolocating an IP address using GeoIP or GeoLite 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 GeoIP API Documentation page for details on our REST API.

1. Install the GeoIP client library

We have a collection of officially supported libraries for you to interact with the GeoIP and GeoLite 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 GeoIP 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 GeoLite API, but this requires additional configuration as demonstrated below:

1int accountId = 10;
2string licenseKey = "LICENSEKEY";
3
4var client = new WebServiceClient(accountId, licenseKey);
5
6// To query the GeoLite web service, you must set the optional `host` parameter
7// to `geolite.info`
8var client = new WebServiceClient(accountId, licenseKey, host: "geolite.info");
1int accountId = 10;
2String licenseKey = "LICENSEKEY";
3
4WebServiceClient client = new WebServiceClient.Builder(accountId, licenseKey).build();
5
6// To query the GeoLite web service, you must call the `host` method on the
7// builder with "geolite.info"
8WebServiceClient client = new WebServiceClient.Builder(accountId, licenseKey)
9    .host("geolite.info").build();
 1const WebServiceClient = require('@maxmind/geoip2-node').WebServiceClient;
 2// TypeScript:
 3// import { WebServiceClient } from '@maxmind/geoip2-node';
 4
 5const accountId = '10';
 6const licenseKey = 'LICENSEKEY';
 7
 8const client = new WebServiceClient(accountId, licenseKey);
 9
10// To query the GeoLite web service, you must set the optional `host` parameter
11const client = new WebServiceClient(accountId, licenseKey, {
12  host: 'geolite.info',
13});
 1<?php require_once 'vendor/autoload.php'
 2use GeoIp2\WebService\Client;
 3
 4$account_id = 10;
 5$license_key = 'LICENSEKEY';
 6
 7$client = new Client($account_id, $license_key);
 8
 9// To query the GeoLite web service, you must set the optional `host` argument.
10// The third argument specifies the language preferences when using the `->name`
11// method on the model classes that this client creates.
12$client = new Client($account_id, $license_key, ['en'], ['host' => 'geolite.info']);
 1import asyncio # for async requests with AsyncClient
 2import geoip2.webservice
 3
 4account_id = 10
 5license_key = 'LICENSEKEY'
 6
 7# If you want to use synchronous requests
 8client = Client(account_id, license_key)
 9
10# To query the GeoLite web service, you must set the "host" keyword argument
11# to "geolite.info"
12client = Client(account_id, license_key, host='geolite.info')
13
14# Or if you want to use asynchronous requests
15async_client = AsyncClient(account_id, license_key)
16
17# To query the GeoLite web service, you must set the "host" keyword argument
18# to "geolite.info"
19async_client = AsyncClient(account_id, license_key, host='geolite.info')
 1require 'maxmind/geoip2'
 2
 3client = MaxMind::GeoIP2::Client.new(
 4  account_id: 10,
 5  license_key: 'LICENSEKEY',
 6
 7  # To use the GeoLite web service instead of GeoIP, set the host
 8  # parameter to "geolite.info", eg:
 9  # host: 'geolite.info',
10)

3. Query the desired geolocation service

GeoIP offers 3 services: Insights, City Plus, and Country. GeoLite 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
 5int accountId = 10;
 6string licenseKey = "LICENSEKEY";
 7
 8// Sync
 9using (var client = new WebServiceClient(accountId, licenseKey))
10{
11    // You can also use `client.City` or `client.Insights`
12    // `client.Insights` is not available to GeoLite users
13    var response = client.Country("128.101.101.101");
14
15    Console.WriteLine(response.Country.IsoCode);        // 'US'
16    Console.WriteLine(response.Country.Name);           // 'United States'
17    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
18}
19
20// Async
21using (var client = new WebServiceClient(accountId, licenseKey))
22{
23    // You can also use `client.CityAsync` or `client.InsightsAsync`
24    // `client.InsightsAsync` is not available to GeoLite users
25    var response = await client.CountryAsync("128.101.101.101");
26
27    Console.WriteLine(response.Country.IsoCode);        // 'US'
28    Console.WriteLine(response.Country.Name);           // 'United States'
29    Console.WriteLine(response.Country.Names["zh-CN"]); // '美国'
30}
 1int accountId = 10;
 2String licenseKey = "LICENSEKEY";
 3
 4try (WebServiceClient client = new WebServiceClient.Builder(accountId, licenseKey)
 5        .build()) {
 6
 7    InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
 8
 9    // You can also use `client.city` or `client.insights`
10    // `client.insights` is not available to GeoLite users
11    CountryResponse response = client.country(ipAddress);
12
13    Country country = response.getCountry();
14    System.out.println(country.getIsoCode());            // 'US'
15    System.out.println(country.getName());               // 'United States'
16    System.out.println(country.getNames().get("zh-CN")); // '美国'
17}
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 GeoLite 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$account_id = 10;
 5$license_key = 'LICENSEKEY';
 6
 7$client = new Client($account_id, $license_key);
 8
 9// You can also use `$client->city` or `$client->insights`
10// `$client->insights` is not available to GeoLite users
11$record = $client->country('128.101.101.101');
12
13print($record->country->isoCode . "\n");
 1# Sync
 2import geoip2.webservice
 3
 4account_id = 10
 5license_key = 'LICENSEKEY'
 6
 7with geoip2.webservice.Client(account_id, license_key) as client:
 8  # You can also use `client.city` or `client.insights`
 9  # `client.insights` is not available to GeoLite users
10  response = client.country('128.101.101.101)
11
12  print(response.country.iso_code)
13
14# Async
15import asyncio
16import geoip2.webservice
17
18async def main():
19  async with geoip2.webservice.AsyncClient(account_id, license_key) as client:
20    # You can also use `client.city` or `client.insights`
21    # `client.insights` is not available to GeoLite users
22    response = await client.country('128.101.101.101)
23
24    print(response.country.iso_code)
25
26asyncio.run(main())
 1require 'maxmind/geoip2'
 2
 3client = MaxMind::GeoIP2::Client.new(
 4  account_id: 10,
 5  license_key: 'LICENSEKEY',
 6)
 7
 8# You can also use `client.city` or `client.insights`.
 9# Note that `client.insights` is not available to GeoLite 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"

GeoLite 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"

GeoLite 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"