Sample code for 30+ languages & platforms
Zig

ip-api.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ip-api.com 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://ip-api.com/json/149.250.207.170") 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

    // {
    //   "as": "AS15854 Hewlett Packard GmbH",
    //   "city": "B�blingen",
    //   "country": "Germany",
    //   "countryCode": "DE",
    //   "isp": "Triaton Frankfurt",
    //   "lat": 48.6779,
    //   "lon": 8.97297,
    //   "org": "EntServ Deutschland GmbH",
    //   "query": "149.250.207.170",
    //   "region": "BW",
    //   "regionName": "Baden-W�rttemberg",
    //   "status": "success",
    //   "timezone": "Europe/Berlin",
    //   "zip": "71034"
    // }

    _ = try json.stringOf(alloc, "as");
    _ = try json.stringOf(alloc, "city");
    _ = try json.stringOf(alloc, "country");
    _ = try json.stringOf(alloc, "countryCode");
    _ = try json.stringOf(alloc, "isp");
    _ = try json.stringOf(alloc, "lat");
    _ = try json.stringOf(alloc, "lon");
    _ = try json.stringOf(alloc, "org");
    _ = try json.stringOf(alloc, "query");
    _ = try json.stringOf(alloc, "region");
    _ = try json.stringOf(alloc, "regionName");
    _ = try json.stringOf(alloc, "status");
    _ = try json.stringOf(alloc, "timezone");
    _ = try json.stringOf(alloc, "zip");
}