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

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

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"
$oHttp.BasicAuth = True
$oHttp.Login = "MAXMIND_ACCOUNT_ID"
$oHttp.Password = "MAXMIND_LICENSE_KEY"
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://geoip.maxmind.com/geoip/v2.1/country/")
$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("code") = 1) Then
            ConsoleWrite($oJson.StringOf("code") & @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.JsonTypeOf("traits.ip_address") <> 1) Or ($oJson.StringOfEquals("traits.ip_address","",True) = True)) Then
    ConsoleWrite("The response did not contain the expected IP address." & @CRLF)
    Exit
EndIf

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

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

If (($oJson.JsonTypeOf("continent.names.en") = 1) And ($oJson.StringOfEquals("continent.names.en","",True) <> True)) Then
    ConsoleWrite("Continent: " & $oJson.StringOf("continent.names.en") & @CRLF)
Else
    ConsoleWrite("Continent: unavailable" & @CRLF)
EndIf

If (($oJson.JsonTypeOf("registered_country.iso_code") = 1) And ($oJson.StringOfEquals("registered_country.iso_code","",True) <> True)) Then
    ConsoleWrite("Registered country code: " & $oJson.StringOf("registered_country.iso_code") & @CRLF)
Else
    ConsoleWrite("Registered country code: unavailable" & @CRLF)
EndIf