Geolocate an IP address using Databases
Geolocating an IP address using GeoIP and GeoLite databases consists of configuring a database reader and querying the database.
MaxMind offers and highly recommends using official client libraries to query our databases.
We have a collection of officially supported libraries for you to query with the GeoIP and GeoLite databases:
1// Install via NuGet2Install-PackageMaxMind.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{10mavenCentral()11}12dependencies{13compile 'com.maxmind.geoip2:geoip2:2.15.0' 14}
1// Install via npm 2npminstall @maxmind/geoip2-node34// Or install via yarn 5yarnadd @maxmind/geoip2-node
1# Install via Composer 2composerrequiregeoip2/geoip2:~2.0
1# Install via pip2pipinstallgeoip2
1# Install as a gem2geminstallmaxmind-geoip234# Or add this to your Gemfile5gem'maxmind-geoip2'
Configuring the database reader requires the database file to be accessible on the filesystem. After configuring the database reader, you can then query the database by calling the method corresponding to the database type (e.g. city
or country
) and passing it the IP address you want to look up.
If the lookup succeeds, the method call will return a model class/object for the database method you called. This model in turn contains multiple record classes/objects, each of which represents part of the data for the record.
If the request fails, the reader class will throw an exception or return an error depending on the library.
For more details on database methods, errors, and exceptions, see the client API documentation below.
1using(varreader=newDatabaseReader("path/to/maxmind-database.mmdb"))2{3varresponse=reader.City("128.101.101.101");45Console.WriteLine(response.Country.IsoCode);6}
1Filedatabase=newFile("/path/to/maxmind-database.mmdb") 2 3// This reader object should be reused across lookups as creation of it is 4// expensive. 5DatabaseReaderreader=newDatabaseReader.Builder(database).build(); 6 7// If you want to use caching at the cost of a small (~2MB) memory overhead: 8// new DatabaseReader.Builder(file).withCache(new CHMCache()).build(); 910InetAddressipAddress=InetAddress.getByName("128.101.101.101");1112CityResponseresponse=reader.city(ipAddress);1314Countrycountry=response.getCountry();15System.out.println(country.getIsoCode());
1// Asynchronous database opening 2constReader=require('@maxmind/geoip2-node').Reader; 3 4Reader.open('/path/to/maxmind-database.mmdb').then(reader=>{ 5constresponse=reader.city('128.101.101.101'); 6 7console.log(response.country.isoCode); 8}); 91011// Synchronous database opening 12constfs=require('fs');13constReader=require('@maxmind/geoip2-node').Reader;1415constdbBuffer=fs.readFileSync('/path/to/maxmind-database.mmdb');1617// This reader object should be reused across lookups as creation of it is 18// expensive. 19constreader=Reader.openBuffer(dbBuffer);2021response=reader.city('128.101.101.101');2223console.log(response.country.isoCode);
1<?php 2require_once'vendor/autoload.php'; 3useGeoIp2\Database\Reader; 4 5// This reader object should be reused across lookups as creation of it is 6// expensive. 7$reader=newReader('/path/to/maxmind-database.mmdb'); 8 9$record=$reader->city('128.101.101.101');1011print($record->country->isoCode);
1importgeoip2.database23# This reader object should be reused across lookups as creation of it is4# expensive.5withgeoip2.database.Reader('/path/to/maxmind-database.mmdb')asreader:6response=reader.city('128.101.101.101');7print(response.country.iso_code)
1require'maxmind/geoip2'23# This reader object should be reused across lookups as creation of it is4# expensive.5reader=MaxMind::GeoIP2::Reader.new('/path/to/maxmind-database.mmdb')67record=reader.city('128.101.101.101')89putsrecord.country.iso_code
You can find a complete list of official and unofficial client APIs, and third-party integrations on the
database documentation page.