Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Look up an IP address with IP2Location.io

See more Geolocation Examples

Look up a public IPv4 or IPv6 address. The sample runs keyless. To use your account allowance, set apiKey and Chilkat will send it as a Bearer token. IP2Location.io recommends header authentication and also accepts its older key query parameter.

Keyless requests have a daily allowance. Registered plans have their own quotas and field availability. Check the provider’s current limits. An HTTP 401 with error 10000 can indicate an invalid key or an exhausted free allowance. Coordinates can default to the capital city when a city is unknown. The returned time_zone is a UTC offset.

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.

Service documentation

Chilkat Lua Downloads

Lua
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"
-- Leave empty for keyless evaluation, or set the API key from your account.
local apiKey = ""
if apiKey ~= "" then
    http.auth_token = apiKey
end

local parameters = chilkat.JsonObject.new()
parameters:update_string("ip", ipAddress)
parameters:update_string("format", "json")
local response = chilkat.HttpResponse.new()
local success = http:http_params("GET", "https://api.ip2location.io/", parameters, 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.error_message") == 1 then
            print(json:string_of("error.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.error_message") == 1 then
        print(json:string_of("error.error_message"))
    end

    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("country_name") == 1) and (not json:string_of_equals("country_name", "", true)) then
    print("Country: " .. json:string_of("country_name"))
else
    print("Country: unavailable")
end

if (json:json_type_of("country_code") == 1) and (not json:string_of_equals("country_code", "", true)) then
    print("Country code: " .. json:string_of("country_code"))
else
    print("Country code: unavailable")
end

if (json:json_type_of("region_name") == 1) and (not json:string_of_equals("region_name", "", true)) then
    print("Region: " .. json:string_of("region_name"))
else
    print("Region: unavailable")
end

if (json:json_type_of("city_name") == 1) and (not json:string_of_equals("city_name", "", true)) then
    print("City: " .. json:string_of("city_name"))
else
    print("City: unavailable")
end

if (json:json_type_of("time_zone") == 1) and (not json:string_of_equals("time_zone", "", true)) then
    print("UTC offset: " .. json:string_of("time_zone"))
else
    print("UTC offset: unavailable")
end

-- Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (json:json_type_of("latitude") == 2) and (json:json_type_of("longitude") == 2) then
    print("Coordinates: " .. json:string_of("latitude") .. ", " .. json:string_of("longitude"))
else
    print("Coordinates: unavailable")
end