This is a beta release. The API may change before the first production release, which will be numbered 2.0.0.
You may find information on the GeoIP2 beta release process on our website.
This distribution provides an API for the GeoIP2 [web services] (http://dev.maxmind.com/geoip/geoip2/web-services) and [databases] (http://dev.maxmind.com/geoip/geoip2/downloadable). The API also works with the free GeoLite2 databases.
This library works with .NET Framework version 4.0 and above. If you are using Mono, 3.2 or greater is required.
This library depends on:
We recommend installing this library with NuGet. To do this, type the following into the Visual Studio Package Manager Console:
install-package MaxMind.GeoIP2
To use the web service API, you must create a new
MaxMind.GeoIP2.WebServiceClient
object with your userID
and licenseKey
.
You may also specify the fall-back locales, the host, timeout, or the request
timeout. You may then call the method corresponding to a specific end point,
passing it the IP address you want to look up. the IP address you want to look
up.
If the request succeeds, the method call will return a response class for the endpoint you called. This response in turn contains multiple model classes, each of which represents part of the data returned by the web service.
See the API documentation for more details.
// This creates a WebServiceClient object that can be reused across requests.
// Replace "42" with your user ID and "license_key" with your license
// key.
var client = new WebServiceClient(42, "license_key");
// Replace "Omni" with the method corresponding to the web service that
// you are using, e.g., "Country", "City", "CityIspOrg".
var omni = client.Omni("128.101.101.101");
Console.WriteLine(omni.Country.IsoCode); // 'US'
Console.WriteLine(omni.Country.Name); // 'United States'
Console.WriteLine(omni.Country.Names["zh-CN"]); // '美国'
Console.WriteLine(omni.MostSpecificSubdivision.Name); // 'Minnesota'
Console.WriteLine(omni.MostSpecificSubdivision.IsoCode); // 'MN'
Console.WriteLine(omni.City.Name); // 'Minneapolis'
Console.WriteLine(omni.Postal.Code); // '55455'
Console.WriteLine(omni.Location.Latitude); // 44.9733
Console.WriteLine(omni.Location.Longitude); // -93.2323
To use the database API, you must create a new DatabaseReader
with a string
representation of the path to your GeoIP2 database. You may also specify the
file access mode. You may then call the appropriate method (e.g., city
) for
your database, passing it the IP address you want to look up.
If the lookup succeeds, the method call will return a response class for the GeoIP lookup. This class in turn contains multiple model classes, each of which represents part of the data returned by the database.
We recommend reusing the DatabaseReader
object rather than creating a new
one for each lookup. The creation of this object is relatively expensive as it
must read in metadata for the file.
See the API documentation for more details.
// This creates the DatabaseReader object, which should be reused across
// lookups.
var reader = new DatabaseReader("GeoIP2-City.mmdb");
// Replace "City" with the appropriate method for your database, e.g.,
// "Country".
var city = reader.City("128.101.101.101");
Console.WriteLine(city.Country.IsoCode); // 'US'
Console.WriteLine(city.Country.Name); // 'United States'
Console.WriteLine(city.Country.Names["zh-CN"]); // '美国'
Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota'
Console.WriteLine(city.MostSpecificSubdivision.IsoCode); // 'MN'
Console.WriteLine(city.City.Name); // 'Minneapolis'
Console.WriteLine(city.Postal.Code); // '55455'
Console.WriteLine(city.Location.Latitude); // 44.9733
Console.WriteLine(city.Location.Longitude); // -93.2323
If the database is corrupt or otherwise invalid, a
MaxMind.Db.InvalidDatabaseException
will be thrown.
If an address is not available in the database, a
AddressNotFoundException
will be thrown.
For details on the possible errors returned by the web service itself, see the GeoIP2 web service documentation.
If the web service returns an explicit error document, this is thrown as a
AddressNotFoundException
, a AuthenticationException
, a
InvalidRequestException
, or a OutOfQueriesException
.
If some sort of transport error occurs, an HttpException
is thrown. This is
thrown when some sort of unanticipated error occurs, such as the web service
returning a 500 or an invalid error document. If the web service request
returns any status code besides 200, 4xx, or 5xx, this also becomes a
HttpException
.
Finally, if the web service returns a 200 but the body is invalid, the client
throws a GeoIP2Exception
. This exception also is the parent exception to the
above exceptions.
This API fully supports use in multi-threaded applications. When using the
DatabaseReader
in a multi-threaded application, we suggest creating one
object and sharing that among threads.
While many of the end points return the same basic records, the attributes which can be populated vary between end points. In addition, while an end point may offer a particular piece of data, MaxMind does not always have every piece of data for any given IP address.
Because of these factors, it is possible for any end point to return a record where some or all of the attributes are unpopulated.
See the GeoIP2 web service docs for details on what data each end point may return.
The only piece of data which is always returned is the ipAddress
attribute
in the GeoIp2\Record\Traits
record.
Every record class attribute has a corresponding predicate method so you can check to see if the attribute is set.
GeoNames offers web services and downloadable
databases with data on geographical features around the world, including
populated places. They offer both free and paid premium data. Each feature is
unique identified by a geonameId
, which is an integer.
Many of the records returned by the GeoIP2 web services and databases include
a geonameId
property. This is the ID of a geographical feature (city,
region, country, etc.) in the GeoNames database.
Some of the data that MaxMind provides is also sourced from GeoNames. We source things like place names, ISO codes, and other similar data from the GeoNames premium data set.
If the problem you find is that an IP address is incorrectly mapped, please submit your correction to MaxMind.
If you find some other sort of mistake, like an incorrect spelling, please check the GeoNames site first. Once you've searched for a place and found it on the GeoNames map view, there are a number of links you can use to correct data ("move", "edit", "alternate names", etc.). Once the correction is part of the GeoNames data set, it will be automatically incorporated into future MaxMind releases.
If you are a paying MaxMind customer and you're not sure where to submit a correction, please contact MaxMind support for help.
Please report all issues with this code using the GitHub issue tracker.
If you are having an issue with a MaxMind service that is not specific to the client API, please see our support page.
Patches and pull requests are encouraged. Please include unit tests whenever possible.
The API uses Semantic Versioning.
This software is Copyright (c) 2013 by MaxMind, Inc.
This is free software, licensed under the Apache License, Version 2.0.