Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Schwab: Export account transactions to CSV

See more Schwab Examples

Exports the last seven days of trades, dividends, and interest for one account. Each UTF-8 CSV row represents an activity, including its signed net amount. The file is replaced on each run. For incremental imports, key records by account and activityId so overlapping date windows do not duplicate entries. If a window returns the 3,000-transaction maximum, split it before treating it as complete.

Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

Chilkat Rust Downloads

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;

let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "SchwabTrader");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "personal-desktop");
let tokens = chilkat::JsonObject::new();
success = vault.get_secret_json(&entry, &tokens).is_ok();
if !success {
    println!("Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

let access_token = tokens.string_of("access_token").unwrap_or_default();
if access_token == "" {
    println!("Run the Schwab authorization or refresh example to save an access token.");
    return;
}

let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&access_token);

// Read the hash saved by the authorized-account selection example.
let _ = entry.update_string("service", "SelectedAccount");
let selection = chilkat::JsonObject::new();
success = vault.get_secret_json(&entry, &selection).is_ok();
let account_hash = selection.string_of("hashValue").unwrap_or_default();
if (!success) || (account_hash == "") {
    println!("Select an authorized account first.");
    return;
}

let _ = http.set_url_var("account", &account_hash);

// Use UTC endpoints for a rolling seven-day window.
let clock = chilkat::DateTime::new();
let _ = clock.set_from_current_system_time();
let end_time = chilkat::StringBuilder::new();
let _ = end_time.append(&clock.get_as_timestamp(false).unwrap_or_default());
let mut replacements = end_time.replace("Z", ".000Z");
let _ = clock.add_days(-7);
let start_time = chilkat::StringBuilder::new();
let _ = start_time.append(&clock.get_as_timestamp(false).unwrap_or_default());
replacements = start_time.replace("Z", ".000Z");
let resp = chilkat::HttpResponse::new();
let query = chilkat::HttpRequest::new();
query.set_http_verb("GET");
query.add_param("startDate", &start_time.get_as_string().unwrap_or_default());
query.add_param("endDate", &end_time.get_as_string().unwrap_or_default());
query.add_param("types", "TRADE,DIVIDEND_OR_INTEREST");
success = http.http_req("https://api.schwabapi.com/trader/v1/accounts/{$account}/transactions", &query, &resp).is_ok();
if (!success) || (resp.status_code() != 200) {
    if resp.status_code() == 0 {
        println!("No HTTP response. Check connectivity.");
    } else {
        println!("Schwab returned HTTP {}", resp.status_code());
        if resp.status_code() == 401 {
            println!("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

let activity = chilkat::JsonArray::new();
if activity.load(&resp.body_str()).is_err() {
    println!("Schwab returned unexpected JSON.");
    return;
}

if activity.size() >= 3000 {
    println!("The response reached the transaction limit. Export smaller time windows.");
    return;
}

let csv = chilkat::Csv::new();
let _ = csv.set_column_name(0, "activityId");
let _ = csv.set_column_name(1, "time");
let _ = csv.set_column_name(2, "type");
let _ = csv.set_column_name(3, "tradeDate");
let _ = csv.set_column_name(4, "settlementDate");
let _ = csv.set_column_name(5, "netAmount");
let row = chilkat::JsonObject::new();
let mut i = 0;
while i < activity.size() {
    let _ = activity.object_at2(i, &row);
    // Keep identifiers and decimal amounts as returned by Schwab.
    let _ = csv.set_cell(i, 0, &row.string_of("activityId").unwrap_or_default());
    let _ = csv.set_cell(i, 1, &row.string_of("time").unwrap_or_default());
    let _ = csv.set_cell(i, 2, &row.string_of("type").unwrap_or_default());
    let _ = csv.set_cell(i, 3, &row.string_of("tradeDate").unwrap_or_default());
    let _ = csv.set_cell(i, 4, &row.string_of("settlementDate").unwrap_or_default());
    let _ = csv.set_cell(i, 5, &row.string_of("netAmount").unwrap_or_default());
    i = i + 1;
}

if csv.save_file2("schwab-transactions.csv", "utf-8").is_err() {
    println!("Could not write schwab-transactions.csv.");
    return;
}

println!("Wrote {} rows to schwab-transactions.csv.", csv.num_rows());