Getting started with IPTrace.

Thanks for trying out IPTrace! Here, we will give you a walk-through on how to get your app or website set up with IPTrace's geolocation API. The code snippets below will show elementary integration with Ruby, PHP, and Node, but as every API, this is both language and framework agnostic: you get vanilla JSON, so any programming environment with a JSON library can use IPTrace.

We assume you have IPTrace account and thereby have an API key to make requests. Please, change the apikey to your own.


Making your first request

Making a request for geolocation data with IPTrace is pretty simple. Try running this in the command line:

          
            curl https://api.iptrace.io/ip/54.144.220.0/json?apikey=API_KEY
          
        

That should return a JSON object that maybe looks something like this:

          
            {
            "country": "United States",
            "continent": "Americas",
            "country_flag": "🇺🇸",
            "CountryCode": "US",
            "City": "Ashburn",
            "Region": "Virginia",
            "lat": 39.0481,
            "lng": -77.4728,
            "tz": "America/New_York",
            "isp": "Amazon",
            "is_anonymous_proxy": false,
            "is_satellite_provider": false,
            "currency": [
            "USD",
            "USN",
            "USS"
            ],
            "country_details": {
            "name": {
            "common": "United States",
            "official": "United States of America",
            "Native": {
            "eng": {
            "common": "United States",
            "official": "United States of America"
            }
            }
            },
            "EuMember": false,
            "LandLocked": false,
            "Nationality": "",
            "tld": [
            ".us"
            ],
            "Languages": {
            "eng": "English"
            },
            "Translations": {
            "FRA": {
            "common": "États-Unis",
            "official": "Les états-unis d'Amérique"
            },
            "HRV": {
            "common": "Sjedinjene Američke Države",
            "official": "Sjedinjene Države Amerike"
            },
            "ITA": {
            "common": "Stati Uniti d'America",
            "official": "Stati Uniti d'America"
            },
            "JPN": {
            "common": "アメリカ合衆国",
            "official": "アメリカ合衆国"
            },
            "NLD": {
            "common": "Verenigde Staten",
            "official": "Verenigde Staten van Amerika"
            },
            "RUS": {
            "common": "Соединённые Штаты Америки",
            "official": "Соединенные Штаты Америки"
            },
            "SPA": {
            "common": "Estados Unidos",
            "official": "Estados Unidos de América"
            }
            },
            "currency": [
            "USD",
            "USN",
            "USS"
            ],
            "Borders": [
            "CAN",
            "MEX"
            ],
            "cca2": "US",
            "cca3": "USA",
            "CIOC": "USA",
            "CCN3": "840",
            "callingCode": [
            "1"
            ],
            "InternationalPrefix": "011",
            "region": "Americas",
            "subregion": "Northern America",
            "Continent": "North America",
            "capital": "Washington D.C.",
            "Area": 9372610,
            "longitude": "97 00 W",
            "latitude": "38 00 N",
            "MinLongitude": -179.23108,
            "MinLatitude": 17.831509,
            "MaxLongitude": -66.885414,
            "MaxLatitude": 71.441055,
            "Latitude": 39.443256,
            "Longitude": -98.95734
            }
            }
          
        

Clear and simple.


cURL

        
curl -X GET /
'https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY'

PHP

With HttpRequest

$request = new HttpRequest();
$request->setUrl( 'https://api.iptrace.io/ip/197.164.124.28/json');
$request->setMethod(HTTP_METH_GET);
$request->setQueryData( array( 'apikey' => 'API_KEY' ));
try { $response = $request->send(); echo $response->getBody(); } catch (HttpException $ex) { echo $ex; }
With cURL
        
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "postman-token: 8c102565-006a-9496-c414-65a7b7a00e20"
          ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          echo "cURL Error #:" . $err;
        } else {
          echo $response;
        }

        }
        
      

Python

