Sample code for 30+ languages & platforms
.NET Core C# 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 .NET Core C# Downloads

.NET Core C#
bool success = false;

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

Chilkat.Secrets vault = new Chilkat.Secrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
Chilkat.JsonObject entry = new Chilkat.JsonObject();
entry.UpdateString("appName","SchwabTrader");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","personal-desktop");
Chilkat.JsonObject tokens = new Chilkat.JsonObject();
success = vault.GetSecretJson(entry,tokens);
if (success != true) {
    Debug.WriteLine("Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

string accessToken = tokens.StringOf("access_token");
if (accessToken == "") {
    Debug.WriteLine("Run the Schwab authorization or refresh example to save an access token.");
    return;
}

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;

//  Supply one of these from your account picker. Leave both empty to list only.
string selectedEnding = "";
string selectedFullNumber = "";
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp);
if ((success != true) || (resp.StatusCode != 200)) {
    if (resp.StatusCode == 0) {
        Debug.WriteLine("No HTTP response. Check connectivity.");
    }
    else {
        Debug.WriteLine("Schwab returned HTTP " + Convert.ToString(resp.StatusCode));
        if (resp.StatusCode == 401) {
            Debug.WriteLine("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

Chilkat.JsonArray accounts = new Chilkat.JsonArray();
success = accounts.Load(resp.BodyStr);
if (success != true) {
    Debug.WriteLine("Schwab returned unexpected JSON.");
    return;
}

Chilkat.JsonObject row = new Chilkat.JsonObject();
Chilkat.StringBuilder number = new Chilkat.StringBuilder();
int matches = 0;
int i = 0;
string selectedHash = "";
while ((i < accounts.Size)) {
    accounts.ObjectAt2(i,row);
    string accountNumber = row.StringOf("accountNumber");
    number.SetString(accountNumber);
    if (number.Length >= 4) {
        int start = number.Length - 4;
        string ending = number.GetRange(start,4,false);
        Debug.WriteLine("Account ending in " + ending);
        bool matched = false;
        if (selectedFullNumber != "") {
            if (accountNumber == selectedFullNumber) {
                matched = true;
            }

        }
        else {
            if (ending == selectedEnding) {
                matched = true;
            }

        }

        if (matched == true) {
            matches = matches + 1;
            selectedHash = row.StringOf("hashValue");
        }

    }

    i = i + 1;
}

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

entry.UpdateString("service","SelectedAccount");
Chilkat.JsonObject selection = new Chilkat.JsonObject();
selection.UpdateString("hashValue",selectedHash);
success = vault.UpdateSecretJson(entry,selection);
if (success != true) {
    Debug.WriteLine("Could not save the selected account.");
    return;
}

Debug.WriteLine("Selected account saved.");