PowerShell Requires Chilkat v11.0.0+
PowerShell
Look up an IP address with ipdata
See more Geolocation Examples
Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.
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.
Chilkat PowerShell Downloads
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"
$apiKey = "YOUR_API_KEY"
$url = New-Object Chilkat.StringBuilder
$url.Append("https://api.ipdata.co/")
$url.Append($http.UrlEncode($ipAddress))
$parameters = New-Object Chilkat.JsonObject
$parameters.UpdateString("api-key",$apiKey)
$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("message") -eq 1) {
$($json.StringOf("message"))
}
}
exit
}
if ($parsed -ne $true) {
$("Expected a JSON object in the lookup response.")
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("time_zone.name") -eq 1) -and ($json.StringOfEquals("time_zone.name","",$true) -ne $true)) {
$("Time zone: " + $json.StringOf("time_zone.name"))
}
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")
}
if (($json.JsonTypeOf("asn.asn") -eq 1) -and ($json.StringOfEquals("asn.asn","",$true) -ne $true)) {
$("ASN: " + $json.StringOf("asn.asn"))
}
else {
$("ASN: unavailable")
}
if (($json.JsonTypeOf("asn.name") -eq 1) -and ($json.StringOfEquals("asn.name","",$true) -ne $true)) {
$("Network organization: " + $json.StringOf("asn.name"))
}
else {
$("Network organization: unavailable")
}