Geolocation of IP Address
See more REST Examples
Retrieve country, state or province, and city for a public IP address using the DB-IP v2 free JSON API. This replaces the retired freegeoip.net request and the historical two-service comparison. The sample sends one HTTPS request, checks transport and service errors, and reads the returned fields.
DB-IP documents 500 daily requests for its free service. Its free HTTPS endpoint returned a successful lookup during verification on November 20, 2026. For other providers and authenticated services, see the Geolocation examples.
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"
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://api.db-ip.com/v2/free/")
$oUrl.Append($oHttp.UrlEncode($sIpAddress))
$oResponse = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$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("errorCode") = 1) Then
ConsoleWrite($oJson.StringOf("errorCode") & @CRLF)
EndIf
If ($oJson.JsonTypeOf("error") = 1) Then
ConsoleWrite($oJson.StringOf("error") & @CRLF)
EndIf
EndIf
Exit
EndIf
If ($bParsed <> True) Then
ConsoleWrite("Expected a JSON object in the lookup response." & @CRLF)
Exit
EndIf
If (($oJson.HasMember("errorCode") = True) Or ($oJson.JsonTypeOf("error") = 1)) Then
ConsoleWrite("The service could not complete the lookup." & @CRLF)
If ($oJson.JsonTypeOf("errorCode") = 1) Then
ConsoleWrite($oJson.StringOf("errorCode") & @CRLF)
EndIf
If ($oJson.JsonTypeOf("error") = 1) Then
ConsoleWrite($oJson.StringOf("error") & @CRLF)
EndIf
Exit
EndIf
If (($oJson.JsonTypeOf("ipAddress") <> 1) Or ($oJson.StringOfEquals("ipAddress","",True) = True)) Then
ConsoleWrite("The response did not contain the expected IP address." & @CRLF)
Exit
EndIf
ConsoleWrite("IP address: " & $oJson.StringOf("ipAddress") & @CRLF)
If (($oJson.JsonTypeOf("countryName") = 1) And ($oJson.StringOfEquals("countryName","",True) <> True)) Then
ConsoleWrite("Country: " & $oJson.StringOf("countryName") & @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("stateProv") = 1) And ($oJson.StringOfEquals("stateProv","",True) <> True)) Then
ConsoleWrite("State or province: " & $oJson.StringOf("stateProv") & @CRLF)
Else
ConsoleWrite("State or province: 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