IP Geolocation API Typescript SDK

In this document, you will go through the basic steps to use IP Geolocation/GeoIP API Typescript SDK.
You need a valid 'ipgeolocation API key' to use this SDK. Sign up here and get your free API key if you don’t have one.

System Requirements

Internet connection is required to run this component.

Installation

NPM

$ npm install ip-geolocation-api-sdk-typescript

Basic Usage

Setup API

import { IPGeolocationAPI } from './node_modules/ip-geolocation-api-sdk-typescript/IPGeolocationAPI';

// Create IPGeolocationAPI object. Constructor takes two parameters.
// 1) API key (Optional: To authenticate your requests through "Request Origin", you can skip it.)
// 2) Async (Optional: It is used to toggle "async" mode in the requests. By default, it is true.)
let ipgeolocationApi = new IPGeolocationAPI("YOUR_API_KEY", false); 

Geolocation Lookup

import { GeolocationParams } from './node_modules/ip-geolocation-api-sdk-typescript/GeolocationParams';

// Function to handle API response
function handleResponse(json) {
    console.log(json);
}

// Get complete geolocation for the calling machine's IP address
ipgeolocationApi.getGeolocation(handleResponse);

// Get complete geolocation in Russian** for IP address (1.1.1.1)
let geolocationParams = new GeolocationParams();
geolocationParams.setIPAddress('1.1.1.1');
geolocationParams.setLang('ru');

ipgeolocationApi.getGeolocation(handleResponse, geolocationParams);

// Get custom geolocation (only "geo, time_zone and currency" fields/objects) for an IP address (1.1.1.1)
let geolocationParams = new GeolocationParams();
geolocationParams.setIPAddress('1.1.1.1'); 
geolocationParams.setFields('geo,time_zone,currency');

ipgeolocationApi.getGeolocation(handleResponse, geolocationParams);

// Exclude fields/obejects from complete geolocation in Italian language
let geolocationParams = new GeolocationParams();
geolocationParams.setExcludes('continent_name,country_code3,time_zone');
geolocationParams.setLang('it');

ipgeolocationApi.getGeolocation(handleResponse, geolocationParams);

Bulk Geolocations Lookup

// Query geolocation in German** for multiple IP addresses and all fields
let geolocationParams = new GeolocationParams();
geolocationParams.setLang('de');
geolocationParams.setIPAddresses(['1.1.1.1', '2.2.2.2', '3.3.3.3']);

ipgeolocationApi.getGeolocation(handleResponse, geolocationParams);

// Specify the required fields/objects for multiple IP addresses
let geolocationParams = new GeolocationParams();
geolocationParams.setIPAddresses(['1.1.1.1', '2.2.2.2', '3.3.3.3']);
geolocationParams.setFields('geo');

ipgeolocationApi.getGeolocation(geolocationParams, geoResponse);

Timezone API

import { TimezoneParams } from './node_modules/ip-geolocation-api-sdk-typescript/TimezoneParams';

// Get time zone information by time zone ID
let timezoneParams = new TimezoneParams();
timezoneParams.setTimezone('America/Los_Angeles');

ipgeolocationApi.getTimezone(handleResponse, timezoneParams);

// Get time zone information by latitude and longitude of the location
let timezoneParams = new TimezoneParams();
timezoneParams.setLocation('37.1838139', '-123.8105225');

ipgeolocationApi.getTimezone(handleResponse, timezoneParams);

// Get time zone information for IP address (1.1.1.1) and geolocation information Japanese**
let timezoneParams = new TimezoneParams();
timezoneParams.setIPAddress('1.1.1.1');

ipgeolocationApi.getTimezone(handleResponse, timezoneParams);

// Query time zone information for calling machine's IP address
ipgeolocationApi.getTimezone(handleResponse);

** IPGeolocation provides geolocation information in the following languages:

  • English (en)
  • German (de)
  • Russian (ru)
  • Japanese (ja)
  • French (fr)
  • Chinese Simplified (cn)
  • Spanish (es)
  • Czech (cs)
  • Italian (it)

By default, geolocation information is returned in English. Response in a language other than English is available to paid users only.

Commands To Run Typescript Application

  1. tsc *.ts
  2. node *.js

Source Code

The complete source code for this SDK is available at Github.