JavaScript
Official NodeJS client library for the ipdata API - https://github.com/ipdata/node

Install

1
$ npm install ipdata
Copied!

Use

Import library

Import the library.
1
import IPData from 'ipdata';
Copied!
Note: If you are using require() then you will need to use the default value exported from the library.
1
const IPData = require('ipdata').default;
Copied!

Create an Instance

Create an instance of the IPData class and pass your api key for IPData as the first parameter.
1
const ipdata = new IPData('<apiKey>');
Copied!
The library will cache 4096 ip addresses responses for 24 hours using a LRU cache by default. You can configure the cache by passing an object as the second paramenter.
1
const cacheConfig = {
2
max: 1000, // max size
3
maxAge: 10 * 60 * 1000, // max age in ms (i.e. 10 minutes)
4
};
5
const ipdata = new IPData('<apiKey>', cacheConfig);
Copied!
Note: To disable the cache pass -1 as the maxAge.
1
const cacheConfig = {
2
maxAge: -1, // disable the cache
3
};
4
const ipdata = new IPData('<apiKey>', cacheConfig);
Copied!

Lookup

The library will lookup the ip address of the host computer if no ip address is provided.
1
ipdata.lookup()
2
.then(function(info) {
3
// info.ip === '<hostcomputerip>'
4
// ...
5
});
Copied!
You can pass an ip address as the first parameter to the lookup() method to lookup information about the ip address using IPData.
1
const ip = '1.1.1.1';
2
ipdata.lookup(ip)
3
.then(function(info) {
4
// info.ip === 1.1.1.1
5
// ...
6
});
Copied!
You can specify only a select field to be returned when looking up an ip address by passing a field as the second parameter to the lookup() method.
1
const ip = '1.1.1.1';
2
const selectField = 'ip';
3
ipdata.lookup(ip, selectField)
4
.then(function(info) {
5
// info.select_field === 1.1.1.1
6
// ...
7
});
Copied!
You can specify only certain fields to be returned when looking up an ip address by passing an array of fields as the third parameter to the lookup() method.
1
const ip = '1.1.1.1';
2
const fields = ['ip', 'city'];
3
ipdata.lookup(ip, null, fields)
4
.then(function(info) {
5
// ...
6
});
Copied!

Bulk Lookup

You can lookup multiple ip addresses with one API call using the bulkLookup() method.
1
const ips = ['1.1.1.1', '1.0.0.1'];
2
ipdata.bulkLookup(ips)
3
.then(function(info) {
4
// info[0].ip === 1.1.1.1
5
// ...
6
});
Copied!
You can specify only certain fields to be returned when looking up multiple ip addresses by passing an array of fields as the second parameter to the bulkLookup() method.
1
const ips = ['1.1.1.1', '1.0.0.1'];
2
const fields = ['ip', 'city'];
3
ipdata.bulkLookup(ips, fields)
4
.then(function(info) {
5
// ...
6
});
Copied!
Last modified 1yr ago