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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Use a public IP address. Provider results can change as their databases are updated.
local ipAddress = "8.8.8.8"
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.connect_timeout = 10
http.read_timeout = 30
http.accept = "application/json"
local url = chilkat.StringBuilder.new()
url:append("https://api.db-ip.com/v2/free/")
url:append(http:url_encode(ipAddress))
local response = chilkat.HttpResponse.new()
local success = http:http_no_body("GET", url:get_as_string(), response)
if response.status_code == 0 then
-- Request diagnostics can contain an API key. Report transport failure without logging credentials.
print("No HTTP response. Check connectivity, DNS, and TLS configuration.")
return
end
local json = chilkat.JsonObject.new()
local parsed = json:load(response.body_str)
if (not success) or ((response.status_code < 200) or (response.status_code >= 300)) then
print("Lookup failed. HTTP " .. response.status_code)
if parsed then
if json:json_type_of("errorCode") == 1 then
print(json:string_of("errorCode"))
end
if json:json_type_of("error") == 1 then
print(json:string_of("error"))
end
end
return
end
if not parsed then
print("Expected a JSON object in the lookup response.")
return
end
if (json:has_member("errorCode")) or (json:json_type_of("error") == 1) then
print("The service could not complete the lookup.")
if json:json_type_of("errorCode") == 1 then
print(json:string_of("errorCode"))
end
if json:json_type_of("error") == 1 then
print(json:string_of("error"))
end
return
end
if (json:json_type_of("ipAddress") ~= 1) or (json:string_of_equals("ipAddress", "", true)) then
print("The response did not contain the expected IP address.")
return
end
print("IP address: " .. json:string_of("ipAddress"))
if (json:json_type_of("countryName") == 1) and (not json:string_of_equals("countryName", "", true)) then
print("Country: " .. json:string_of("countryName"))
else
print("Country: unavailable")
end
if (json:json_type_of("countryCode") == 1) and (not json:string_of_equals("countryCode", "", true)) then
print("Country code: " .. json:string_of("countryCode"))
else
print("Country code: unavailable")
end
if (json:json_type_of("stateProv") == 1) and (not json:string_of_equals("stateProv", "", true)) then
print("State or province: " .. json:string_of("stateProv"))
else
print("State or province: unavailable")
end
if (json:json_type_of("city") == 1) and (not json:string_of_equals("city", "", true)) then
print("City: " .. json:string_of("city"))
else
print("City: unavailable")
end