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

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

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"
http.basic_auth = true
http.login = "MAXMIND_ACCOUNT_ID"
http.password = "MAXMIND_LICENSE_KEY"
local url = chilkat.StringBuilder.new()
url:append("https://geoip.maxmind.com/geoip/v2.1/country/")
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("code") == 1 then
            print(json:string_of("code"))
        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:json_type_of("traits.ip_address") ~= 1) or (json:string_of_equals("traits.ip_address", "", true)) then
    print("The response did not contain the expected IP address.")
    return
end

print("IP address: " .. json:string_of("traits.ip_address"))
if (json:json_type_of("country.names.en") == 1) and (not json:string_of_equals("country.names.en", "", true)) then
    print("Country: " .. json:string_of("country.names.en"))
else
    print("Country: unavailable")
end

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

if (json:json_type_of("continent.names.en") == 1) and (not json:string_of_equals("continent.names.en", "", true)) then
    print("Continent: " .. json:string_of("continent.names.en"))
else
    print("Continent: unavailable")
end

if (json:json_type_of("registered_country.iso_code") == 1) and (not json:string_of_equals("registered_country.iso_code", "", true)) then
    print("Registered country code: " .. json:string_of("registered_country.iso_code"))
else
    print("Registered country code: unavailable")
end