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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false

local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "SchwabTrader")
entry:update_string("service", "OAuth2")
entry:update_string("username", "personal-desktop")
local tokens = chilkat.JsonObject.new()
success = vault:get_secret_json(entry, tokens)
if not success then
    print("Could not read the token record. Check the store and complete desktop authorization first.")
    return
end

local accessToken = tokens:string_of("access_token")
if accessToken == "" then
    print("Run the Schwab authorization or refresh example to save an access token.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = accessToken

-- Supply one of these from your account picker. Leave both empty to list only.
local selectedEnding = ""
local selectedFullNumber = ""
local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", "https://api.schwabapi.com/trader/v1/accounts/accountNumbers", resp)
if (not success) or (resp.status_code ~= 200) then
    if resp.status_code == 0 then
        print("No HTTP response. Check connectivity.")
    else
        print("Schwab returned HTTP " .. resp.status_code)
        if resp.status_code == 401 then
            print("Refresh the access token or repeat desktop authorization.")
        end
    end

    return
end

local accounts = chilkat.JsonArray.new()
success = accounts:load(resp.body_str)
if not success then
    print("Schwab returned unexpected JSON.")
    return
end

local row = chilkat.JsonObject.new()
local number = chilkat.StringBuilder.new()
local matches = 0
local i = 0
local selectedHash = ""
while i < accounts.size do
    accounts:object_at2(i, row)
    local accountNumber = row:string_of("accountNumber")
    number:set_string(accountNumber)
    if number.length >= 4 then
        local start = number.length - 4
        local ending = number:get_range(start, 4, false)
        print("Account ending in " .. ending)
        local matched = false
        if selectedFullNumber ~= "" then
            if accountNumber == selectedFullNumber then
                matched = true
            end
        elseif ending == selectedEnding then
            matched = true
        end

        if matched then
            matches = matches + 1
            selectedHash = row:string_of("hashValue")
        end
    end

    i = i + 1
end

if (matches ~= 1) or (selectedHash == "") then
    print("Select exactly one account. For duplicate endings use the full-account input.")
    return
end

entry:update_string("service", "SelectedAccount")
local selection = chilkat.JsonObject.new()
selection:update_string("hashValue", selectedHash)
success = vault:update_secret_json(entry, selection)
if not success then
    print("Could not save the selected account.")
    return
end

print("Selected account saved.")