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 Tcl Downloads
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"
set url [new_CkStringBuilder]
CkStringBuilder_Append $url "https://api.db-ip.com/v2/free/"
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 "errorCode"] == 1} then {
puts [CkJsonObject_stringOf $json errorCode]
}
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_HasMember $json "errorCode"] == 1] || [[CkJsonObject_JsonTypeOf $json "error"] == 1]} then {
puts "The service could not complete the lookup."
if {[CkJsonObject_JsonTypeOf $json "errorCode"] == 1} then {
puts [CkJsonObject_stringOf $json errorCode]
}
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 {expr [[CkJsonObject_JsonTypeOf $json "ipAddress"] != 1] || [[CkJsonObject_StringOfEquals $json "ipAddress" "" 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 ipAddress]"
if {expr [[CkJsonObject_JsonTypeOf $json "countryName"] == 1] && [[CkJsonObject_StringOfEquals $json "countryName" "" 1] != 1]} then {
puts "Country: [CkJsonObject_stringOf $json countryName]"
} else {
puts "Country: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "countryCode"] == 1] && [[CkJsonObject_StringOfEquals $json "countryCode" "" 1] != 1]} then {
puts "Country code: [CkJsonObject_stringOf $json countryCode]"
} else {
puts "Country code: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "stateProv"] == 1] && [[CkJsonObject_StringOfEquals $json "stateProv" "" 1] != 1]} then {
puts "State or province: [CkJsonObject_stringOf $json stateProv]"
} else {
puts "State or province: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "city"] == 1] && [[CkJsonObject_StringOfEquals $json "city" "" 1] != 1]} then {
puts "City: [CkJsonObject_stringOf $json city]"
} else {
puts "City: unavailable"
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpResponse $response
delete_CkJsonObject $json