1.0.1 • Published 4 years ago

location-by-ip v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Location by IP

npm.io npm.io

This module returns the location of a given IP address (v4 or v6).

Data comes from Spott, which is a geographical API.

Usage

  1. Install:
npm install --save location-by-ip
  1. Get a Spott API Key here.

  2. Use it in your project:

const GetLocation = require('location-by-ip');
const SPOTT_API_KEY = 'PUT_YOUR_API_KEY_HERE';

const getLocation = new GetLocation(SPOTT_API_KEY);

// Start using the client. For Example:
// const location = await getLocation.byMyIp();

Data

This module will try to return the most precise location available, which is a CITY. If there's no enough information it will return a COUNTRY.

The properties returned for both responses are:

PropertyTypeDescription
idStringUnique identifier given by Spott.
geonameIdIntegerUnique identifier given by GeoNames.
typeStringThe classification of the place. Possible values are: CITY and COUNTRY
nameStringDefault name of the place (usually in English). This property always has a value.
localizedNameStringLocalized name of the place in the requested language. This property is only present when option language is specified. It's null when the translation is not available.
populationIntegerThe approximate population living in the place.
elevationFloatThe approximate elevation from sea level. Value is expressed in meters.
coordinatesObjectThe geographic coordinates where the place is located.
coordinates.latitudeFloatLatitude component from the geographic coordinates of the place.
coordinates.longitudeFloatLongitude component from the geographic coordinates of the place.
timezoneIdStringTime zone associated to the place. This property is null for countries since they may have multiple.
adminDivision2ObjectA minimal version of the Administrative Division level 2 where the place is located. This property is only present for places of type: CITY. The object contain the properties: id, geonameId, name and localizedName.
adminDivision1ObjectA minimal version of the Administrative Division level 1 where the place is located. This property is only present for places of type: CITY. The object contain the properties: id, geonameId, name and localizedName.
countryObjectA minimal version of the Country where the place is located. This property is only present for places of type: CITY. The object contain the properties: id, geonameId, name and localizedName.

API

.byIp(ip, options)

Returns the location of a given IP Address, or null when the location is not available.

Parameters

ParamterTypeDescription
ipStringIP Address (v4 and v6 are supported).
optionsObjectOptional object with modifiers for the call.
options.languageStringSpecifies a language ISO 639-1 to get the localized name of the place. If the translation is not available, "localizedName" property will be null.

Example

const options = {
  language: 'ru' // Russian
};

const location = await getLocation.byIp('200.194.51.97', options);

/*
console.log(location);
Prints:

{
  "id": "4005539",
  "geonameId": 4005539,
  "type": "CITY",
  "name": "Guadalajara",
  "localizedName": "Гвадалахара",
  "population": 1495182,
  "elevation": 1598,
  "timezoneId": "America/Mexico_City",
  "country": {
    "id": "MX",
    "geonameId": 3996063,
    "name": "Mexico",
    "localizedName": "Мексика"
  },
  "adminDivision1": {
    "id": "MX.14",
    "geonameId": 4004156,
    "name": "Jalisco",
    "localizedName": null
  },
  "adminDivision2": {
    "id": "MX.14.039",
    "geonameId": 8582140,
    "name": "Guadalajara",
    "localizedName": null
  },
  "coordinates": {
    "latitude": 20.6668,
    "longitude": -103.392
  }
}

*/

.byMyIp(options)

Returns the location related to the IP where the request comes from, or null when the location is not available.

Parameters

ParamterTypeDescription
optionsObjectOptional object with modifiers for the call.
options.languageStringSpecifies a language ISO 639-1 to get the localized name of the place. If the translation is not available, "localizedName" property will be null.

Example

const options = {
  language: 'fr' // French
};

const location = await getLocation.byMyIp(options);

/*
console.log(location);
Prints:

{
  "id": "5391959",
  "geonameId": 5391959,
  "type": "CITY",
  "name": "San Francisco",
  "localizedName": "San Francisco",
  "population": 864816,
  "elevation": 16,
  "timezoneId": "America/Los_Angeles",
  "coordinates": {
    "latitude": 37.7749,
    "longitude": -122.419
  },
  "country": {
    "id": "US",
    "geonameId": 6252001,
    "name": "United States of America",
    "localizedName": "États-Unis"
  },
  "adminDivision1": {
    "id": "US.CA",
    "geonameId": 5332921,
    "name": "California",
    "localizedName": "Californie"
  },
  "adminDivision2": {
    "id": "US.CA.075",
    "geonameId": 5391997,
    "name": "San Francisco County",
    "localizedName": "Comté de San Francisco"
  }
}

*/

Related projects

License

MIT