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

Look up an IP location with the ipify v2 API

See more Geolocation Examples

Use the ipify IP Geolocation v2 country,city endpoint with an API key. This endpoint returns country and city details, including coordinates when available, and currently costs two credits per request. The country endpoint has a smaller result and costs one credit.

The example explicitly supplies the target IPv4 or IPv6 address and URL-encodes the query parameters. location.timezone is a UTC offset. This replaces the older v1 request used by this example.

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
;  Use a public IP address. Provider results can change as their databases are updated.
Local $sIpAddress = "8.8.8.8"
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.ConnectTimeout = 10
$oHttp.ReadTimeout = 30
$oHttp.Accept = "application/json"
$oParameters = ObjCreate("Chilkat.JsonObject")
$oParameters.UpdateString("apiKey","YOUR_API_KEY")
$oParameters.UpdateString("ipAddress",$sIpAddress)
$oResponse = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpParams("GET","https://geo.ipify.org/api/v2/country,city",$oParameters,$oResponse)
If ($oResponse.StatusCode = 0) Then
    ;  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    ConsoleWrite("No HTTP response. Check connectivity, DNS, and TLS configuration." & @CRLF)
    Exit
EndIf

$oJson = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oJson.Load($oResponse.BodyStr)
If (($bSuccess <> True) Or ($oResponse.StatusCode < 200) Or ($oResponse.StatusCode >= 300)) Then
    ConsoleWrite("Lookup failed. HTTP " & $oResponse.StatusCode & @CRLF)
    If ($bParsed = True) Then
        If ($oJson.JsonTypeOf("messages") = 1) Then
            ConsoleWrite($oJson.StringOf("messages") & @CRLF)
        EndIf

        If ($oJson.JsonTypeOf("message") = 1) Then
            ConsoleWrite($oJson.StringOf("message") & @CRLF)
        EndIf

    EndIf

    Exit
EndIf

If ($bParsed <> True) Then
    ConsoleWrite("Expected a JSON object in the lookup response." & @CRLF)
    Exit
EndIf

If (($oJson.JsonTypeOf("ip") <> 1) Or ($oJson.StringOfEquals("ip","",True) = True)) Then
    ConsoleWrite("The response did not contain the expected IP address." & @CRLF)
    Exit
EndIf

ConsoleWrite("IP address: " & $oJson.StringOf("ip") & @CRLF)
If (($oJson.JsonTypeOf("location.country") = 1) And ($oJson.StringOfEquals("location.country","",True) <> True)) Then
    ConsoleWrite("Country code: " & $oJson.StringOf("location.country") & @CRLF)
Else
    ConsoleWrite("Country code: unavailable" & @CRLF)
EndIf

If (($oJson.JsonTypeOf("location.region") = 1) And ($oJson.StringOfEquals("location.region","",True) <> True)) Then
    ConsoleWrite("Region: " & $oJson.StringOf("location.region") & @CRLF)
Else
    ConsoleWrite("Region: unavailable" & @CRLF)
EndIf

If (($oJson.JsonTypeOf("location.city") = 1) And ($oJson.StringOfEquals("location.city","",True) <> True)) Then
    ConsoleWrite("City: " & $oJson.StringOf("location.city") & @CRLF)
Else
    ConsoleWrite("City: unavailable" & @CRLF)
EndIf

If (($oJson.JsonTypeOf("location.timezone") = 1) And ($oJson.StringOfEquals("location.timezone","",True) <> True)) Then
    ConsoleWrite("UTC offset: " & $oJson.StringOf("location.timezone") & @CRLF)
Else
    ConsoleWrite("UTC offset: unavailable" & @CRLF)
EndIf

;  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
If (($oJson.JsonTypeOf("location.lat") = 2) And ($oJson.JsonTypeOf("location.lng") = 2)) Then
    ConsoleWrite("Coordinates: " & $oJson.StringOf("location.lat") & ", " & $oJson.StringOf("location.lng") & @CRLF)
Else
    ConsoleWrite("Coordinates: unavailable" & @CRLF)
EndIf