Sample code for 30+ languages & platforms
Zig

WaTrend Create Instance

See more WaTrend Examples

Create a new WaTrend Instance ID.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Use your actual access token instead of 555555555555555555555555555555
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://app.watrend.com/api/createinstance.php?access_token=555555555555555555555555555555", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Both success and failed responses use 200 status code.

    // A success response contains this JSON in the response body:
    // {"status":"success","message":"Instance ID generated successfully","instance_id":"638EE5A76D3AA"}

    // A failed response will contain something like this:
    // {"status":"error","message":"Access token does not exist"}

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

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};

    const status = try j_resp.stringOf(alloc, "status");
    var instance_id: [:0]const u8 = "";
    if (j_resp.hasMember("instance_id")) {
        instance_id = try j_resp.stringOf(alloc, "instance_id");
    }

    std.debug.print("status: {s}\n", .{status});
    std.debug.print("instance_id: {s}\n", .{instance_id});
}