AutoIt Requires Chilkat v11.0.0+
AutoIt
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.
Chilkat AutoIt Downloads
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"
; The free endpoint is HTTP-only. This single lookup sends no API credentials.
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("http://ip-api.com/json/")
$oUrl.Append($oHttp.UrlEncode($sIpAddress))
$oParameters = ObjCreate("Chilkat.JsonObject")
$oParameters.UpdateString("fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
$oResponse = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpParams("GET",$oUrl.GetAsString(),$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
; Save these values if the application will make further requests.
Local $sRemaining = $oResponse.GetHeaderField("X-Rl")
If ($sRemaining = "0") Then
ConsoleWrite("Rate limit reached. Wait " & $oResponse.GetHeaderField("X-Ttl") & " seconds before the next request." & @CRLF)
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("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.StringOfEquals("status","success",True) <> True) Then
ConsoleWrite("The service could not complete the lookup." & @CRLF)
If ($oJson.JsonTypeOf("message") = 1) Then
ConsoleWrite($oJson.StringOf("message") & @CRLF)
EndIf
Exit
EndIf
If (($oJson.JsonTypeOf("query") <> 1) Or ($oJson.StringOfEquals("query","",True) = True)) Then
ConsoleWrite("The response did not contain the expected IP address." & @CRLF)
Exit
EndIf
ConsoleWrite("IP address: " & $oJson.StringOf("query") & @CRLF)
If (($oJson.JsonTypeOf("country") = 1) And ($oJson.StringOfEquals("country","",True) <> True)) Then
ConsoleWrite("Country: " & $oJson.StringOf("country") & @CRLF)
Else
ConsoleWrite("Country: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("countryCode") = 1) And ($oJson.StringOfEquals("countryCode","",True) <> True)) Then
ConsoleWrite("Country code: " & $oJson.StringOf("countryCode") & @CRLF)
Else
ConsoleWrite("Country code: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("regionName") = 1) And ($oJson.StringOfEquals("regionName","",True) <> True)) Then
ConsoleWrite("Region: " & $oJson.StringOf("regionName") & @CRLF)
Else
ConsoleWrite("Region: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("city") = 1) And ($oJson.StringOfEquals("city","",True) <> True)) Then
ConsoleWrite("City: " & $oJson.StringOf("city") & @CRLF)
Else
ConsoleWrite("City: unavailable" & @CRLF)
EndIf
If (($oJson.JsonTypeOf("timezone") = 1) And ($oJson.StringOfEquals("timezone","",True) <> True)) Then
ConsoleWrite("Time zone: " & $oJson.StringOf("timezone") & @CRLF)
Else
ConsoleWrite("Time zone: unavailable" & @CRLF)
EndIf
; Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
If (($oJson.JsonTypeOf("lat") = 2) And ($oJson.JsonTypeOf("lon") = 2)) Then
ConsoleWrite("Coordinates: " & $oJson.StringOf("lat") & ", " & $oJson.StringOf("lon") & @CRLF)
Else
ConsoleWrite("Coordinates: unavailable" & @CRLF)
EndIf