- Notifications
You must be signed in to change notification settings - Fork23
Node.js API for GeoIP2 webservice client and database reader
License
maxmind/GeoIP2-node
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package provides a server-side API for theGeoIP2 databases and GeoLite2databases, and a server-sideAPI for theGeoIP2 web services and GeoLite2 webservices.
This package will not work client-side.
npm install @maxmind/geoip2-node
You can also useyarn orpnpm.
IP geolocation is inherently imprecise. Locations are often near the center ofthe population. Any location provided by a GeoIP2 database or web serviceshould not be used to identify a particular address or household.
To use the web service API, you must create a newWebServiceClient, usingyour MaxMindaccountID andlicenseKey as parameters. The third argument isan object holding additional option. Thetimeout option defaults to3000.Thehost option defaults togeoip.maxmind.com. Sethost togeolite.infoto use the GeoLite2 web service instead of GeoIP2. Sethost tosandbox.maxmind.com to use the Sandbox environment.
You may then call the function corresponding to a specific end point, passing itthe IP address you want to lookup.
If the request succeeds, the function's Promise will resolve with the modelfor the end point you called. This model in turn contains multiplerecords, each of which represents part of the data returned by the web service.
If the request fails, the function's Promise will reject with an error object.
See theAPI documentation formore details.
constWebServiceClient=require('@maxmind/geoip2-node').WebServiceClient;// Typescript:// import { WebServiceClient } from '@maxmind/geoip2-node';// To use the GeoLite2 web service instead of the GeoIP2 web service, set// the host to geolite.info, e.g.:// new WebServiceClient('1234', 'licenseKey', {host: 'geolite.info'});//// To use the Sandbox GeoIP2 web service instead of the production GeoIP2// web service, set the host to sandbox.maxmind.com, e.g.:// new WebServiceClient('1234', 'licenseKey', {host: 'sandbox.maxmind.com'});constclient=newWebServiceClient('1234','licenseKey');client.country('142.1.1.1').then(response=>{console.log(response.country.isoCode);// 'CA'});
constWebServiceClient=require('@maxmind/geoip2-node').WebServiceClient;// Typescript:// import { WebServiceClient } from '@maxmind/geoip2-node';// To use the GeoLite2 web service instead of the GeoIP2 web service, set// the host to geolite.info, e.g.:// new WebServiceClient('1234', 'licenseKey', {host: 'geolite.info'});constclient=newWebServiceClient('1234','licenseKey');client.city('142.1.1.1').then(response=>{console.log(response.country.isoCode);// 'CA'console.log(response.postal.code);// 'M5S'});
constWebServiceClient=require('@maxmind/geoip2-node').WebServiceClient;// Typescript:// import { WebServiceClient } from '@maxmind/geoip2-node';// Note that the Insights web service is only supported by the GeoIP2// web service, not the GeoLite2 web service.//// To use the Sandbox GeoIP2 web service instead of the production GeoIP2// web service, set the host to sandbox.maxmind.com, e.g.:// new WebServiceClient('1234', 'licenseKey', {host: 'sandbox.maxmind.com'});constclient=newWebServiceClient('1234','licenseKey');client.insights('142.1.1.1').then(response=>{console.log(response.country.isoCode);// 'CA'console.log(response.postal.code);// 'M5S'console.log(response.traits.userType);// 'school'});
For details on the possible errors returned by the web service itself,seethe GeoIP2 web service documentation.
If the web service returns an explicit error document, the promise will be rejectedwith the following object structure:
{code:'THE_ERROR_CODE',error:'some human readable error',url:'https://geoip.maxmind.com...',}
In addition to the possible errors returned by the web service, the following errorcodes are provided:
SERVER_ERRORfor 5xx level errorsHTTP_STATUS_CODE_ERRORfor unexpected HTTP status codesINVALID_RESPONSE_BODYfor invalid JSON responses or unparseable response bodiesNETWORK_TIMEOUTfor network request timeoutsFETCH_ERRORfor internalfetcherrors
The database reader returns a promise that resolves with a reader instance.You may then call the function corresponding to the request type (e.g.city orcountry), passing it the IP address you want to look up.
If the request succeeds, the function call will return an object for the GeoIP2lookup. The object in turn contains multiple record objects, each of whichrepresents part of the data returned by the database.
We use thenode-maxmind library as thedatabase reader. As such, you have access to the sameoptions found in that libraryand can be used like this:
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';constoptions={// you can use options like `cache` or `watchForUpdates`};Reader.open('/usr/local/database.mmdb',options).then(reader=>{console.log(reader.country('1.1.1.1'));});
If you prefer to use aBuffer instead of using aPromise to open thedatabase, you can useReader.openBuffer(). Use cases include:
- You want to open the database in a synchronous manner.
- You want to fetch the database from an external source.
constfs=require('fs');constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';constdbBuffer=fs.readFileSync('/usr/local/city-database.mmdb');constreader=Reader.openBuffer(dbBuffer);console.log(reader.city('1.1.1.1'));
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-Anonymous-IP.mmdb').then(reader=>{constresponse=reader.anonymousIP('85.25.43.84');console.log(response.isAnonymous);// trueconsole.log(response.isAnonymousVpn);// falseconsole.log(response.isHostingProvider);// trueconsole.log(response.isPublicProxy);// falseconsole.log(response.isResidentialProxy);// falseconsole.log(response.isTorExitNode);// falseconsole.log(response.ipAddress);// '85.25.43.84'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP-Anonymous-Plus.mmdb').then(reader=>{constresponse=reader.anonymousPlus('85.25.43.84');console.log(response.anonymizerConfidence);// 30console.log(response.isAnonymous);// trueconsole.log(response.isAnonymousVpn);// falseconsole.log(response.isHostingProvider);// trueconsole.log(response.isPublicProxy);// falseconsole.log(response.isResidentialProxy);// falseconsole.log(response.isTorExitNode);// falseconsole.log(response.ipAddress);// '85.25.43.84'console.log(response.networkLastSeen);// '2025-04-14'console.log(response.providerName);// 'FooBar VPN'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoLite2-ASN.mmdb').then(reader=>{constresponse=reader.asn('128.101.101.101');console.log(response.autonomousSystemNumber);// 217console.log(response.autonomousSystemOrganization);// 'University of Minnesota'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-City.mmdb').then(reader=>{constresponse=reader.city('128.101.101.101');console.log(response.country.isoCode);// 'US'console.log(response.city.names.en);// 'Minneapolis'console.log(response.postal.code);// '55407'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-Connection-Type.mmdb').then(reader=>{constresponse=reader.connectionType('128.101.101.101');console.log(response.connectionType)// 'Cable/DSL'console.log(response.ipAddress)// '128.101.101.101'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-Country.mmdb').then(reader=>{constresponse=reader.country('128.101.101.101');console.log(response.country.isoCode);// 'US'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-Domain.mmdb').then(reader=>{constresponse=reader.domain('128.101.101.101');console.log(response.domain)// 'umn.edu'console.log(response.ipAddress)// '128.101.101.101'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-Enterprise.mmdb').then(reader=>{constresponse=reader.enterprise('128.101.101.101');console.log(response.country.isoCode)// 'US'});
constReader=require('@maxmind/geoip2-node').Reader;// Typescript:// import { Reader } from '@maxmind/geoip2-node';Reader.open('/usr/local/share/GeoIP/GeoIP2-ISP.mmdb').then(reader=>{constresponse=reader.isp('128.101.101.101');console.log(response.autonomousSystemNumber);// 217console.log(response.autonomousSystemOrganization);// 'University of Minnesota'console.log(response.isp);// 'University of Minnesota'console.log(response.organization);// 'University of Minnesota'console.log(response.ipAddress);// '128.101.101.101'});
If the database file does not exist, is not readable, is invalid, or there is a bugin the reader, the promise will be rejected with anError with a messageexplaining the issue.
If the database file and the reader method do not match (e.g.reader.city is used with a Country database), aBadMethodCalledError willbe thrown.
If the IP address is not found in the database, anAddressNotFoundError willbe thrown.
If the IP address is not valid, aValueError will be thrown.
If the database buffer is not a valid database, anInvalidDbBufferError willbe thrown.
We strongly discourage you from using a value from anynames property as akey in a database or object.
These names may change between releases. Instead we recommend using one of thefollowing:
- geoip2-node.CityRecord -
city.geonameId - geoip2-node.ContinentRecord -
continent.codeorcontinent.geonameId - geoip2-node.CountryRecord and geoip2.records.RepresentedCountry -
country.isoCodeorcountry.geonameId - geoip2-node.SubdivisionsRecord -
subdivision.isoCodeorsubdivision.geonameId
While many of the models contain the same basic records, the attributes whichcan be populated vary between web service end points or databases. In addition,while a model may offer a particular piece of data, MaxMind does not alwayshave every piece of data for any given IP address.
Because of these factors, it is possible for any request to return a recordwhere some or all of the attributes are unpopulated.
The only piece of data which is always returned is theipAddress attribute inthegeoip2-node.TraitsRecord record.
GeoNames offers web services and downloadabledatabases with data on geographical features around the world, includingpopulated places. They offer both free and paid premium data. Each featureis uniquely identified by ageonameId, which is an integer.
Many of the records returned by the GeoIP web services and databases include ageonameId field. 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 sourcethings like place names, ISO codes, and other similar data from the GeoNamespremium data set.
If the problem you find is that an IP address is incorrectly mapped, pleasesubmit your correction to MaxMind.
If you find some other sort of mistake, like an incorrect spelling,please checkthe GeoNames site first. Onceyou've searched for a place and found it on the GeoNames map view, thereare a number of links you can use to correct data ("move", "edit","alternate names", etc.). Once the correction is part of the GeoNamesdata set, it will be automatically incorporated into future MaxMindreleases.
If you are a paying MaxMind customer and you're not sure where to submit acorrection, pleasecontact MaxMind support for help.
MaxMind has tested this API with Node.js versions 18, 20, and 22. We aim to supportactive and maintained LTS versions of Node.js.
Patches and pull requests are encouraged. Please include unit testswhenever possible, as we strive to maintain 100% code coverage.
The GeoIP2 Node.js API usesSemantic Versioning.
Please report all issues with this code using theGitHub issuetracker
If you are having an issue with a MaxMind service that is not specific to theclient API, please contactMaxMind support for assistance.
This software is Copyright (c) 2018-2025 by MaxMind, Inc.
This is free software, licensed under the Apache License, Version 2.0.
About
Node.js API for GeoIP2 webservice client and database reader
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.