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

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lcAccessToken
LOCAL loHttp
LOCAL lcSelectedEnding
LOCAL lcSelectedFullNumber
LOCAL loResp
LOCAL loAccounts
LOCAL loRow
LOCAL loNumber
LOCAL lnMatches
LOCAL i
LOCAL lcSelectedHash
LOCAL lcAccountNumber
LOCAL lnStart
LOCAL lcEnding
LOCAL lnMatched
LOCAL loSelection

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","SchwabTrader")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","personal-desktop")
loTokens = CreateObject('Chilkat.JsonObject')
lnSuccess = loVault.GetSecretJson(loEntry,loTokens)
IF (lnSuccess <> 1) THEN
    ? "Could not read the token record. Check the store and complete desktop authorization first."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

lcAccessToken = loTokens.StringOf("access_token")
IF (lcAccessToken = "") THEN
    ? "Run the Schwab authorization or refresh example to save an access token."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = lcAccessToken

*  Supply one of these from your account picker. Leave both empty to list only.
lcSelectedEnding = ""
lcSelectedFullNumber = ""
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",loResp)
IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
    IF (loResp.StatusCode = 0) THEN
        ? "No HTTP response. Check connectivity."
    ELSE
        ? "Schwab returned HTTP " + STR(loResp.StatusCode)
        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the access token or repeat desktop authorization."
        ENDIF

    ENDIF

    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    CANCEL
ENDIF

loAccounts = CreateObject('Chilkat.JsonArray')
lnSuccess = loAccounts.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Schwab returned unexpected JSON."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loAccounts
    CANCEL
ENDIF

loRow = CreateObject('Chilkat.JsonObject')
loNumber = CreateObject('Chilkat.StringBuilder')
lnMatches = 0
i = 0
lcSelectedHash = ""
DO WHILE (i < loAccounts.Size)
    loAccounts.ObjectAt2(i,loRow)
    lcAccountNumber = loRow.StringOf("accountNumber")
    loNumber.SetString(lcAccountNumber)
    IF (loNumber.Length >= 4) THEN
        lnStart = loNumber.Length - 4
        lcEnding = loNumber.GetRange(lnStart,4,0)
        ? "Account ending in " + lcEnding
        lnMatched = 0
        IF (lcSelectedFullNumber <> "") THEN
            IF (lcAccountNumber = lcSelectedFullNumber) THEN
                lnMatched = 1
            ENDIF

        ELSE
            IF (lcEnding = lcSelectedEnding) THEN
                lnMatched = 1
            ENDIF

        ENDIF

        IF (lnMatched = 1) THEN
            lnMatches = lnMatches + 1
            lcSelectedHash = loRow.StringOf("hashValue")
        ENDIF

    ENDIF

    i = i + 1
ENDDO
IF ((lnMatches <> 1) OR (lcSelectedHash = "")) THEN
    ? "Select exactly one account. For duplicate endings use the full-account input."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loAccounts
    RELEASE loRow
    RELEASE loNumber
    CANCEL
ENDIF

loEntry.UpdateString("service","SelectedAccount")
loSelection = CreateObject('Chilkat.JsonObject')
loSelection.UpdateString("hashValue",lcSelectedHash)
lnSuccess = loVault.UpdateSecretJson(loEntry,loSelection)
IF (lnSuccess <> 1) THEN
    ? "Could not save the selected account."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loResp
    RELEASE loAccounts
    RELEASE loRow
    RELEASE loNumber
    RELEASE loSelection
    CANCEL
ENDIF

? "Selected account saved."

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loResp
RELEASE loAccounts
RELEASE loRow
RELEASE loNumber
RELEASE loSelection