Tcl Requires Chilkat v11.0.0+
Tcl
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 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"
# The free endpoint is HTTP-only. This single lookup sends no API credentials.
set url [new_CkStringBuilder]
CkStringBuilder_Append $url "http://ip-api.com/json/"
CkStringBuilder_Append $url [CkHttp_urlEncode $http $ipAddress]
set parameters [new_CkJsonObject]
CkJsonObject_UpdateString $parameters "fields" "status,message,query,country,countryCode,regionName,city,lat,lon,timezone"
set response [new_CkHttpResponse]
set success [CkHttp_HttpParams $http "GET" [CkStringBuilder_getAsString $url] $parameters $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_CkJsonObject $parameters
delete_CkHttpResponse $response
exit
}
# Save these values if the application will make further requests.
set remaining [CkHttpResponse_getHeaderField $response "X-Rl"]
if {$remaining == "0"} then {
puts "Rate limit reached. Wait [CkHttpResponse_getHeaderField $response X-Ttl] seconds before the next request."
}
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 "message"] == 1} then {
puts [CkJsonObject_stringOf $json message]
}
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkJsonObject $parameters
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_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json
exit
}
if {[CkJsonObject_StringOfEquals $json "status" "success" 1] != 1} then {
puts "The service could not complete the lookup."
if {[CkJsonObject_JsonTypeOf $json "message"] == 1} then {
puts [CkJsonObject_stringOf $json message]
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json
exit
}
if {expr [[CkJsonObject_JsonTypeOf $json "query"] != 1] || [[CkJsonObject_StringOfEquals $json "query" "" 1] == 1]} then {
puts "The response did not contain the expected IP address."
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json
exit
}
puts "IP address: [CkJsonObject_stringOf $json query]"
if {expr [[CkJsonObject_JsonTypeOf $json "country"] == 1] && [[CkJsonObject_StringOfEquals $json "country" "" 1] != 1]} then {
puts "Country: [CkJsonObject_stringOf $json country]"
} 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 "regionName"] == 1] && [[CkJsonObject_StringOfEquals $json "regionName" "" 1] != 1]} then {
puts "Region: [CkJsonObject_stringOf $json regionName]"
} else {
puts "Region: 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"
}
if {expr [[CkJsonObject_JsonTypeOf $json "timezone"] == 1] && [[CkJsonObject_StringOfEquals $json "timezone" "" 1] != 1]} then {
puts "Time zone: [CkJsonObject_stringOf $json timezone]"
} else {
puts "Time zone: unavailable"
}
# Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if {expr [[CkJsonObject_JsonTypeOf $json "lat"] == 2] && [[CkJsonObject_JsonTypeOf $json "lon"] == 2]} then {
puts "Coordinates: [CkJsonObject_stringOf $json lat], [CkJsonObject_stringOf $json lon]"
} else {
puts "Coordinates: unavailable"
}
delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json