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

Look up an IP location with the ipify v2 API

See more Geolocation Examples

Use the ipify IP Geolocation v2 country,city endpoint with an API key. This endpoint returns country and city details, including coordinates when available, and currently costs two credits per request. The country endpoint has a smaller result and costs one credit.

The example explicitly supplies the target IPv4 or IPv6 address and URL-encodes the query parameters. location.timezone is a UTC offset. This replaces the older v1 request used by this example.

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"
local parameters = chilkat.JsonObject.new()
parameters:update_string("apiKey", "YOUR_API_KEY")
parameters:update_string("ipAddress", ipAddress)
local response = chilkat.HttpResponse.new()
local success = http:http_params("GET", "https://geo.ipify.org/api/v2/country,city", 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("messages") == 1 then
            print(json:string_of("messages"))
        end

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

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

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

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

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