Look up an IP address with IP2Location.io
See more Geolocation Examples
Look up a public IPv4 or IPv6 address. The sample runs keyless. To use your account allowance, set apiKey and Chilkat will send it as a Bearer token. IP2Location.io recommends header authentication and also accepts its older key query parameter.
Keyless requests have a daily allowance. Registered plans have their own quotas and field availability. Check the provider’s current limits. An HTTP 401 with error 10000 can indicate an invalid key or an exhausted free allowance. Coordinates can default to the capital city when a city is unknown. The returned time_zone is a UTC offset.
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"
# Leave empty for keyless evaluation, or set the API key from your account.
set apiKey ""
if {$apiKey != ""} then {
CkHttp_put_AuthToken $http $apiKey
}
set parameters [new_CkJsonObject]
CkJsonObject_UpdateString $parameters "ip" $ipAddress
CkJsonObject_UpdateString $parameters "format" "json"
set response [new_CkHttpResponse]
set success [CkHttp_HttpParams $http "GET" "https://api.ip2location.io/" $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_CkJsonObject $parameters
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 "error.error_message"] == 1} then {
puts [CkJsonObject_stringOf $json error.error_message]
}
}
delete_CkHttp $http
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_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json
exit
}
if {[CkJsonObject_JsonTypeOf $json "error"] == 3} then {
puts "The service could not complete the lookup."
if {[CkJsonObject_JsonTypeOf $json "error.error_message"] == 1} then {
puts [CkJsonObject_stringOf $json error.error_message]
}
delete_CkHttp $http
delete_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json
exit
}
if {expr [[CkJsonObject_JsonTypeOf $json "ip"] != 1] || [[CkJsonObject_StringOfEquals $json "ip" "" 1] == 1]} then {
puts "The response did not contain the expected IP address."
delete_CkHttp $http
delete_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json
exit
}
puts "IP address: [CkJsonObject_stringOf $json ip]"
if {expr [[CkJsonObject_JsonTypeOf $json "country_name"] == 1] && [[CkJsonObject_StringOfEquals $json "country_name" "" 1] != 1]} then {
puts "Country: [CkJsonObject_stringOf $json country_name]"
} else {
puts "Country: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "country_code"] == 1] && [[CkJsonObject_StringOfEquals $json "country_code" "" 1] != 1]} then {
puts "Country code: [CkJsonObject_stringOf $json country_code]"
} else {
puts "Country code: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "region_name"] == 1] && [[CkJsonObject_StringOfEquals $json "region_name" "" 1] != 1]} then {
puts "Region: [CkJsonObject_stringOf $json region_name]"
} else {
puts "Region: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "city_name"] == 1] && [[CkJsonObject_StringOfEquals $json "city_name" "" 1] != 1]} then {
puts "City: [CkJsonObject_stringOf $json city_name]"
} else {
puts "City: unavailable"
}
if {expr [[CkJsonObject_JsonTypeOf $json "time_zone"] == 1] && [[CkJsonObject_StringOfEquals $json "time_zone" "" 1] != 1]} then {
puts "UTC offset: [CkJsonObject_stringOf $json time_zone]"
} else {
puts "UTC offset: unavailable"
}
# Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if {expr [[CkJsonObject_JsonTypeOf $json "latitude"] == 2] && [[CkJsonObject_JsonTypeOf $json "longitude"] == 2]} then {
puts "Coordinates: [CkJsonObject_stringOf $json latitude], [CkJsonObject_stringOf $json longitude]"
} else {
puts "Coordinates: unavailable"
}
delete_CkHttp $http
delete_CkJsonObject $parameters
delete_CkHttpResponse $response
delete_CkJsonObject $json