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 PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
; Use a public IP address. Provider results can change as their databases are updated.
ipAddress.s = "8.8.8.8"
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkConnectTimeout(http, 10)
CkHttp::setCkReadTimeout(http, 30)
CkHttp::setCkAccept(http, "application/json")
; Leave empty for keyless evaluation, or set the API key from your account.
apiKey.s = ""
If apiKey <> ""
CkHttp::setCkAuthToken(http, apiKey)
EndIf
parameters.i = CkJsonObject::ckCreate()
If parameters.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(parameters,"ip",ipAddress)
CkJsonObject::ckUpdateString(parameters,"format","json")
response.i = CkHttpResponse::ckCreate()
If response.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpParams(http,"GET","https://api.ip2location.io/",parameters,response)
If CkHttpResponse::ckStatusCode(response) = 0
; Request diagnostics can contain an API key. Report transport failure without logging credentials.
Debug "No HTTP response. Check connectivity, DNS, and TLS configuration."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
parsed.i = CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(response))
If (success <> 1) OR (CkHttpResponse::ckStatusCode(response) < 200) OR (CkHttpResponse::ckStatusCode(response) >= 300)
Debug "Lookup failed. HTTP " + Str(CkHttpResponse::ckStatusCode(response))
If parsed = 1
If CkJsonObject::ckJsonTypeOf(json,"error.error_message") = 1
Debug CkJsonObject::ckStringOf(json,"error.error_message")
EndIf
EndIf
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If parsed <> 1
Debug "Expected a JSON object in the lookup response."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If CkJsonObject::ckJsonTypeOf(json,"error") = 3
Debug "The service could not complete the lookup."
If CkJsonObject::ckJsonTypeOf(json,"error.error_message") = 1
Debug CkJsonObject::ckStringOf(json,"error.error_message")
EndIf
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"ip") <> 1) OR (CkJsonObject::ckStringOfEquals(json,"ip","",1) = 1)
Debug "The response did not contain the expected IP address."
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
Debug "IP address: " + CkJsonObject::ckStringOf(json,"ip")
If (CkJsonObject::ckJsonTypeOf(json,"country_name") = 1) AND (CkJsonObject::ckStringOfEquals(json,"country_name","",1) <> 1)
Debug "Country: " + CkJsonObject::ckStringOf(json,"country_name")
Else
Debug "Country: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"country_code") = 1) AND (CkJsonObject::ckStringOfEquals(json,"country_code","",1) <> 1)
Debug "Country code: " + CkJsonObject::ckStringOf(json,"country_code")
Else
Debug "Country code: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"region_name") = 1) AND (CkJsonObject::ckStringOfEquals(json,"region_name","",1) <> 1)
Debug "Region: " + CkJsonObject::ckStringOf(json,"region_name")
Else
Debug "Region: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"city_name") = 1) AND (CkJsonObject::ckStringOfEquals(json,"city_name","",1) <> 1)
Debug "City: " + CkJsonObject::ckStringOf(json,"city_name")
Else
Debug "City: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"time_zone") = 1) AND (CkJsonObject::ckStringOfEquals(json,"time_zone","",1) <> 1)
Debug "UTC offset: " + CkJsonObject::ckStringOf(json,"time_zone")
Else
Debug "UTC offset: unavailable"
EndIf
; Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
If (CkJsonObject::ckJsonTypeOf(json,"latitude") = 2) AND (CkJsonObject::ckJsonTypeOf(json,"longitude") = 2)
Debug "Coordinates: " + CkJsonObject::ckStringOf(json,"latitude") + ", " + CkJsonObject::ckStringOf(json,"longitude")
Else
Debug "Coordinates: unavailable"
EndIf
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure