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

Schwab: List authorized accounts and select one

See more Schwab Examples

Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.

Use the access token saved by desktop authorization. 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);

// Supply one of these from your account picker. Leave both empty to list only.
let selected_ending = String::new();
let selected_full_number = String::new();
let resp = chilkat::HttpResponse::new();
success = http.http_no_body("GET", "https://api.schwabapi.com/trader/v1/accounts/accountNumbers", &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 accounts = chilkat::JsonArray::new();
if accounts.load(&resp.body_str()).is_err() {
    println!("Schwab returned unexpected JSON.");
    return;
}

let row = chilkat::JsonObject::new();
let number = chilkat::StringBuilder::new();
let mut matches = 0;
let mut i = 0;
let mut selected_hash = String::new();
while i < accounts.size() {
    let _ = accounts.object_at2(i, &row);
    let account_number = row.string_of("accountNumber").unwrap_or_default();
    let _ = number.set_string(&account_number);
    if number.length() >= 4 {
        let start = number.length() - 4;
        let ending = number.get_range(start, 4, false).unwrap_or_default();
        println!("Account ending in {}", ending);
        let mut matched = false;
        if selected_full_number != "" {
            if account_number == selected_full_number {
                matched = true;
            }

        } else {
            if ending == selected_ending {
                matched = true;
            }

        }

        if matched {
            matches = matches + 1;
            selected_hash = row.string_of("hashValue").unwrap_or_default();
        }

    }

    i = i + 1;
}

if (matches != 1) || (selected_hash == "") {
    println!("Select exactly one account. For duplicate endings use the full-account input.");
    return;
}

let _ = entry.update_string("service", "SelectedAccount");
let selection = chilkat::JsonObject::new();
let _ = selection.update_string("hashValue", &selected_hash);
if vault.update_secret_json(&entry, &selection).is_err() {
    println!("Could not save the selected account.");
    return;
}

println!("Selected account saved.");