# GeoIP Location API

{% hint style="info" %}
The API host for all tools is <mark style="color:blue;">**tools.datalabs.net**</mark>.
{% endhint %}

***

> ## GeoIP Location API

{% tabs %}
{% tab title="REST API" %}

```json
POST https://tools.datalabs.net/geoIP
Host: tools.datalabs.net
Content-Type: application/json

{
  "key": "DATALABS_API_KEY", // Required - DataLabs API Key
  "host": "22.22.22.22" // Required - IP to be checked
}
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

api_key = "DATALABS_API_KEY"
tool = "geoIP"
host = "22.22.22.22"

url = f"https://tools.datalabs.net/{tool}?key={api_key}&host={host}"
headers = {
    'Host': 'tools.datalabs.net'
}

response = requests.get(url, headers=headers)

try:
    response_data = response.json()
except ValueError:
    response_data = response.text

print(response_data)
```

{% endtab %}

{% tab title="Node.JS" %}

```javascript
const axios = require('axios');

const api_key = "DATALABS_API_KEY";
const tool = "geoIP";
const host = "22.22.22.22";

const url = `https://tools.datalabs.net/${tool}?key=${api_key}&host=${host}`;
const headers = {
    'Host': 'tools.datalabs.net'
};

axios.get(url, { headers })
    .then(response => {
        console.log(response.data);
    })
    .catch(error => {
        if (error.response) {
            console.log(error.response.data);
        } else {
            console.log(error.message);
        }
    });

```

{% endtab %}

{% tab title="Go" %}

```go
package main

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

func main() {
    apiKey := "DATALABS_API_KEY"
    tool := "geoIP"
    host := "22.22.22.22"

    url := fmt.Sprintf("https://tools.datalabs.net/%s?key=%s&host=%s", tool, apiKey, host)
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    req.Header.Set("Host", "tools.datalabs.net")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error making HTTP request:", err)
        return
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        fmt.Printf("Non-OK HTTP status: %s\n", resp.Status)
        return
    }

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }

    var responseData map[string]interface{}
    if err := json.Unmarshal(body, &responseData); err != nil {
        fmt.Println("Error unmarshaling JSON:", err)
        fmt.Println(string(body))
    } else {
        fmt.Println(responseData)
    }
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json; // Ensure this directive is present for JsonReaderException

class Program
{
    static void Main(string[] args)
    {
        // Call the async method
        MainAsync(args).GetAwaiter().GetResult();
    }

    static async Task MainAsync(string[] args)
    {
        string apiKey = "DATALABS_API_KEY";
        string tool = "geoIP";
        string host = "22.22.22.22";
        
        string url = string.Format("https://tools.datalabs.net/{0}?key={1}&host={2}", tool, apiKey, host);
        
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Host", "tools.datalabs.net");
            
            try
            {
                HttpResponseMessage response = await client.GetAsync(url);
                response.EnsureSuccessStatusCode();
                
                string responseBody = await response.Content.ReadAsStringAsync();
                
                try
                {
                    var responseData = JObject.Parse(responseBody);
                    Console.WriteLine("Response data:");
                    Console.WriteLine(responseData);
                }
                catch (JsonReaderException jsonEx)
                {
                    Console.WriteLine("JSON Parsing error: " + jsonEx.Message);
                    Console.WriteLine("Response body:");
                    Console.WriteLine(responseBody);
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("Request error: " + e.Message);
            }
        }
    }
}

```

{% endtab %}
{% endtabs %}

<details>

<summary>Example Response</summary>

```json
{
    "success": true,
    "ip": "22.22.22.22",
    "hostname": "Unknown",
    "records": {
        "country_code": "US",
        "country_code3": "USA",
        "country_name": "United States",
        "region": {
            "code": "Unknown",
            "name": "Unknown"
        },
        "city": "Unknown",
        "postal_code": "Unknown",
        "latitude": "37.751",
        "longitude": "-97.822",
        "metro_code": "Unknown",
        "isp": "DoD Network Information Center",
        "organization": "DNIC-AS-00749",
        "asn": "AS749",
        "timezone": {
            "format": "America/Chicago",
            "time": "Thu, 06 Jun 2024 21:44:27 -0500"
        },
        "continent": {
            "code": "NA",
            "name": "North America"
        }
    }
}
```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.datalabs.net/tools-api/network-tools/geoip-location-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
