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

Look up an IP address with ipstack over HTTPS

See more Geolocation Examples

Set your ipstack access key and request one public IP address over HTTPS. HTTP status errors and the API’s success=false error object are checked separately. Account quotas and optional modules depend on the subscription. The example displays core geolocation fields and handles null values.

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"
$accessKey = "YOUR_ACCESS_KEY"
$url = New-Object Chilkat.StringBuilder
$url.Append("https://api.ipstack.com/")
$url.Append($http.UrlEncode($ipAddress))
$parameters = New-Object Chilkat.JsonObject
$parameters.UpdateString("access_key",$accessKey)
$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
}

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

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

    }

    exit
}

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

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

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

    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_name") -eq 1) -and ($json.StringOfEquals("region_name","",$true) -ne $true)) {
    $("Region: " + $json.StringOf("region_name"))
}
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("zip") -eq 1) -and ($json.StringOfEquals("zip","",$true) -ne $true)) {
    $("Postal code: " + $json.StringOf("zip"))
}
else {
    $("Postal code: 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")
}