PureBasic Requires Chilkat v11.0.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.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")
; The free endpoint is HTTP-only. This single lookup sends no API credentials.
url.i = CkStringBuilder::ckCreate()
If url.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(url,"http://ip-api.com/json/")
CkStringBuilder::ckAppend(url,CkHttp::ckUrlEncode(http,ipAddress))
parameters.i = CkJsonObject::ckCreate()
If parameters.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(parameters,"fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
response.i = CkHttpResponse::ckCreate()
If response.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpParams(http,"GET",CkStringBuilder::ckGetAsString(url),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)
CkStringBuilder::ckDispose(url)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
ProcedureReturn
EndIf
; Save these values if the application will make further requests.
remaining.s = CkHttpResponse::ckGetHeaderField(response,"X-Rl")
If remaining = "0"
Debug "Rate limit reached. Wait " + CkHttpResponse::ckGetHeaderField(response,"X-Ttl") + " seconds before the next request."
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,"message") = 1
Debug CkJsonObject::ckStringOf(json,"message")
EndIf
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
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)
CkStringBuilder::ckDispose(url)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If CkJsonObject::ckStringOfEquals(json,"status","success",1) <> 1
Debug "The service could not complete the lookup."
If CkJsonObject::ckJsonTypeOf(json,"message") = 1
Debug CkJsonObject::ckStringOf(json,"message")
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"query") <> 1) OR (CkJsonObject::ckStringOfEquals(json,"query","",1) = 1)
Debug "The response did not contain the expected IP address."
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
Debug "IP address: " + CkJsonObject::ckStringOf(json,"query")
If (CkJsonObject::ckJsonTypeOf(json,"country") = 1) AND (CkJsonObject::ckStringOfEquals(json,"country","",1) <> 1)
Debug "Country: " + CkJsonObject::ckStringOf(json,"country")
Else
Debug "Country: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"countryCode") = 1) AND (CkJsonObject::ckStringOfEquals(json,"countryCode","",1) <> 1)
Debug "Country code: " + CkJsonObject::ckStringOf(json,"countryCode")
Else
Debug "Country code: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"regionName") = 1) AND (CkJsonObject::ckStringOfEquals(json,"regionName","",1) <> 1)
Debug "Region: " + CkJsonObject::ckStringOf(json,"regionName")
Else
Debug "Region: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"city") = 1) AND (CkJsonObject::ckStringOfEquals(json,"city","",1) <> 1)
Debug "City: " + CkJsonObject::ckStringOf(json,"city")
Else
Debug "City: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"timezone") = 1) AND (CkJsonObject::ckStringOfEquals(json,"timezone","",1) <> 1)
Debug "Time zone: " + CkJsonObject::ckStringOf(json,"timezone")
Else
Debug "Time zone: unavailable"
EndIf
; Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
If (CkJsonObject::ckJsonTypeOf(json,"lat") = 2) AND (CkJsonObject::ckJsonTypeOf(json,"lon") = 2)
Debug "Coordinates: " + CkJsonObject::ckStringOf(json,"lat") + ", " + CkJsonObject::ckStringOf(json,"lon")
Else
Debug "Coordinates: unavailable"
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkJsonObject::ckDispose(parameters)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure