Sample code for 30+ languages & platforms
PowerShell Requires Chilkat v11.0.0+

Look up an IP address with ipapi.co

See more Geolocation Examples

Request the HTTPS JSON endpoint for a public IPv4 or IPv6 address. The sample uses the keyless service for evaluation. Use your subscription credentials and limits for production traffic.

Check both the HTTP status and the JSON error flag. An invalid or reserved address can return HTTP 200 with an error object. Unknown location fields are returned as null. The example keeps those values unavailable instead of displaying zero coordinates.

IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.

Service documentation

Chilkat PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
#  Use a public IP address. Provider results can change as their databases are updated.
$ipAddress = "8.8.8.8"
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.ConnectTimeout = 10
$http.ReadTimeout = 30
$http.Accept = "application/json"
$url = New-Object Chilkat.StringBuilder
$url.Append("https://ipapi.co/")
$url.Append($http.UrlEncode($ipAddress))
$url.Append("/json/")
$response = New-Object Chilkat.HttpResponse
$success = $http.HttpNoBody("GET",$url.GetAsString(),$response)
if ($response.StatusCode -eq 0) {
    #  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    $("No HTTP response. Check connectivity, DNS, and TLS configuration.")
    exit
}

$json = New-Object Chilkat.JsonObject
$parsed = $json.Load($response.BodyStr)
if (($success -ne $true) -or ($response.StatusCode -lt 200) -or ($response.StatusCode -ge 300)) {
    $("Lookup failed. HTTP " + $response.StatusCode)
    if ($parsed -eq $true) {
        if ($json.JsonTypeOf("reason") -eq 1) {
            $($json.StringOf("reason"))
        }

        if ($json.JsonTypeOf("message") -eq 1) {
            $($json.StringOf("message"))
        }

    }

    exit
}

if ($parsed -ne $true) {
    $("Expected a JSON object in the lookup response.")
    exit
}

if (($json.JsonTypeOf("error") -eq 5) -and ($json.BoolOf("error") -eq $true)) {
    $("The service could not complete the lookup.")
    if ($json.JsonTypeOf("reason") -eq 1) {
        $($json.StringOf("reason"))
    }

    if ($json.JsonTypeOf("message") -eq 1) {
        $($json.StringOf("message"))
    }

    exit
}

if (($json.JsonTypeOf("ip") -ne 1) -or ($json.StringOfEquals("ip","",$true) -eq $true)) {
    $("The response did not contain the expected IP address.")
    exit
}

$("IP address: " + $json.StringOf("ip"))
if (($json.JsonTypeOf("country_name") -eq 1) -and ($json.StringOfEquals("country_name","",$true) -ne $true)) {
    $("Country: " + $json.StringOf("country_name"))
}
else {
    $("Country: unavailable")
}

if (($json.JsonTypeOf("country_code") -eq 1) -and ($json.StringOfEquals("country_code","",$true) -ne $true)) {
    $("Country code: " + $json.StringOf("country_code"))
}
else {
    $("Country code: unavailable")
}

if (($json.JsonTypeOf("region") -eq 1) -and ($json.StringOfEquals("region","",$true) -ne $true)) {
    $("Region: " + $json.StringOf("region"))
}
else {
    $("Region: unavailable")
}

if (($json.JsonTypeOf("city") -eq 1) -and ($json.StringOfEquals("city","",$true) -ne $true)) {
    $("City: " + $json.StringOf("city"))
}
else {
    $("City: unavailable")
}

if (($json.JsonTypeOf("timezone") -eq 1) -and ($json.StringOfEquals("timezone","",$true) -ne $true)) {
    $("Time zone: " + $json.StringOf("timezone"))
}
else {
    $("Time zone: unavailable")
}

#  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (($json.JsonTypeOf("latitude") -eq 2) -and ($json.JsonTypeOf("longitude") -eq 2)) {
    $("Coordinates: " + $json.StringOf("latitude") + ", " + $json.StringOf("longitude"))
}
else {
    $("Coordinates: unavailable")
}