The API returns the location of an IP address (country, region, city, zipcode, latitude, longitude) and the associated timezone in XML, JSON or plain text format. You can find below code samples with PHP, Javascript, Ruby, Python and ASP.


For city precision, do a query with the following API (if you omit the IP parameter it will check with your own IP):

For country precision (faster), do a query with the following API:

Note: Supported both the http and https API queries.

If you don’t have an API key yet, just register a free API key. You will need this API key to be able to use our APIs.

API Parameters

Parameter Required Default Value
key Yes API key provided with your free account.
ip No Client IP IPv4 or IPv6 address.
format No raw raw, xml, json
callback No Required when using JSON callback.

Please use the appropriate API for your needs. You can help us keep the load low on our servers by making sure that:

  • If you only need the country name, avoid using the city precision API.
  • If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example).
To remain the stabilities of free service, a rate limit of 2 queries per second applied to our API servers. Any IP querying the API faster than this rate will be temporarily blocked by our firewall. Your IP will be permanently banned if your keep hitting the API server by ignoring this rate limit.
If you are looking for web service with no rate limits and high accuracy, please visit IP2Location Web Service.

Why do I need to register?

We are always trying to improve on our free services and to do so we need to be able to tell who is querying what data and how often. This will enable us to fully optimize our limited resources to providing more stable and reliable service to our users.

What is this API key and do I need it?

This API key is just a random string of characters which uniquely identifies each user. This is how we can track your usage for the purpose of optimizing our resources. Users of our existing APIs need not use this API key but please note that we are in the process of phasing out the old APIs and you will have to use our newer APIs in the future. These new APIs are more optimized for performance and as such we need to be able to allocate resources in an optimal fashion by tracking the usage of the APIs.

How do I get this API key?

This API key is free for everyone. You just have to go here to register and a unique API key will be sent to your email address.

Data accuracy

Over 99.5% on a country level and around 60% on a city level for the US within a 25 mile radius. You can try a few lookups with our IP locator.

Servers uptime

We have multiple servers. They are load-balanced & highly available. Our goal is to have over 99.99% uptime.

PHP example

We wrote a PHP class for our API. You can download it here. Here is an example using all the features of the class:


include 'class.IPInfoDB.php';

// Load the class
$ipinfodb = new IPInfoDB('YOUR_API_KEY');

$results = $ipinfodb->getCity($_SERVER['REMOTE_ADDR']);

// Getting the result
echo "Result
\n"; if (!empty($results) && is_array($results)) { foreach ($results as $key => $value) { echo $key . ' : ' . $value . "
\n"; } }

csharp (C#) example

	public partial class ipinfodb : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

	private void WebAPI()
		string myIP = HttpContext.Current.Request.UserHostAddress;
            string strQuery;
            string key = "";
            HttpWebRequest HttpWReq;
            HttpWebResponse HttpWResp;

            strQuery = "" + "ip=" + myIP + "&key=" + key + "&format=json";

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            HttpWReq = (HttpWebRequest)WebRequest.Create(strQuery);
            HttpWReq.Method = "GET";
            HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();

            System.IO.StreamReader reader = new System.IO.StreamReader(HttpWResp.GetResponseStream());
            string content = reader.ReadToEnd();

            dynamic item = serializer.Deserialize<object>(content);
            string city = item["cityName"];
            string countryc = item["countryCode"];
            string countryn = item["countryName"];
            string region = item["regionName"];
            string lat = item["latitude"];
            string longi = item["longitude"];
            string timez = item["timeZone"];
            string zip = item["zipCode"];

            //displaying the result
            Response.Write("Welcome Visitors from " + city + "<br />");
            Response.Write("Your IP address: " + myIP + "<br />");
            Response.Write("Country Code: " + countryc + "<br />");
            Response.Write("Country Name: " + countryn + "<br />");
            Response.Write("Region Name: " + region + "<br />");
            Response.Write("Latitude: " + lat + "<br />");
            Response.Write("Longitude: " + longi + "<br />");
            Response.Write("Time Zone: " + timez + "<br />");
            Response.Write("Zip Code: " + zip + "<br />");



Putting the geolocation data in a cookie

If you use our API to track your website visitors geolocation, we highly recommend that you put the geolocation data in a cookie (or in a database...). This way, you only query our servers for new visitors, not all page view. To achieve this, here is a quick sample in PHP how to set the country code in a cookie:


include 'class.IPInfoDB.php';

//Set geolocation cookie
if (!isset($_COOKIE['geolocation'])) {
	$ipinfodb = new IPInfoDB('YOUR_API_KEY');

	$result = $ipinfodb->getCountry($_SERVER['REMOTE_ADDR']);

	if ($result['statusCode'] == 'OK') {
		$data = base64_encode(serialize($result));
		setcookie('geolocation', $data, time() + 3600 * 24 * 7); // Set cookie for 1 week
} else {
	$result = unserialize(base64_decode($_COOKIE['geolocation']));


ASP example

	'Get visitor IP
	Dim UserIPAddress
	UserIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
	If UserIPAddress = "" Then
	 UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
	End If
	'Set API key
	api_key = "YOUR_API_KEY"
	'Get info and print
	myxml=""&api_key&"&ip=" &UserIPAddress&"&format=xml"
	set xml = server.CreateObject("MSXML2.DOMDocument.6.0")
	xml.async = "false"
	xml.resolveExternals = "false"
	xml.setProperty "ServerHTTPRequest", true
	response.write "

First result
" for i=0 to 10 response.write xml.documentElement.childNodes(i).nodename & " : " response.write xml.documentElement.childNodes(i).text & "
" NEXT response.write "

" %>

Other languages and modules

If you find or write examples in other languages, please send us the sample code or link at to be included in the list.

Accents encoding

Depending on the locales of your server, you might have trouble displaying accent in country, region and city name. Using PHP, you might need to utf8_encode the answer from our API. For other languages, look at the documentation on how to encode in UTF-8.

Query limit

We do not have a specific daily limit but queries that are at a rate faster than 2 per second will be blocked. If you stay below 2 queries/second everything will be normal. Good programming practices such as not querying our API for all page views (you can store the data in a cookie or a database) will also help not reaching the limit.

If you need to perform more queries in the website, it is better to implement a local database query using IP2Location LITE. It is free for personal or commercial use with attribution required.

IP2Location IP Geolocation

IP2Location Free Widgets

IP2Location Firefox Add-on