Sample code for 30+ languages & platforms
Lua

ipdata.co IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipdata.co API.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

local jsonStr = http:quick_get_str("https://api.ipdata.co/149.250.207.170?api-key=MY_API_KEY")
if not http.last_method_success then
    print(http.last_error_text)
    return
end

local json = chilkat.JsonObject.new()
json.emit_compact = false
local success = json:load(jsonStr)

print(json:emit())

-- Sample output:
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

-- {
--   "ip": "149.250.207.170",
--   "is_eu": true,
--   "city": null,
--   "region": null,
--   "region_code": null,
--   "country_name": "Germany",
--   "country_code": "DE",
--   "continent_name": "Europe",
--   "continent_code": "EU",
--   "latitude": 51.2993,
--   "longitude": 9.491,
--   "asn": "AS15854",
--   "organisation": "EntServ Deutschland GmbH",
--   "postal": null,
--   "calling_code": "49",
--   "flag": "https://ipdata.co/flags/de.png",
--   "emoji_flag": "\ud83c\udde9\ud83c\uddea",
--   "emoji_unicode": "U+1F1E9 U+1F1EA",
--   "languages": [
--     {
--       "name": "German",
--       "native": "Deutsch"
--     }
--   ],
--   "currency": {
--     "name": "Euro",
--     "code": "EUR",
--     "symbol": "\u20ac",
--     "native": "\u20ac",
--     "plural": "euros"
--   },
--   "time_zone": {
--     "name": "Europe/Berlin",
--     "abbr": "CEST",
--     "offset": "+0200",
--     "is_dst": true,
--     "current_time": "2019-04-20T23:54:30.715507+02:00"
--   },
--   "threat": {
--     "is_tor": false,
--     "is_proxy": false,
--     "is_anonymous": false,
--     "is_known_attacker": false,
--     "is_known_abuser": false,
--     "is_threat": false,
--     "is_bogon": false
--   },
--   "count": "2"
-- }

local name = ""
local native = ""

local ip = json:string_of("ip")
local is_eu = json:bool_of("is_eu")
local city = json:string_of("city")
local region = json:string_of("region")
local region_code = json:string_of("region_code")
local country_name = json:string_of("country_name")
local country_code = json:string_of("country_code")
local continent_name = json:string_of("continent_name")
local continent_code = json:string_of("continent_code")
local latitude = json:string_of("latitude")
local longitude = json:string_of("longitude")
local asn = json:string_of("asn")
local organisation = json:string_of("organisation")
local postal = json:string_of("postal")
local calling_code = json:string_of("calling_code")
local flag = json:string_of("flag")
local emoji_flag = json:string_of("emoji_flag")
local emoji_unicode = json:string_of("emoji_unicode")
local currencyName = json:string_of("currency.name")
local currencyCode = json:string_of("currency.code")
local currencySymbol = json:string_of("currency.symbol")
local currencyNative = json:string_of("currency.native")
local currencyPlural = json:string_of("currency.plural")
local time_zoneName = json:string_of("time_zone.name")
local time_zoneAbbr = json:string_of("time_zone.abbr")
local time_zoneOffset = json:string_of("time_zone.offset")
local time_zoneIs_dst = json:bool_of("time_zone.is_dst")
local time_zoneCurrent_time = json:string_of("time_zone.current_time")
local threatIs_tor = json:bool_of("threat.is_tor")
local threatIs_proxy = json:bool_of("threat.is_proxy")
local threatIs_anonymous = json:bool_of("threat.is_anonymous")
local threatIs_known_attacker = json:bool_of("threat.is_known_attacker")
local threatIs_known_abuser = json:bool_of("threat.is_known_abuser")
local threatIs_threat = json:bool_of("threat.is_threat")
local threatIs_bogon = json:bool_of("threat.is_bogon")
local count = json:string_of("count")
local i = 0
local count_i = json:size_of_array("languages")
while i < count_i do
    json.i = i
    name = json:string_of("languages[i].name")
    native = json:string_of("languages[i].native")
    i = i + 1
end