Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    const json_str = http.quickGetStr(alloc, "https://api.ipdata.co/149.250.207.170?api-key=MY_API_KEY") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);
    try json.load(json_str);

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // 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"
    // }

    var i: i32 = 0;
    var name: [:0]const u8 = "";
    var native: [:0]const u8 = "";

    _ = try json.stringOf(alloc, "ip");
    _ = json.boolOf("is_eu");
    _ = try json.stringOf(alloc, "city");
    _ = try json.stringOf(alloc, "region");
    _ = try json.stringOf(alloc, "region_code");
    _ = try json.stringOf(alloc, "country_name");
    _ = try json.stringOf(alloc, "country_code");
    _ = try json.stringOf(alloc, "continent_name");
    _ = try json.stringOf(alloc, "continent_code");
    _ = try json.stringOf(alloc, "latitude");
    _ = try json.stringOf(alloc, "longitude");
    _ = try json.stringOf(alloc, "asn");
    _ = try json.stringOf(alloc, "organisation");
    _ = try json.stringOf(alloc, "postal");
    _ = try json.stringOf(alloc, "calling_code");
    _ = try json.stringOf(alloc, "flag");
    _ = try json.stringOf(alloc, "emoji_flag");
    _ = try json.stringOf(alloc, "emoji_unicode");
    _ = try json.stringOf(alloc, "currency.name");
    _ = try json.stringOf(alloc, "currency.code");
    _ = try json.stringOf(alloc, "currency.symbol");
    _ = try json.stringOf(alloc, "currency.native");
    _ = try json.stringOf(alloc, "currency.plural");
    _ = try json.stringOf(alloc, "time_zone.name");
    _ = try json.stringOf(alloc, "time_zone.abbr");
    _ = try json.stringOf(alloc, "time_zone.offset");
    _ = json.boolOf("time_zone.is_dst");
    _ = try json.stringOf(alloc, "time_zone.current_time");
    _ = json.boolOf("threat.is_tor");
    _ = json.boolOf("threat.is_proxy");
    _ = json.boolOf("threat.is_anonymous");
    _ = json.boolOf("threat.is_known_attacker");
    _ = json.boolOf("threat.is_known_abuser");
    _ = json.boolOf("threat.is_threat");
    _ = json.boolOf("threat.is_bogon");
    _ = try json.stringOf(alloc, "count");
    i = 0;
    const count_i = json.sizeOfArray("languages");
    while (i < count_i) {
        json.setI(i);
        name = try json.stringOf(alloc, "languages[i].name");
        native = try json.stringOf(alloc, "languages[i].native");
        i = i + 1;
    }
}