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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

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

set vault = CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
set tokens = CreateObject("Chilkat.JsonObject")
success = vault.GetSecretJson(entry,tokens)
If (success <> 1) Then
    outFile.WriteLine("Could not read the token record. Check the store and complete desktop authorization first.")
    WScript.Quit
End If

accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
    outFile.WriteLine("Run the Schwab authorization or refresh example to save an access token.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

'  Supply one of these from your account picker. Leave both empty to list only.
selectedEnding = ""
selectedFullNumber = ""
set resp = CreateObject("Chilkat.HttpResponse")
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
        outFile.WriteLine("No HTTP response. Check connectivity.")
    Else
        outFile.WriteLine("Schwab returned HTTP " & resp.StatusCode)
        If (resp.StatusCode = 401) Then
            outFile.WriteLine("Refresh the access token or repeat desktop authorization.")
        End If

    End If

    WScript.Quit
End If

set accounts = CreateObject("Chilkat.JsonArray")
success = accounts.Load(resp.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Schwab returned unexpected JSON.")
    WScript.Quit
End If

set row = CreateObject("Chilkat.JsonObject")
set number = CreateObject("Chilkat.StringBuilder")
matches = 0
i = 0
selectedHash = ""
Do While (i < accounts.Size)
    success = accounts.ObjectAt2(i,row)
    accountNumber = row.StringOf("accountNumber")
    success = number.SetString(accountNumber)
    If (number.Length >= 4) Then
        start = number.Length - 4
        ending = number.GetRange(start,4,0)
        outFile.WriteLine("Account ending in " & ending)
        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
    outFile.WriteLine("Select exactly one account. For duplicate endings use the full-account input.")
    WScript.Quit
End If

success = entry.UpdateString("service","SelectedAccount")
set selection = CreateObject("Chilkat.JsonObject")
success = selection.UpdateString("hashValue",selectedHash)
success = vault.UpdateSecretJson(entry,selection)
If (success <> 1) Then
    outFile.WriteLine("Could not save the selected account.")
    WScript.Quit
End If

outFile.WriteLine("Selected account saved.")

outFile.Close