Sample code for 30+ languages & platforms
Zig

SMSAPI - Get User Account Information

See more SMSAPI Examples

Get a list of subusers.

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();

    // Implements the following CURL command:

    // curl -i -H "Authorization: Bearer token_api_oauth" \
    // -H  "Content-Type: application/json" -X GET https://api.smsapi.com/subusers

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // Adds the "Authorization: Bearer token_api_oauth" header.
    http.setAuthToken("token_api_oauth");
    http.setRequestHeader("Content-Type", "application/json");

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.smsapi.com/subusers", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const jarr_resp = try chilkat.JsonArray.init();
    defer jarr_resp.deinit();
    jarr_resp.loadSb(sb_response_body) catch {};
    jarr_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try jarr_resp.emit(alloc)});

    const resp_status_code = http.getLastStatus();
    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 http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // [
    //   {
    //     "id": "5A5359173738303F2F95B7E2",
    //     "username": "subuser1",
    //     "active": "true",
    //     "description": "null",
    //     "from_account": "10.0000",
    //     "per_month": "0"
    //   },
    //   {
    //     "id": "5A5359173738303F2F95B7E2",
    //     "username": "subuser2",
    //     "active": "true",
    //     "description": "null",
    //     "from_account": "10.0000",
    //     "per_month": "0"
    //   }
    // ]

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var id: [:0]const u8 = "";
    var username: [:0]const u8 = "";
    var active: [:0]const u8 = "";
    var description: [:0]const u8 = "";
    var from_account: [:0]const u8 = "";
    var per_month: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = jarr_resp.getSize();
    while (i < count_i) {
        const json = try jarr_resp.objectAt(i);
        defer json.deinit();
        id = try json.stringOf(alloc, "id");
        username = try json.stringOf(alloc, "username");
        active = try json.stringOf(alloc, "active");
        description = try json.stringOf(alloc, "description");
        from_account = try json.stringOf(alloc, "from_account");
        per_month = try json.stringOf(alloc, "per_month");

        i = i + 1;
    }
}