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

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False

vault = chilkat.CkSecrets()
vault.put_Location("local_manager")
vault.put_AutoSplit(True)
entry = chilkat.CkJsonObject()
entry.UpdateString("appName","SchwabTrader")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","personal-desktop")
tokens = chilkat.CkJsonObject()
success = vault.GetSecretJson(entry,tokens)
if (success != True):
    print("Could not read the token record. Check the store and complete desktop authorization first.")
    sys.exit()

accessToken = tokens.stringOf("access_token")
if (accessToken == ""):
    print("Run the Schwab authorization or refresh example to save an access token.")
    sys.exit()

http = chilkat.CkHttp()
http.put_RequireSslCertVerify(True)
http.put_AuthToken(accessToken)

#  Supply one of these from your account picker. Leave both empty to list only.
selectedEnding = ""
selectedFullNumber = ""
resp = chilkat.CkHttpResponse()
success = http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",resp)
if ((success != True) or (resp.get_StatusCode() != 200)):
    if (resp.get_StatusCode() == 0):
        print("No HTTP response. Check connectivity.")
    else:
        print("Schwab returned HTTP " + str(resp.get_StatusCode()))
        if (resp.get_StatusCode() == 401):
            print("Refresh the access token or repeat desktop authorization.")

    sys.exit()

accounts = chilkat.CkJsonArray()
success = accounts.Load(resp.bodyStr())
if (success != True):
    print("Schwab returned unexpected JSON.")
    sys.exit()

row = chilkat.CkJsonObject()
number = chilkat.CkStringBuilder()
matches = 0
i = 0
selectedHash = ""
while (i < accounts.get_Size()) :
    accounts.ObjectAt2(i,row)
    accountNumber = row.stringOf("accountNumber")
    number.SetString(accountNumber)
    if (number.get_Length() >= 4):
        start = number.get_Length() - 4
        ending = number.getRange(start,4,False)
        print("Account ending in " + ending)
        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) or (selectedHash == "")):
    print("Select exactly one account. For duplicate endings use the full-account input.")
    sys.exit()

entry.UpdateString("service","SelectedAccount")
selection = chilkat.CkJsonObject()
selection.UpdateString("hashValue",selectedHash)
success = vault.UpdateSecretJson(entry,selection)
if (success != True):
    print("Could not save the selected account.")
    sys.exit()

print("Selected account saved.")