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

B4X
Dim success As Boolean = False

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

Dim vault As ChilkatSecrets
vault.Initialize("vault")
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As ChilkatJsonObject
entry.Initialize
entry.UpdateString("appName", "SchwabTrader")
entry.UpdateString("service", "OAuth2")
entry.UpdateString("username", "personal-desktop")
Dim tokens As ChilkatJsonObject
tokens.Initialize
success = vault.GetSecretJson(entry, tokens)
If success <> True Then
    Log("Could not read the token record. Check the store and complete desktop authorization first.")
    Return
End If

Dim accessToken As String = tokens.StringOf("access_token")
If accessToken = "" Then
    Log("Run the Schwab authorization or refresh example to save an access token.")
    Return
End If

Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.AuthToken = accessToken

'  Supply one of these from your account picker. Leave both empty to list only.
Dim selectedEnding As String = ""
Dim selectedFullNumber As String = ""
Dim resp As ChilkatHttpResponse
resp.Initialize
success = http.HttpNoBody("GET", "https://api.schwabapi.com/trader/v1/accounts/accountNumbers", resp)
If (success <> True) Or (resp.StatusCode <> 200) Then
    If resp.StatusCode = 0 Then
        Log("No HTTP response. Check connectivity.")
    Else
        Log("Schwab returned HTTP " & resp.StatusCode)
        If resp.StatusCode = 401 Then
            Log("Refresh the access token or repeat desktop authorization.")
        End If

    End If

    Return
End If

Dim accounts As ChilkatJsonArray
accounts.Initialize
success = accounts.Load(resp.BodyStr)
If success <> True Then
    Log("Schwab returned unexpected JSON.")
    Return
End If


Dim row As ChilkatJsonObject
row.Initialize
Dim number As ChilkatStringBuilder
number.Initialize
Dim matches As Int = 0
Dim i As Int = 0
Dim selectedHash As String = ""
Do While (i < accounts.Size)
    accounts.ObjectAt2(i, row)
    Dim accountNumber As String = row.StringOf("accountNumber")
    number.SetString(accountNumber)
    If number.Length >= 4 Then
        Dim start As Int = number.Length - 4
        Dim ending As String = number.GetRange(start, 4, False)
        Log("Account ending in " & ending)
        Dim matched As Boolean = False
        If selectedFullNumber <> "" Then
            If accountNumber = selectedFullNumber Then
                matched = True
            End If

        Else
            If ending = selectedEnding Then
                matched = True
            End If

        End If

        If matched = True Then
            matches = matches + 1
            selectedHash = row.StringOf("hashValue")
        End If

    End If

    i = i + 1
Loop
If (matches <> 1) Or (selectedHash = "") Then
    Log("Select exactly one account. For duplicate endings use the full-account input.")
    Return
End If

entry.UpdateString("service", "SelectedAccount")
Dim selection As ChilkatJsonObject
selection.Initialize
selection.UpdateString("hashValue", selectedHash)
success = vault.UpdateSecretJson(entry, selection)
If success <> True Then
    Log("Could not save the selected account.")
    Return
End If

Log("Selected account saved.")