geoip2ws

Maxmind GeoIP2 Web Services (unofficial)

geoip2ws

Unofficial Node.js module for the Maxmind GeoIP2 Web Services.

There are multiple ways to load and set up this module. All communication is done over HTTPS.

require ('geoip2ws') (userId, licenseKey) (ip, callback);
var geo = require ('geoip2ws') (userId, licenseKey);
geo (ip1, callback);
geo (ip2, callback);
var insights = new require ('geoip2ws') (userId, licenseKey, 'insights');
var country = new require ('geoip2ws') (userId, licenseKey, 'country');
 
// precise lookup, higher cost 
insights (ip, callback);
 
// vague lookup, lower cost 
country (ip, callback);

You need a Maxmind account ID and license key with enough credits for one of their GeoIP web services. You can find both here.

npm install geoip2ws

The first function is the setup and takes these settings:

parametertyperequireddescription
userIdstringyesYour Maxmind account user ID
licenseKeystringyesYour Maxmind account license key
servicestringnoThe service you'd like to use: insights, country, city (default)
requestTimeoutintegernoSocket read timeout in milliseconds to wait for reply from MaxMind
// With arguments 
var geo = require ('geoip2ws') (1234, 'abc', 'country', 2000);
 
// Or with an object 
var geo = require ('geoip2ws') ({
  userId: 1234,
  licenseKey: 'abc',
  service: 'country',
  requestTimeout: 2000
});

The second function does the IP-address lookup and takes these arguments:

parametertyperequireddescription
servicestringnoThe service, same as above
ipstringyesThe IPv4 or IPv6 address to lookup
callbackfunctionyesYour callback function to receive the data
geo ('city', '145.53.252.135', myCallback);

The callback function receives result data and errors. Unless an error occurs the data JSON will be parsed to an object. When everything is ok err is null else err is an instance of Error. It also returns API errors this same way.

function myCallback (errdata) {
  if (err) {
    console.log (err);
  } else {
    console.log (data.city.names.en);
  }
}

Response data: http://dev.maxmind.com/geoip/geoip2/web-services/#Response_Body

error messagedescriptionadditional
no userId or licenseKeyYou did not set your credentials
invalid serviceThe service name is invalidno credits used
invalid ipThe IP-address is invalidno credits used
request failedA request error occurederr.error
API errorAPI error occurederr.code and err.error

API errors: http://dev.maxmind.com/geoip/geoip2/web-services/#Errors

The source code repository has two main branches: master and develop. The master is exactly the same as the latest release on npm, while develop is where all development happens and other branches and Pull Requests are merged into.

When you intent to submit a Pull Request, please code against the develop branch.

You can test your code with npm test in the source path or simply create a PR and see what happens.

  • With account the API endpoint is at https://geoip.maxmind.com: credits count, real data.
  • Without account the API endpoint is at https://frankl.in: no credits, no logs, fake data.
variabledescriptiondefault
GEOIP2WS_USERIDAccount user IDnull
GEOIP2WS_LICENCEAccount license keynull
GEOIP2WS_SERVICEcountry, city, insightscity
GEOIP2WS_TIMEOUTWait time out in ms5000
GEOIP2WS_ENDPOINTAPI endpoint, i.e. http proxyhttps://geoip.maxmind.com/geoip/v2.1/

Example: GEOIP2WS_TIMEOUT=30000 npm test

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

Franklin van de Meent | Website | Github