Lua Requires Chilkat v11.0.0+
Lua
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 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"
http.auth_token = "IPINFO_TOKEN"
local url = chilkat.StringBuilder.new()
url:append("https://api.ipinfo.io/lookup/")
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("error.title") == 1 then
print(json:string_of("error.title"))
end
if json:json_type_of("error.message") == 1 then
print(json:string_of("error.message"))
end
end
return
end
if not parsed then
print("Expected a JSON object in the lookup response.")
return
end
if json:json_type_of("error") == 3 then
print("The service could not complete the lookup.")
if json:json_type_of("error.title") == 1 then
print(json:string_of("error.title"))
end
if json:json_type_of("error.message") == 1 then
print(json:string_of("error.message"))
end
return
end
if (json:json_type_of("bogon") == 5) and (json:bool_of("bogon")) then
print("This is a reserved or otherwise non-public IP address.")
return
end
if (json:json_type_of("ip") ~= 1) or (json:string_of_equals("ip", "", true)) then
print("The response did not contain the expected IP address.")
return
end
print("IP address: " .. json:string_of("ip"))
if (json:json_type_of("geo.country") == 1) and (not json:string_of_equals("geo.country", "", true)) then
print("Country: " .. json:string_of("geo.country"))
else
print("Country: unavailable")
end
if (json:json_type_of("geo.country_code") == 1) and (not json:string_of_equals("geo.country_code", "", true)) then
print("Country code: " .. json:string_of("geo.country_code"))
else
print("Country code: unavailable")
end
if (json:json_type_of("geo.region") == 1) and (not json:string_of_equals("geo.region", "", true)) then
print("Region: " .. json:string_of("geo.region"))
else
print("Region: unavailable")
end
if (json:json_type_of("geo.city") == 1) and (not json:string_of_equals("geo.city", "", true)) then
print("City: " .. json:string_of("geo.city"))
else
print("City: unavailable")
end
if (json:json_type_of("geo.timezone") == 1) and (not json:string_of_equals("geo.timezone", "", true)) then
print("Time zone: " .. json:string_of("geo.timezone"))
else
print("Time zone: unavailable")
end
-- Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (json:json_type_of("geo.latitude") == 2) and (json:json_type_of("geo.longitude") == 2) then
print("Coordinates: " .. json:string_of("geo.latitude") .. ", " .. json:string_of("geo.longitude"))
else
print("Coordinates: unavailable")
end
if (json:json_type_of("as.asn") == 1) and (not json:string_of_equals("as.asn", "", true)) then
print("ASN: " .. json:string_of("as.asn"))
else
print("ASN: unavailable")
end
if (json:json_type_of("as.name") == 1) and (not json:string_of_equals("as.name", "", true)) then
print("Network organization: " .. json:string_of("as.name"))
else
print("Network organization: unavailable")
end