Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@maxmind/geoip2-node

maxmind474.8kApache-2.06.0.0TypeScript support: included

Node.js API for GeoIP2 webservice client and database reader

geoip, geoip2, geoip precision, geoip2 precision, geolite, geolite2, maxmind, maxminddb, mmdb

readme

MaxMind GeoIP2 Node.js API

Description

This package provides a server-side API for the GeoIP2 databases and GeoLite2 databases, and a server-side API for the GeoIP2 web services and GeoLite2 web services.

This package will not work client-side.

Installation

npm install @maxmind/geoip2-node

You can also use yarn or pnpm.

IP Geolocation Usage

IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household.

Web Service Usage

To use the web service API, you must create a new WebServiceClient, using your MaxMind accountID and licenseKey as parameters. The third argument is an object holding additional option. The timeout option defaults to 3000. The host option defaults to geoip.maxmind.com. Set host to geolite.info to use the GeoLite2 web service instead of GeoIP2. Set host to sandbox.maxmind.com to use the Sandbox environment.

You may then call the function corresponding to a specific end point, passing it the IP address you want to lookup.

If the request succeeds, the function's Promise will resolve with the model for the end point you called. This model in turn contains multiple records, 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 the API documentation for more details.

Web Service Example

Country Service

const WebServiceClient = 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'});
const client = new WebServiceClient('1234', 'licenseKey');

client.country('142.1.1.1').then(response => {
  console.log(response.country.isoCode); // 'CA'
});

City Plus Service

const WebServiceClient = 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'});
const client = new WebServiceClient('1234', 'licenseKey');

client.city('142.1.1.1').then(response => {
  console.log(response.country.isoCode); // 'CA'
  console.log(response.postal.code); // 'M5S'
});

Insights Service

const WebServiceClient = 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'});
const client = new WebServiceClient('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'
});

Web Service Errors

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, the promise will be rejected with 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 error codes are provided:

  • SERVER_ERROR for 5xx level errors
  • HTTP_STATUS_CODE_ERROR for unexpected HTTP status codes
  • INVALID_RESPONSE_BODY for invalid JSON responses or unparseable response bodies
  • NETWORK_TIMEOUT for network request timeouts
  • FETCH_ERROR for internal fetch errors

Database Usage

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 or country), passing it the IP address you want to look up.

If the request succeeds, the function call will return an object for the GeoIP2 lookup. The object in turn contains multiple record objects, each of which represents part of the data returned by the database.

Options

We use the node-maxmind library as the database reader. As such, you have access to the same options found in that library and can be used like this:

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

const options = {
  // 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'));
});

Using a Buffer

If you prefer to use a Buffer instead of using a Promise to open the database, you can use Reader.openBuffer(). Use cases include:

  • You want to open the database in a synchronous manner.
  • You want to fetch the database from an external source.
const fs = require('fs');
const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

const dbBuffer = fs.readFileSync('/usr/local/city-database.mmdb');
const reader = Reader.openBuffer(dbBuffer);

console.log(reader.city('1.1.1.1'));

Database Examples

Anonymous IP Database Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-Anonymous-IP.mmdb').then(reader => {
  const response = reader.anonymousIP('85.25.43.84');

  console.log(response.isAnonymous); // true
  console.log(response.isAnonymousVpn); // false
  console.log(response.isHostingProvider); // true
  console.log(response.isPublicProxy); // false
  console.log(response.isResidentialProxy); // false
  console.log(response.isTorExitNode); // false
  console.log(response.ipAddress); // '85.25.43.84'
});

ASN Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoLite2-ASN.mmdb').then(reader => {
  const response = reader.asn('128.101.101.101');

  console.log(response.autonomousSystemNumber); // 217
  console.log(response.autonomousSystemOrganization); // 'University of Minnesota'
});

City Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-City.mmdb').then(reader => {
  const response = 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'
});

Connection-Type Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-Connection-Type.mmdb').then(reader => {
  const response = reader.connectionType('128.101.101.101');

  console.log(response.connectionType) // 'Cable/DSL'
  console.log(response.ipAddress) // '128.101.101.101'
});

Country Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-Country.mmdb').then(reader => {
  const response = reader.country('128.101.101.101');

  console.log(response.country.isoCode); // 'US'
});

Domain Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-Domain.mmdb').then(reader => {
  const response = reader.domain('128.101.101.101');

  console.log(response.domain) // 'umn.edu'
  console.log(response.ipAddress) // '128.101.101.101'
});

Enterprise Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-Enterprise.mmdb').then(reader => {
  const response = reader.enterprise('128.101.101.101');

  console.log(response.country.isoCode) // 'US'
});

ISP Example

const Reader = require('@maxmind/geoip2-node').Reader;
// Typescript:
// import { Reader } from '@maxmind/geoip2-node';

Reader.open('/usr/local/share/GeoIP/GeoIP2-ISP.mmdb').then(reader => {
  const response = reader.isp('128.101.101.101');

  console.log(response.autonomousSystemNumber); // 217
  console.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'
});

Database Exceptions

If the database file does not exist, is not readable, is invalid, or there is a bug in the reader, the promise will be rejected with an Error with a message explaining the issue.

If the database file and the reader method do not match (e.g. reader.city is used with a Country database), a BadMethodCalledError will be thrown.

If the IP address is not found in the database, an AddressNotFoundError will be thrown.

If the IP address is not valid, a ValueError will be thrown.

If the database buffer is not a valid database, an InvalidDbBufferError will be thrown.

Values to use for Database or Object Keys

We strongly discourage you from using a value from any names property as a key in a database or object.

These names may change between releases. Instead we recommend using one of the following:

  • geoip2-node.CityRecord - city.geonameId
  • geoip2-node.ContinentRecord - continent.code or continent.geonameId
  • geoip2-node.CountryRecord and geoip2.records.RepresentedCountry - country.isoCode or country.geonameId
  • geoip2-node.SubdivisionsRecord - subdivision.isoCode or subdivision.geonameId

What data is returned?

While many of the models contain the same basic records, the attributes which can 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 always have every piece of data for any given IP address.

Because of these factors, it is possible for any request to return a record where some or all of the attributes are unpopulated.

The only piece of data which is always returned is the ipAddress attribute in the geoip2-node.TraitsRecord record.

Integration with GeoNames

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 uniquely identified by a geonameId, which is an integer.

Many of the records returned by the GeoIP web services and databases include a geonameId 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 source things like place names, ISO codes, and other similar data from the GeoNames premium data set.

Reporting Data Problems

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.

Requirements

MaxMind has tested this API with Node.js versions 18, 20, and 22. We aim to support active and maintained LTS versions of Node.js.

Contributing

Patches and pull requests are encouraged. Please include unit tests whenever possible, as we strive to maintain 100% code coverage.

Versioning

The GeoIP2 Node.js API uses Semantic Versioning.

Support

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 contact MaxMind 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.

changelog

CHANGELOG

6.0.0

  • Breaking Internal webservice calls now use Node's built-in fetch instead of http. This will affect users who are on unsupported versions of Node, specifically Node 17 and below.
  • Two new error codes have been added: NETWORK_TIMEOUT and FETCH_ERROR, second of which is returned when there's a fetch related error that could not be handled by other errors.
  • The ip6addr dependency has been removed.
  • metroCode on LocationRecord has been marked deprecated. The code values are no longer being updated.

5.0.0 (2023-12-05)

  • Breaking Drop node 16 support
  • The isAnycast attribute was added to TraitsRecord. This is true if the IP address belongs to an anycast network. This is available for the GeoIP2 Country, City Plus, and Insights web services and the GeoIP2 Country, City, and Enterprise databases.
  • The boolean attributes on the record models are no longer optional. We set missing values to false during construction.

4.2.0 (2023-07-27)

  • Added Satellite to ConnectionType type.

4.1.0 (2023-06-30)

  • Update dependencies. Fixes issue #911

4.0.0 (2023-05-16)

  • Breaking Drop Node 14 support

3.5.0 (2022-10-28)

  • consumer_privacy_network was added to the type union for the userType property in the TraitsRecord interface.
  • Address lodash security vulnerability.

3.4.0 (2022-01-11)

  • Upgrade dependencies
  • Support for mobile country code (MCC) and mobile network codes (MNC) was added for the GeoIP2 ISP and Enterprise databases as well as the GeoIP2 City Plus and Insights web services. mobileCountryCode and mobileNetworkCode attributes were added to Isp for the GeoIP2 ISP database and TraitsRecord for the Enterprise database and the GeoIP2 City Plus and Insights web services. We expect this data to be available by late January, 2022.

3.3.0 (2021-11-29)

  • Upgrade yarn dependencies

3.2.0 (2021-08-17)

  • Upgrade yarn dependencies

3.1.0 (2021-07-08)

  • Upgrade yarn dependencies

3.0.0 (2021-06-03)

  • Breaking Drop node 10 support
  • Upgrade yarn dependencies

2.3.2 (2021-04-13)

  • The staticIpScore property was incorrectly spelled staticIPScore. This is now fixed. Reported by griffyn-showit. GitHub #402.
  • Upgrade yarn dependencies

2.3.1 (2021-03-17)

  • Upgrade yarn dependencies

2.2.1 (2021-02-12)

  • Upgrade yarn dependencies

2.1.1 (2021-01-18)

  • Upgrade yarn dependencies

2.1.0 (2021-01-05)

  • The WebServiceClient class now accepts an options object as the third parameter. The currently valid options are timeout and host. To use the GeoLite2 web service instead of GeoIP2 Precision, set host to geolite.info. If you were previously passing the timeout as the third parameter, this is deprecated and you should transition to passing it in the options object.

2.0.0 (2020-11-02)

Breaking change

  • country and city values return undefined instead of {} when empty.

1.6.0 (2020-09-29)

  • Add the isResidentialProxy property to AnonymousIP and TraitsRecord for use with the Anonymous IP database and GeoIP2 Precision Insights.

1.5.0 (2020-08-07)

  • Add connection-type to traits (Enterprise database)
  • Add API ts-doc documentation

1.4.0 (2020-01-07)

  • Drop support for Node 8.
  • A network property has been added to the various response models. This represents the largest network where all the fields besides the IP address are the same.
  • Add the userCount property to TraitsRecord. This is an integer which indicates the estimated number of users sharing the IP/network during the past 24 hours. This output is available from GeoIP2 Precision Insights.
  • Add the staticIpScore property to TraitsRecord. This is a float which indicates how static or dynamic an IP address is. This output is available from GeoIP2 Precision Insights.

1.3.0 (2019-09-25)

  • Upgrade yarn dependencies

1.2.0 (2020-09-19)

  • Upgrade yarn dependencies

1.1.1 (2020-09-03)

  • Fix path to types. GitHub #53.

1.1.0 (2020-09-03)

  • Upgrade yarn dependencies

1.0.0 (2019-08-22)

  • Fix user-agent header in request
  • Point package.json's main to dist/src/index.js

0.6.0 (2019-06-04)

  • Fix incorrect readerModel return type for country
  • Update yarn.lock modules

0.5.0 (2019-04-15)

  • Update yarn.lock modules

0.4.0 (2018-01-11)

  • Drop support for Node 6.
  • Fix export of models and record interfaces.

0.3.0 (2018-01-02)

  • Add web service API support.

0.2.1 (2018-12-27)

  • Fix Buffer documentation.

0.2.0 (2018-12-21)

  • Add ability to use Buffers instead of local db file.

0.1.1 (2018-11-20)

  • Fix release script.

0.1.0 (2018-11-16)

  • Initial release.