Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
#  Use a public IP address. Provider results can change as their databases are updated.
set ipAddress "8.8.8.8"
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_ConnectTimeout $http 10
CkHttp_put_ReadTimeout $http 30
CkHttp_put_Accept $http "application/json"
CkHttp_put_BasicAuth $http 1
CkHttp_put_Login $http "MAXMIND_ACCOUNT_ID"
CkHttp_put_Password $http "MAXMIND_LICENSE_KEY"
set url [new_CkStringBuilder]

CkStringBuilder_Append $url "https://geoip.maxmind.com/geoip/v2.1/country/"
CkStringBuilder_Append $url [CkHttp_urlEncode $http $ipAddress]
set response [new_CkHttpResponse]

set success [CkHttp_HttpNoBody $http "GET" [CkStringBuilder_getAsString $url] $response]
if {[CkHttpResponse_get_StatusCode $response] == 0} then {
    #  Request diagnostics can contain an API key. Report transport failure without logging credentials.
    puts "No HTTP response. Check connectivity, DNS, and TLS configuration."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpResponse $response
    exit
}

set json [new_CkJsonObject]

set parsed [CkJsonObject_Load $json [CkHttpResponse_bodyStr $response]]
if {expr [$success != 1]  ||  [expr [[CkHttpResponse_get_StatusCode $response] < 200]  ||  [[CkHttpResponse_get_StatusCode $response] >= 300]]} then {
    puts "Lookup failed. HTTP [CkHttpResponse_get_StatusCode $response]"
    if {$parsed == 1} then {
        if {[CkJsonObject_JsonTypeOf $json "code"] == 1} then {
            puts [CkJsonObject_stringOf $json code]
        }

        if {[CkJsonObject_JsonTypeOf $json "error"] == 1} then {
            puts [CkJsonObject_stringOf $json error]
        }

    }

    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpResponse $response
    delete_CkJsonObject $json
    exit
}

if {$parsed != 1} then {
    puts "Expected a JSON object in the lookup response."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpResponse $response
    delete_CkJsonObject $json
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $json "traits.ip_address"] != 1]  ||  [[CkJsonObject_StringOfEquals $json "traits.ip_address" "" 1] == 1]} then {
    puts "The response did not contain the expected IP address."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpResponse $response
    delete_CkJsonObject $json
    exit
}

puts "IP address: [CkJsonObject_stringOf $json traits.ip_address]"
if {expr [[CkJsonObject_JsonTypeOf $json "country.names.en"] == 1]  &&  [[CkJsonObject_StringOfEquals $json "country.names.en" "" 1] != 1]} then {
    puts "Country: [CkJsonObject_stringOf $json country.names.en]"
} else {
    puts "Country: unavailable"
}

if {expr [[CkJsonObject_JsonTypeOf $json "country.iso_code"] == 1]  &&  [[CkJsonObject_StringOfEquals $json "country.iso_code" "" 1] != 1]} then {
    puts "Country code: [CkJsonObject_stringOf $json country.iso_code]"
} else {
    puts "Country code: unavailable"
}

if {expr [[CkJsonObject_JsonTypeOf $json "continent.names.en"] == 1]  &&  [[CkJsonObject_StringOfEquals $json "continent.names.en" "" 1] != 1]} then {
    puts "Continent: [CkJsonObject_stringOf $json continent.names.en]"
} else {
    puts "Continent: unavailable"
}

if {expr [[CkJsonObject_JsonTypeOf $json "registered_country.iso_code"] == 1]  &&  [[CkJsonObject_StringOfEquals $json "registered_country.iso_code" "" 1] != 1]} then {
    puts "Registered country code: [CkJsonObject_stringOf $json registered_country.iso_code]"
} else {
    puts "Registered country code: unavailable"
}


delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $response
delete_CkJsonObject $json