Sample code for 30+ languages & platforms
Zig

ETrade List Accounts

See more ETrade Examples

Returns a list of E*TRADE accounts for the current 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 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.setOAuth1(true);
    http.setOAuthVerifier("");
    http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
    http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 Authorization
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/etrade.json") catch {
        std.debug.print("Failed to load OAuth1 token\n", .{});
        return;
    };

    http.setOAuthToken(try json_token.stringOf(alloc, "oauth_token"));
    http.setOAuthTokenSecret(try json_token.stringOf(alloc, "oauth_token_secret"));

    const sandbox_url = "https://apisb.etrade.com/v1/accounts/list";

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", sandbox_url, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Make sure a successful response was received.
    if (resp.getStatusCode() >= 300) {
        std.debug.print("{s}\n", .{try resp.getStatusLine(alloc)});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
        return;
    }

    if (resp.getStatusCode() == 204) {
        std.debug.print("No records available.\n", .{});
        return;
    }

    // Sample XML response:

    // Use this online tool to generate parsing code from sample XML:
    // Generate Parsing Code from XML

    // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    // <AccountListResponse>
    //     <Accounts>
    //         <Account>
    //             <accountId>82314598</accountId>
    //             <accountIdKey>dBZOKt9xDrtRSAOl4MSiiA</accountIdKey>
    //             <accountMode>IRA</accountMode>
    //             <accountDesc>Brokerage</accountDesc>
    //             <accountName>NickName-1</accountName>
    //             <accountType>MARGIN</accountType>
    //             <institutionType>BROKERAGE</institutionType>
    //             <accountStatus>ACTIVE</accountStatus>
    //             <closedDate>0</closedDate>
    //         </Account>
    //         <Account>
    //             <accountId>58315636</accountId>
    //             <accountIdKey>vQMsebA1H5WltUfDkJP48g</accountIdKey>
    //             <accountMode>BROKERAGE</accountMode>
    //             <accountDesc>Complete Savings</accountDesc>
    //             <accountName>NickName-2</accountName>
    //             <accountType>INDIVIDUAL</accountType>
    //             <institutionType>BROKERAGE</institutionType>
    //             <accountStatus>ACTIVE</accountStatus>
    //             <closedDate>0</closedDate>
    //         </Account>
    //         <Account>
    //             <accountId>70700418</accountId>
    //             <accountIdKey>6_Dpy0rmuQ9cu9IbTfvF2A</accountIdKey>
    //             <accountMode>CASH</accountMode>
    //             <accountDesc>INDIVIDUAL</accountDesc>
    //             <accountName>NickName-3</accountName>
    //             <accountType>INDIVIDUAL</accountType>
    //             <institutionType>BROKERAGE</institutionType>
    //             <accountStatus>ACTIVE</accountStatus>
    //             <closedDate>0</closedDate>
    //         </Account>
    //         <Account>
    //             <accountId>83515143</accountId>
    //             <accountIdKey>xj1Dc18FTqWPqkEEVUr5rw</accountIdKey>
    //             <accountMode>CASH</accountMode>
    //             <accountDesc>INDIVIDUAL</accountDesc>
    //             <accountName/>
    //             <accountType>CASH</accountType>
    //             <institutionType>BROKERAGE</institutionType>
    //             <accountStatus>CLOSED</accountStatus>
    //             <closedDate>1521027780</closedDate>
    //
    //         </Account>
    //     </Accounts>
    // </AccountListResponse>

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(try resp.getBodyStr(alloc)) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    var account_id: i32 = 0;
    var account_id_key: [:0]const u8 = "";
    var account_mode: [:0]const u8 = "";
    var account_desc: [:0]const u8 = "";
    var account_name: [:0]const u8 = "";
    var account_type: [:0]const u8 = "";
    var institution_type: [:0]const u8 = "";
    var account_status: [:0]const u8 = "";
    var closed_date: i32 = 0;

    var i: i32 = 0;
    const count_i = xml.numChildrenHavingTag("Accounts|Account");
    while (i < count_i) {
        xml.setI(i);
        account_id = xml.getChildIntValue("Accounts|Account[i]|accountId");
        account_id_key = try xml.getChildContent(alloc, "Accounts|Account[i]|accountIdKey");
        account_mode = try xml.getChildContent(alloc, "Accounts|Account[i]|accountMode");
        account_desc = try xml.getChildContent(alloc, "Accounts|Account[i]|accountDesc");
        account_name = try xml.getChildContent(alloc, "Accounts|Account[i]|accountName");
        account_type = try xml.getChildContent(alloc, "Accounts|Account[i]|accountType");
        institution_type = try xml.getChildContent(alloc, "Accounts|Account[i]|institutionType");
        account_status = try xml.getChildContent(alloc, "Accounts|Account[i]|accountStatus");
        closed_date = xml.getChildIntValue("Accounts|Account[i]|closedDate");
        i = i + 1;
    }

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