Sample code for 30+ languages & platforms
Zig

Get a List of Message IDs in GMail User's Mailbox

See more GMail REST API Examples

Demonstrates how to get a list of message IDs in a GMail mailbox. The "userId" can be either the user's email address or the special value "me" to indicate the authenticated user.

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

    http.setAuthToken("ACCESS_TOKEN");

    http.setAccept("application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://www.googleapis.com/gmail/v1/users/userId/messages", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(try resp.getBodyStr(alloc)) catch {};
    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    if (resp.getStatusCode() != 200) {
        std.debug.print("Failed.\n", .{});
        return;
    }

    // {
    //   "messages": [
    //     users.messages Resource
    //   ],
    //   "nextPageToken": string,
    //   "resultSizeEstimate": unsigned integer
    // }

    var id: [:0]const u8 = "";
    var thread_id: [:0]const u8 = "";

    _ = json_response.intOf("resultSizeEstimate");
    var i: i32 = 0;
    const count_i = json_response.sizeOfArray("messages");
    while (i < count_i) {
        json_response.setI(i);
        id = try json_response.stringOf(alloc, "messages[i].id");
        thread_id = try json_response.stringOf(alloc, "messages[i].threadId");
        i = i + 1;
    }
}