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

Look up an IP address with the ip-api free endpoint

See more Geolocation Examples

This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.

Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.

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"
#  The free endpoint is HTTP-only. This single lookup sends no API credentials.
$url = New-Object Chilkat.StringBuilder
$url.Append("http://ip-api.com/json/")
$url.Append($http.UrlEncode($ipAddress))
$parameters = New-Object Chilkat.JsonObject
$parameters.UpdateString("fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
$response = New-Object Chilkat.HttpResponse
$success = $http.HttpParams("GET",$url.GetAsString(),$parameters,$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
}

#  Save these values if the application will make further requests.
$remaining = $response.GetHeaderField("X-Rl")
if ($remaining -eq "0") {
    $("Rate limit reached. Wait " + $response.GetHeaderField("X-Ttl") + " seconds before the next request.")
}

$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("message") -eq 1) {
            $($json.StringOf("message"))
        }

    }

    exit
}

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

if ($json.StringOfEquals("status","success",$true) -ne $true) {
    $("The service could not complete the lookup.")
    if ($json.JsonTypeOf("message") -eq 1) {
        $($json.StringOf("message"))
    }

    exit
}

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

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

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

if (($json.JsonTypeOf("regionName") -eq 1) -and ($json.StringOfEquals("regionName","",$true) -ne $true)) {
    $("Region: " + $json.StringOf("regionName"))
}
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("lat") -eq 2) -and ($json.JsonTypeOf("lon") -eq 2)) {
    $("Coordinates: " + $json.StringOf("lat") + ", " + $json.StringOf("lon"))
}
else {
    $("Coordinates: unavailable")
}