Sample code for 30+ languages & platforms
Zig

Dropbox: Access Your Own Account

See more Dropbox Examples

To programmatically interact with your own Dropbox account, such as for uploading, downloading, listing files, etc., go to the Dropbox app console and create an application. Then generate an access token in the online app console as shown in the screenshot below. The access token does not expire, and can be used to access your own account from your own non-web application.

The example code, located below the screenshot, shows how to list the files in the root folder.

image

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 rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the www.dropbox.com endpoint.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    rest.connect("api.dropboxapi.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    rest.addHeader("Content-Type", "application/json") catch {};
    rest.addHeader("Authorization", "Bearer DROPBOX-ACCESS-TOKEN") catch {};

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    // The root folder should be an empty string, not "/"
    json.appendString("path", "") catch {};
    json.appendBool("recursive", false) catch {};
    json.appendBool("include_media_info", false) catch {};
    json.appendBool("include_deleted", false) catch {};
    json.appendBool("include_has_explicit_shared_members", false) catch {};

    const response_str = rest.fullRequestString(alloc, "POST", "/2/files/list_folder", try json.emit(alloc)) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Success is indicated by a 200 response status code.
    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body (if any): {s}\n", .{response_str});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        return;
    }

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(response_str) catch {};

    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    // A sample JSON response is shown at the end of this example.
    // The following code iterates over the entries.
    const dt = try chilkat.DtObj.init();
    defer dt.deinit();
    const num_entries = json_response.sizeOfArray("entries");
    var i: i32 = 0;
    while (i < num_entries) {
        json_response.setI(i);
        std.debug.print("----\n", .{});
        std.debug.print("name: {s}\n", .{try json_response.stringOf(alloc, "entries[i].name")});
        std.debug.print("path_lower: {s}\n", .{try json_response.stringOf(alloc, "entries[i].path_lower")});
        std.debug.print("path_display: {s}\n", .{try json_response.stringOf(alloc, "entries[i].path_display")});
        if (json_response.hasMember("entries[i].sharing_info")) {
            std.debug.print("has sharing_info...\n", .{});
            std.debug.print("read_only: {s}\n", .{try json_response.stringOf(alloc, "entries[i].sharing_info.read_only")});
            std.debug.print("shared_folder_id: {s}\n", .{try json_response.stringOf(alloc, "entries[i].sharing_info.shared_folder_id")});
        }

        if (json_response.hasMember("entries[i].client_modified")) {
            // Demonstrate how to parse a date/time:
            const ckdt = try chilkat.DateTime.init();
            defer ckdt.deinit();
            try ckdt.setFromTimestamp(try json_response.stringOf(alloc, "entries[i].client_modified"));
            // The date/time can now be converted to many other formats, or the individual parts
            // can be accessed.
            const b_local_date_time = true;
            ckdt.toDtObj(b_local_date_time, dt);

            std.debug.print("{d}-{d}-{d}\n", .{ dt.getYear(), dt.getMonth(), dt.getDay() });
        }

        i = i + 1;
    }

    std.debug.print("Success.\n", .{});

    // {
    //   "entries": [
    //     {
    //       ".tag": "folder",
    //       "name": "chilkat Team Folder",
    //       "path_lower": "/chilkat team folder",
    //       "path_display": "/chilkat Team Folder",
    //       "id": "id:2VqX9RLr-JAAAAAAAAAAAQ",
    //       "shared_folder_id": "1210954290",
    //       "sharing_info": {
    //         "read_only": false,
    //         "shared_folder_id": "1210954290"
    //       }
    //     },
    //     {
    //       ".tag": "file",
    //       "name": "Get Started with Dropbox.pdf",
    //       "path_lower": "/get started with dropbox.pdf",
    //       "path_display": "/Get Started with Dropbox.pdf",
    //       "id": "id:oxE2oMDqH4AAAAAAAAAAAQ",
    //       "client_modified": "2016-05-07T11:47:47Z",
    //       "server_modified": "2016-05-07T11:47:47Z",
    //       "rev": "1483db13f",
    //       "size": 692088
    //     }
    //   ],
    //   "cursor": "AAEnZXciJyS4gzC_tlX6K2na4c8o7_09-dxmXKHpkPPyf3Kl0H4N-VYnz424nCrFOJuhMiM5_RgNAersumx9qe7NgCSdlppr80iFk0gf6vPlecM6SBtLcs6OYXL8ILWiZ62pfnOvgC3WKGlG6dqZ-VXH",
    //   "has_more": false
    // }
    //
}