Sample code for 30+ languages & platforms
Unicode 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 Unicode C++ Downloads

Unicode C++
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonArrayW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkSecretsW vault;
    vault.put_Location(L"local_manager");
    vault.put_AutoSplit(true);
    CkJsonObjectW entry;
    entry.UpdateString(L"appName",L"SchwabTrader");
    entry.UpdateString(L"service",L"OAuth2");
    entry.UpdateString(L"username",L"personal-desktop");
    CkJsonObjectW tokens;
    success = vault.GetSecretJson(entry,tokens);
    if (success != true) {
        wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
        return;
    }

    const wchar_t *accessToken = tokens.stringOf(L"access_token");
    if (accessToken == L"") {
        wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(accessToken);

    //  Supply one of these from your account picker. Leave both empty to list only.
    const wchar_t *selectedEnding = L"";
    const wchar_t *selectedFullNumber = L"";
    CkHttpResponseW resp;
    success = http.HttpNoBody(L"GET",L"https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp);
    if ((success != true) || (resp.get_StatusCode() != 200)) {
        if (resp.get_StatusCode() == 0) {
            wprintf(L"No HTTP response. Check connectivity.\n");
        }
        else {
            wprintf(L"Schwab returned HTTP %d\n",resp.get_StatusCode());
            if (resp.get_StatusCode() == 401) {
                wprintf(L"Refresh the access token or repeat desktop authorization.\n");
            }

        }

        return;
    }

    CkJsonArrayW accounts;
    success = accounts.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Schwab returned unexpected JSON.\n");
        return;
    }

    CkJsonObjectW row;
    CkStringBuilderW number;
    int matches = 0;
    int i = 0;
    const wchar_t *selectedHash = L"";
    while ((i < accounts.get_Size())) {
        accounts.ObjectAt2(i,row);
        const wchar_t *accountNumber = row.stringOf(L"accountNumber");
        number.SetString(accountNumber);
        if (number.get_Length() >= 4) {
            int start = number.get_Length() - 4;
            const wchar_t *ending = number.getRange(start,4,false);
            wprintf(L"Account ending in %s\n",ending);
            bool matched = false;
            if (selectedFullNumber != L"") {
                if (accountNumber == selectedFullNumber) {
                    matched = true;
                }

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

            }

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

        }

        i = i + 1;
    }

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

    entry.UpdateString(L"service",L"SelectedAccount");
    CkJsonObjectW selection;
    selection.UpdateString(L"hashValue",selectedHash);
    success = vault.UpdateSecretJson(entry,selection);
    if (success != true) {
        wprintf(L"Could not save the selected account.\n");
        return;
    }

    wprintf(L"Selected account saved.\n");
    }