Geolocation of IP Address
See more REST Examples
Retrieve country, state or province, and city for a public IP address using the DB-IP v2 free JSON API. This replaces the retired freegeoip.net request and the historical two-service comparison. The sample sends one HTTPS request, checks transport and service errors, and reads the returned fields.
DB-IP documents 500 daily requests for its free service. Its free HTTPS endpoint returned a successful lookup during verification on November 20, 2026. For other providers and authenticated services, see the Geolocation examples.
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")
url.i = CkStringBuilder::ckCreate()
If url.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(url,"https://api.db-ip.com/v2/free/")
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,"errorCode") = 1
Debug CkJsonObject::ckStringOf(json,"errorCode")
EndIf
If CkJsonObject::ckJsonTypeOf(json,"error") = 1
Debug CkJsonObject::ckStringOf(json,"error")
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::ckHasMember(json,"errorCode") = 1) OR (CkJsonObject::ckJsonTypeOf(json,"error") = 1)
Debug "The service could not complete the lookup."
If CkJsonObject::ckJsonTypeOf(json,"errorCode") = 1
Debug CkJsonObject::ckStringOf(json,"errorCode")
EndIf
If CkJsonObject::ckJsonTypeOf(json,"error") = 1
Debug CkJsonObject::ckStringOf(json,"error")
EndIf
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(json,"ipAddress") <> 1) OR (CkJsonObject::ckStringOfEquals(json,"ipAddress","",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,"ipAddress")
If (CkJsonObject::ckJsonTypeOf(json,"countryName") = 1) AND (CkJsonObject::ckStringOfEquals(json,"countryName","",1) <> 1)
Debug "Country: " + CkJsonObject::ckStringOf(json,"countryName")
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,"stateProv") = 1) AND (CkJsonObject::ckStringOfEquals(json,"stateProv","",1) <> 1)
Debug "State or province: " + CkJsonObject::ckStringOf(json,"stateProv")
Else
Debug "State or province: 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
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(url)
CkHttpResponse::ckDispose(response)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure