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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  success = false;

  final vault = CkSecrets();
  vault.location = 'local_manager';
  vault.autoSplit = true;
  final entry = CkJsonObject();
  entry.updateString('appName', 'SchwabTrader');
  entry.updateString('service', 'OAuth2');
  entry.updateString('username', 'personal-desktop');
  final tokens = CkJsonObject();
  try {
    vault.getSecretJson(entry, tokens);
  } on ChilkatException {
    print('Could not read the token record. Check the store and complete desktop authorization first.');
    return;
  }

  final accessToken = tokens.stringOf('access_token');
  if (accessToken == '') {
    print('Run the Schwab authorization or refresh example to save an access token.');
    return;
  }

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.authToken = accessToken;

  // Supply one of these from your account picker. Leave both empty to list only.
  final selectedEnding = '';
  final selectedFullNumber = '';
  final resp = CkHttpResponse();
  success = true;
  try {
    http.httpNoBody('GET', 'https://api.schwabapi.com/trader/v1/accounts/accountNumbers', resp);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (resp.statusCode != 200)) {
    if (resp.statusCode == 0) {
      print('No HTTP response. Check connectivity.');
    } else {
      print('Schwab returned HTTP ${resp.statusCode}');
      if (resp.statusCode == 401) {
        print('Refresh the access token or repeat desktop authorization.');
      }
    }

    return;
  }

  final accounts = CkJsonArray();
  try {
    accounts.load(resp.bodyStr);
  } on ChilkatException {
    print('Schwab returned unexpected JSON.');
    return;
  }

  final row = CkJsonObject();
  final number = CkStringBuilder();
  var matches = 0;
  var i = 0;
  var selectedHash = '';
  while (i < accounts.size) {
    accounts.objectAt2(i, row);
    final accountNumber = row.stringOf('accountNumber');
    number.setString(accountNumber);
    if (number.length >= 4) {
      final start = number.length - 4;
      final ending = number.getRange(start, 4, false);
      print('Account ending in $ending');
      var matched = false;
      if (selectedFullNumber != '') {
        if (accountNumber == selectedFullNumber) {
          matched = true;
        }
      } else {
        if (ending == selectedEnding) {
          matched = true;
        }
      }

      if (matched) {
        matches++;
        selectedHash = row.stringOf('hashValue');
      }
    }

    i++;
  }

  if ((matches != 1) || (selectedHash == '')) {
    print('Select exactly one account. For duplicate endings use the full-account input.');
    return;
  }

  entry.updateString('service', 'SelectedAccount');
  final selection = CkJsonObject();
  selection.updateString('hashValue', selectedHash);
  try {
    vault.updateSecretJson(entry, selection);
  } on ChilkatException {
    print('Could not save the selected account.');
    return;
  }

  print('Selected account saved.');
}