Documentation

Javascript geolocation developer

Documentation for IP Geolocation Javascript API version 2 (r5578)

In order to use the API in a web browser you have to use your public key. Paid API service users have the option to enable their public key in the API dashboard.

Using dbip.js is the easiest way to write location-aware client side applications. You simply start by enabling the Javascript API with the following addition to your HTML code :

<script src="https://cdn.db-ip.com/js/dbip.js"
	data-api-key="a05p3c1m3n79875p3c1m3nd26cd0n07u5307ac5d">
</script>


Get visitor info as a Javascript geolocation developer



dbip.getVisitor()


In order to fetch information about the visitor IP address you just make a call to dbip.getVisitor().
The function returns a Promise object that resolves to the visitor information


Optional parameters

tryNavigatorGeolocation bool
If enabled getVisitor() will try to use both IP address and navigator geolocation. This can improve accuracy and resolve the distinct locations of multiple visitors sharing a single IP address such as 3G/4G or VPN users. Default value is true.
navigatorGeolocationTimeout int
The time in milliseconds to wait for a navigator geolocation response before getVisitor() only returns location information from IP address lookup. Default value is 5000.
navigatorGeolocationOptions object
The options passed by getVisitor() to the navigator geolocation API. Default value is { enableHighAccuracy: true }





Autofill form fields


Make signup or order forms quicker and easier for your visitors and autofill the country field with their originating country.

When you add a data-auto-select-country attribute to dbip.js script tag, it will automatically fill visitor country for select elements that have a dbip-auto-select-country class :

Alternatively you can use the dbip.autoSelectCountry() method to autofill dynamically loaded forms :

dbip.autoSelectCountry() can optionally take a DOM selector as parameter and returns a Promise object :




Convert currencies


The Core and Extended APIs provide automatic price conversions into your visitors currencies.

When you add a data-auto-convert-currencies attribute to the dbip.js script tag, it will automatically convert currencies for elements that have a dbip-convert-currency class :

Alternatively you can use the dbip.convertCurrencies() method to convert dynamically loaded amounts or provide on-demand conversion to your visitors :

dbip.convertCurrencies() can optionally take a DOM selector as parameter and returns a Promise object.


Advanced conversion options for IP geolocation API developers

When an amount has been converted, a converted class is added so you can use CSS to show or hide it as you see fit. The example below shows how to always display the base currency and only show the converted amount when necessary.

data-format attributes if present are used to customize the format of the displayed amount. The {amount} is replaced by the actual converted amount.

The data-currency-display attribute may be added to either the global dbip.js script tag or individial elements. It specifies how the currency is to be displayed and can be symbol ($), code (USD), or name (dollar).

The code example below is using a combination of those conversion options :



Available currencies

Below is the list of currency codes and corresponding names that are currently supported by the live conversion feature :

AED  Dirham
ARS  Peso
AUD  Dollar
BGN  Lev
BHD  Dinar
BND  Dollar
BRL  Real
BTC  Bitcoin
BWP  Pula
CAD  Dollar
CHF  Franc
CLP  Peso
CNY  Yuan Renminbi
COP  Peso
CZK  Koruna
DKK  Krone
EUR  Euro
GBP  Pound
HKD  Dollar
HRK  Kuna
HUF  Forint
IDR  Rupiah
ILS  Shekel
INR  Rupee
IRR  Rial
ISK  Krona
JPY  Yen
KRW  Won
KWD  Dinar
KZT  Tenge
LKR  Rupee
LYD  Dinar
MUR  Rupee
MXN  Peso
MYR  Ringgit
NOK  Krone
NPR  Rupee
NZD  Dollar
OMR  Rial
PHP  Peso
PKR  Rupee
PLN  Zloty
QAR  Rial
RON  Leu
RUB  Ruble
SAR  Rial
SEK  Krona
SGD  Dollar
THB  Baht
TRY  Lira
TTD  Dollar
TWD  Dollar
USD  Dollar
VEF  Bolivar
ZAR  Rand




Browser geolocation


Browser geolocation is a useful feature that can provide more accurate results than IP address based geolocation. The drawback is that you cannot use it as a reliable source because it is often unavailable for various reasons such as browser support or user permissions.

dbip.getCurrentPosition is a drop-in replacement for the standard navigator.geolocation.getCurrentPosition method.

It takes the same parameters and returns the same values as its standard couterpart, the difference is that it never fails.

If the visitor has browser geolocation turned off or doesn't allow it before the specified timeout, the coordinates are looked up using their IP address :