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

Look up an IP address with the ip-api free endpoint

See more Geolocation Examples

This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.

Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.

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"
-- The free endpoint is HTTP-only. This single lookup sends no API credentials.
local url = chilkat.StringBuilder.new()
url:append("http://ip-api.com/json/")
url:append(http:url_encode(ipAddress))
local parameters = chilkat.JsonObject.new()
parameters:update_string("fields", "status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
local response = chilkat.HttpResponse.new()
local success = http:http_params("GET", url:get_as_string(), 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

-- Save these values if the application will make further requests.
local remaining = response:get_header_field("X-Rl")
if remaining == "0" then
    print("Rate limit reached. Wait " .. response:get_header_field("X-Ttl") .. " seconds before the next request.")
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("message") == 1 then
            print(json:string_of("message"))
        end
    end

    return
end

if not parsed then
    print("Expected a JSON object in the lookup response.")
    return
end

if not json:string_of_equals("status", "success", true) then
    print("The service could not complete the lookup.")
    if json:json_type_of("message") == 1 then
        print(json:string_of("message"))
    end

    return
end

if (json:json_type_of("query") ~= 1) or (json:string_of_equals("query", "", true)) then
    print("The response did not contain the expected IP address.")
    return
end

print("IP address: " .. json:string_of("query"))
if (json:json_type_of("country") == 1) and (not json:string_of_equals("country", "", true)) then
    print("Country: " .. json:string_of("country"))
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("regionName") == 1) and (not json:string_of_equals("regionName", "", true)) then
    print("Region: " .. json:string_of("regionName"))
else
    print("Region: 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

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

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