Python http.client (Python3)

        
          import http.client

          conn = http.client.HTTPSConnection("api.iptrace.io")

          headers = {
          'cache-control': "no-cache",
          'postman-token': "af79289d-56ce-8e00-16e2-43a279b931e0"
          }

          conn.request("GET", "/ip/197.164.124.28/json?apikey=API_KEY",
          headers=headers)

          res = conn.getresponse()
          data = res.read()
          print(data.decode("utf-8"))
        
      
With Request
        
        import requests

        url = "https://api.iptrace.io/ip/197.164.124.28/json"

        querystring = {"apikey":"API_KEY"}

        headers = {
        'cache-control': "no-cache",
        'postman-token': "95b5b551-7870-b00f-b554-d4f41631bf3b"
        }

        response = requests.request("GET", url, headers=headers, params=querystring)

        print(response.text)
        
      

Nodejs

native

        
          var http = require("https");
          var options = {
            "method": "GET",
            "hostname": "api.iptrace.io",
            "port": null,
            "path": "/ip/197.164.124.28/json?apikey=API_KEY",
            "headers": {
          "cache-control": "no-cache",
          "postman-token": "c6d01235-1513-3e3c-e79f-80f3d0b20a01"
            }
          };

          var req = http.request(options, function (res) {
            var chunks = [];

            res.on("data", function (chunk) {
          chunks.push(chunk);
            });

            res.on("end", function () {
          var body = Buffer.concat(chunks);
          console.log(body.toString());
            });
          });
          req.end();
        
      

With Request
        
        var request = require("request");

        var options = { method: 'GET',
          url: 'https://api.iptrace.io/ip/197.164.124.28/json',
          qs: { apikey: 'API_KEY' },
          headers:
          { 'postman-token': '0010f3d4-76e5-d044-31f3-42ecc4260371',
        'cache-control': 'no-cache' } };

        request(options, function (error, response, body) {
          if (error) throw new Error(error);

          console.log(body);
        });
        
      

Ruby

With NET:Http

        
        require 'uri'
        require 'net/http'

        url = URI("https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY")

        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE

        request = Net::HTTP::Get.new(url)
        request["cache-control"] = 'no-cache'
        request["postman-token"] = 'a4f01260-f493-6218-a952-245020768e29'

        response = http.request(request)
        puts response.read_body
        
      

Go

        
          package main

          import (
            "fmt"
            "net/http"
            "io/ioutil"
          )

          func main() {

            url := "https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY"

            req, _ := http.NewRequest("GET", url, nil)

            req.Header.Add("cache-control", "no-cache")
            req.Header.Add("postman-token", "b3f5610d-291a-6a18-9b5e-27d2d0e6cb5b")

            res, _ := http.DefaultClient.Do(req)

            defer res.Body.Close()
            body, _ := ioutil.ReadAll(res.Body)

            fmt.Println(res)
            fmt.Println(string(body))

          }
        
      

Java

OK HTTP

        
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
          .url("https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY")
          .get()
          .addHeader("cache-control", "no-cache")
          .addHeader("postman-token", "95fee9d2-30eb-5877-ac5b-235255c43f51")
          .build();

        Response response = client.newCall(request).execute();
        
      

Unirest

        
        HttpResponse response = Unirest.get("https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY")
          .header("cache-control", "no-cache")
          .header("postman-token", "4f24365f-81ff-7469-0333-c475afb454bf")
          .asString();
        
      

Swift

NSURL

        
        import Foundation

        let headers = [
          "cache-control": "no-cache",
          "postman-token": "efd01ea4-cd6e-ad57-366a-94130554d360"
        ]

        let request = NSMutableURLRequest(url: NSURL(string: "https://api.iptrace.io/ip/197.164.124.28/json?apikey=API_KEY")! as URL,
        cachePolicy: .useProtocolCachePolicy,
        timeoutInterval: 10.0)
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
          if (error != nil) {
        print(error)
          } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
          }
        })

        dataTask.resume()