PureBasic Requires Chilkat v11.0.0+
PureBasic
Look up an IP location with IPinfo Core
See more Geolocation Examples
Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.
IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.
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")
CkHttp::setCkAuthToken(http, "IPINFO_TOKEN")
url.i = CkStringBuilder::ckCreate()
If url.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(url,"https://api.ipinfo.io/lookup/")
CkStringBuilder::ckAppend(url,CkHttp::ckUrlEncode(http,ipAddress))
response.i = CkHttpResponse::ckCreate()
If response.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpNoBody(http,"GET",CkStringBuilder::ckGetAsString(url),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)
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.title") = 1
Debug CkJsonObject::ckStringOf(json,"error.title")
EndIf
If CkJsonObject::ckJsonTypeOf(json,"error.message") = 1
Debug CkJsonObject::ckStringOf(json,"error.message")
EndIf
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
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)
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.title") = 1
Debug CkJsonObject::ckStringOf(json,"error.title")
EndIf
If CkJsonObject::ckJsonTypeOf(json,"error.message") = 1
Debug CkJsonObject::ckStringOf(json,"error.message")
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"bogon") = 5) AND (CkJsonObject::ckBoolOf(json,"bogon") = 1)
Debug "This is a reserved or otherwise non-public IP address."
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
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)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
Debug "IP address: " + CkJsonObject::ckStringOf(json,"ip")
If (CkJsonObject::ckJsonTypeOf(json,"geo.country") = 1) AND (CkJsonObject::ckStringOfEquals(json,"geo.country","",1) <> 1)
Debug "Country: " + CkJsonObject::ckStringOf(json,"geo.country")
Else
Debug "Country: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"geo.country_code") = 1) AND (CkJsonObject::ckStringOfEquals(json,"geo.country_code","",1) <> 1)
Debug "Country code: " + CkJsonObject::ckStringOf(json,"geo.country_code")
Else
Debug "Country code: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"geo.region") = 1) AND (CkJsonObject::ckStringOfEquals(json,"geo.region","",1) <> 1)
Debug "Region: " + CkJsonObject::ckStringOf(json,"geo.region")
Else
Debug "Region: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"geo.city") = 1) AND (CkJsonObject::ckStringOfEquals(json,"geo.city","",1) <> 1)
Debug "City: " + CkJsonObject::ckStringOf(json,"geo.city")
Else
Debug "City: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"geo.timezone") = 1) AND (CkJsonObject::ckStringOfEquals(json,"geo.timezone","",1) <> 1)
Debug "Time zone: " + CkJsonObject::ckStringOf(json,"geo.timezone")
Else
Debug "Time zone: unavailable"
EndIf
; Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
If (CkJsonObject::ckJsonTypeOf(json,"geo.latitude") = 2) AND (CkJsonObject::ckJsonTypeOf(json,"geo.longitude") = 2)
Debug "Coordinates: " + CkJsonObject::ckStringOf(json,"geo.latitude") + ", " + CkJsonObject::ckStringOf(json,"geo.longitude")
Else
Debug "Coordinates: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"as.asn") = 1) AND (CkJsonObject::ckStringOfEquals(json,"as.asn","",1) <> 1)
Debug "ASN: " + CkJsonObject::ckStringOf(json,"as.asn")
Else
Debug "ASN: unavailable"
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"as.name") = 1) AND (CkJsonObject::ckStringOfEquals(json,"as.name","",1) <> 1)
Debug "Network organization: " + CkJsonObject::ckStringOf(json,"as.name")
Else
Debug "Network organization: unavailable"
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure