• Stars
    star
    223
  • Rank 178,458 (Top 4 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js API for GeoIP2 webservice client and database reader

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.

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'});
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.
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
  • General Node.js error codes

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 16, 18, and 20. 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.

Copyright and License

This software is Copyright (c) 2018-2022 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0.

More Repositories

1

GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
PHP
2,325
star
2

GeoIP2-python

Python code for GeoIP2 webservice client and database reader
Python
1,107
star
3

libmaxminddb

C library for the MaxMind DB file format
C
913
star
4

GeoIP2-java

Java API for GeoIP2 webservice client and database reader
Java
781
star
5

geoipupdate

GeoIP update client code
Go
723
star
6

MaxMind-DB-Reader-php

PHP Reader for the MaxMind DB Database Format
PHP
639
star
7

geoip-api-php

DEPRECATED GeoIP Legacy PHP API
PHP
523
star
8

geoip-api-c

DEPRECATED GeoIP Legacy C API
C
371
star
9

GeoIP2-dotnet

MaxMind GeoIP2 .NET API
C#
348
star
10

web-service-common-php

Shared code for the MaxMind Web Service PHP client APIs
PHP
288
star
11

MaxMind-DB

Spec and test data for the MaxMind DB file format
Go
277
star
12

geoipupdate-legacy

GeoIP update client code
C
258
star
13

geoip-api-python

DEPRECATED GeoIP Legacy Python API
C
232
star
14

geoip2-csv-converter

GeoIP2 CSV Format Converter
Go
202
star
15

MaxMind-DB-Reader-python

Python MaxMind DB reader extension
Python
178
star
16

geoip-api-java

DEPRECATED GeoIP Legacy Java API
Java
176
star
17

mmdbinspect

look up records for one or more IPs/networks in one or more .mmdb databases
Go
130
star
18

mod_maxminddb

MaxMind DB Apache Module
C
126
star
19

MaxMind-DB-Reader-java

Java reader for the MaxMind DB format
Java
114
star
20

mmdbwriter

Go library for writing MaxMind DB (mmdb) files
Go
114
star
21

MaxMind-DB-Reader-dotnet

.NET Reader for the MaxMind DB Database Format
C#
102
star
22

MaxMind-DB-Writer-perl

Create MaxMind DB database files
Perl
75
star
23

GeoIP2-ruby

Ruby API for GeoIP2 webservice client and database reader
Ruby
59
star
24

geoip-api-mod_geoip2

DEPRECATED GeoIP Legacy module for Apache 2
C
50
star
25

minfraud-api-php

PHP API for minFraud Score, Insights, and Factors
PHP
49
star
26

geoip-api-csharp2

DEPRECATED GeoIP Legacy C# API
C#
47
star
27

MaxMind-DB-Reader-ruby

Ruby reader for the MaxMind DB Database Format
Ruby
46
star
28

getting-started-with-mmdb

A quick guide to writing and reading from your own MMDB databases.
Perl
37
star
29

minfraud-api-python

Python API for minFraud Score, Insights, and Factors
Python
27
star
30

mmdb-from-go-blogpost

Enriching MMDB files with your own data using Go.
Go
23
star
31

ccfd-api-php

Deprecated minFraud Legacy PHP API
PHP
23
star
32

minfraud-api-dotnet

.NET API for MaxMind minFraud Score, Insights, and Factors
C#
20
star
33

minfraud-api-java

Java API for minFraud Score, Insights, and Factors
Java
19
star
34

GeoIP2-perl

Perl API for MaxMind's GeoIP2 web services and databases
Perl
18
star
35

mm-geofeed-verifier

Verify the format of a geofeed file, and make some comparisons to data in an MMDB file.
Go
16
star
36

minfraud-api-ruby

Ruby API for minFraud Score, Insights, and Factors
Ruby
14
star
37

minfraud-api-node

Node.js API for MaxMind minFraud Score, Insights, and Factors
TypeScript
14
star
38

mm-network-analyzer

A program to aid in diagnosing networking issues
Go
13
star
39

dev-hire-homework

A homework exercise for engineering applicants
Perl
13
star
40

MaxMind-DB-Reader-perl

Read MaxMind DB files and look up IP addresses
Perl
12
star
41

mmdbverify

Verifier for the MaxMind DB format
Go
10
star
42

geoip-api-perl

DEPRECATED GeoIP Legacy Perl API
Perl
10
star
43

Stepford

A vaguely Rake/Make/Cake-like thing for Perl - create steps and let a runner run them
Perl
9
star
44

ccfd-api-java

Deprecated minFraud Legacy Java API
Java
7
star
45

Locale-Country-Multilingual

mapping ISO codes to localized country names
Perl
6
star
46

Database-Migrator

Mirror of Database-Migrator on urth.org
Perl
5
star
47

Net-Works

Sane APIs for IP addresses and networks
Perl
5
star
48

MaxMind-DB-Reader-XS

Fast XS implementation of MaxMind DB reader
Perl
5
star
49

dev-site

Static site generator for https://dev.maxmind.com.
HTML
4
star
50

xgb2code

A converter for xgboost model dumps to code.
Go
4
star
51

ccfd-api-asp

minFraud ASP API
ASP
3
star
52

webservice-paypal-paymentsadvanced

A simple wrapper around the PayPal Payments Advanced web service
Perl
3
star
53

gatling-gen

C++
2
star
54

geoip-api-mscom

DEPRECATED GeoIP Legacy MS COM API
C
2
star
55

App-CISetup

Command line tools to generate and update Travis and AppVeyor configs for Perl libraries
Perl
2
star
56

api-specs

TypeScript
2
star
57

MaxMind-DB-Common-perl

Code shared by the MaxMind DB reader and writer modules
Perl
2
star
58

WebService-PivotalTracker

Perl library for the Pivotal Tracker REST API
Perl
2
star
59

minfraud-api-perl

Perl API for minFraud Score, Insights, and Factors
Perl
2
star
60

TAP-Formatter-TeamCity

Emit test results as TeamCity build messages
Perl
2
star
61

geolite2-ws-blogpost

Integrating MaxMind's Free and Paid IP Geolocation Web Services (in PHP)
PHP
2
star
62

fuzzing-workshop

Code for Summit Fuzzing Workshop
Go
1
star
63

Dist-Zilla-PluginBundle-MAXMIND

Perl
1
star
64

blog-site

Static site generator for https://blog.maxmind.com.
SCSS
1
star
65

TeamCity-Message

Generate TeamCity build messages
Perl
1
star
66

xgbshap

Calculates feature contributions for XGBoost models
Go
1
star