Sample code for 30+ languages & platforms
Zig

ipstack.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipstack.com REST 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();

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    const json_str = http.quickGetStr(alloc, "http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_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",
    //   "type": "ipv4",
    //   "continent_code": "EU",
    //   "continent_name": "Europe",
    //   "country_code": "DE",
    //   "country_name": "Germany",
    //   "region_code": null,
    //   "region_name": null,
    //   "city": null,
    //   "zip": null,
    //   "latitude": 51.2993,
    //   "longitude": 9.491,
    //   "location": {
    //     "geoname_id": null,
    //     "capital": "Berlin",
    //     "languages": [
    //       {
    //         "code": "de",
    //         "name": "German",
    //         "native": "Deutsch"
    //       }
    //     ],
    //     "country_flag": "http:\/\/assets.ipstack.com\/flags\/de.svg",
    //     "country_flag_emoji": "\ud83c\udde9\ud83c\uddea",
    //     "country_flag_emoji_unicode": "U+1F1E9 U+1F1EA",
    //     "calling_code": "49",
    //     "is_eu": true
    //   }
    // }

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

    _ = try json.stringOf(alloc, "ip");
    _ = try json.stringOf(alloc, "type");
    _ = try json.stringOf(alloc, "continent_code");
    _ = try json.stringOf(alloc, "continent_name");
    _ = try json.stringOf(alloc, "country_code");
    _ = try json.stringOf(alloc, "country_name");
    _ = try json.stringOf(alloc, "region_code");
    _ = try json.stringOf(alloc, "region_name");
    _ = try json.stringOf(alloc, "city");
    _ = try json.stringOf(alloc, "zip");
    _ = try json.stringOf(alloc, "latitude");
    _ = try json.stringOf(alloc, "longitude");
    _ = try json.stringOf(alloc, "location.geoname_id");
    _ = try json.stringOf(alloc, "location.capital");
    _ = try json.stringOf(alloc, "location.country_flag");
    _ = try json.stringOf(alloc, "location.country_flag_emoji");
    _ = try json.stringOf(alloc, "location.country_flag_emoji_unicode");
    _ = try json.stringOf(alloc, "location.calling_code");
    _ = json.boolOf("location.is_eu");
    i = 0;
    const count_i = json.sizeOfArray("location.languages");
    while (i < count_i) {
        json.setI(i);
        code = try json.stringOf(alloc, "location.languages[i].code");
        name = try json.stringOf(alloc, "location.languages[i].name");
        native = try json.stringOf(alloc, "location.languages[i].native");
        i = i + 1;
    }
}