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

VB.NET
Dim success As Boolean = False

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

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

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

Dim http As New Chilkat.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 New Chilkat.HttpResponse
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
        Debug.WriteLine("No HTTP response. Check connectivity.")
    Else
        Debug.WriteLine("Schwab returned HTTP " & resp.StatusCode)
        If (resp.StatusCode = 401) Then
            Debug.WriteLine("Refresh the access token or repeat desktop authorization.")
        End If

    End If

    Exit Sub
End If

Dim accounts As New Chilkat.JsonArray
success = accounts.Load(resp.BodyStr)
If (success <> True) Then
    Debug.WriteLine("Schwab returned unexpected JSON.")
    Exit Sub
End If


Dim row As New Chilkat.JsonObject
Dim number As New Chilkat.StringBuilder
Dim matches As Integer = 0
Dim i As Integer = 0
Dim selectedHash As String = ""
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 Integer = number.Length - 4
        Dim ending As String = number.GetRange(start,4,False)
        Debug.WriteLine("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
End While
If ((matches <> 1) Or (selectedHash = "")) Then
    Debug.WriteLine("Select exactly one account. For duplicate endings use the full-account input.")
    Exit Sub
End If

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

Debug.WriteLine("Selected account saved.")