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

Visual Basic 6.0
Dim success As Long
success = 0

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

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

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

Dim http As New ChilkatHttp
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

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

    End If

    Exit Sub
End If

Dim accounts As New ChilkatJsonArray
success = accounts.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Schwab returned unexpected JSON."
    Exit Sub
End If

Dim row As New ChilkatJsonObject
Dim number As New ChilkatStringBuilder
Dim matches As Long
matches = 0
Dim i As Long
i = 0
Dim selectedHash As String
selectedHash = ""
Do While (i < accounts.Size)
    success = accounts.ObjectAt2(i,row)
    Dim accountNumber As String
    accountNumber = row.StringOf("accountNumber")
    success = number.SetString(accountNumber)
    If (number.Length >= 4) Then
        Dim start As Long
        start = number.Length - 4
        Dim ending As String
        ending = number.GetRange(start,4,0)
        Debug.Print "Account ending in " & ending
        Dim matched As Long
        matched = 0
        If (selectedFullNumber <> "") Then
            If (accountNumber = selectedFullNumber) Then
                matched = 1
            End If

        Else
            If (ending = selectedEnding) Then
                matched = 1
            End If

        End If

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

    End If

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

success = entry.UpdateString("service","SelectedAccount")
Dim selection As New ChilkatJsonObject
success = selection.UpdateString("hashValue",selectedHash)
success = vault.UpdateSecretJson(entry,selection)
If (success <> 1) Then
    Debug.Print "Could not save the selected account."
    Exit Sub
End If

Debug.Print "Selected account saved."