Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Schwab: Get quotes for a watchlist

See more Schwab Examples

Requests several symbols in one call and prints the bid, ask, last price, quote timestamp in Unix milliseconds, and the real-time indicator when available. Missing quote fields can occur for individual instruments. The response uses symbol names as JSON keys and can report invalid symbols alongside valid quotes.

Use the access token saved by desktop authorization. Enable the Market Data API for the Schwab app. 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;

  final resp = CkHttpResponse();
  final query = CkHttpRequest();
  query.httpVerb = 'GET';
  query.addParam('symbols', 'AAPL,MSFT,BRK/B');
  query.addParam('fields', 'quote');
  success = true;
  try {
    http.httpReq('https://api.schwabapi.com/marketdata/v1/quotes', query, 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 reply = CkJsonObject();
  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    print('Schwab returned unexpected JSON.');
    return;
  }

  // Read members by index. A symbol may contain punctuation meaningful in a JSON path.
  final instrument = CkJsonObject();
  var i = 0;
  while (i < reply.size) {
    final key = reply.nameAt(i);
    if (key != 'errors') {
      try {
        reply.objectAt2(i, instrument);
        print('$key  bid ${instrument.stringOf('quote.bidPrice')}  ask ${instrument.stringOf('quote.askPrice')}  last ${instrument.stringOf('quote.lastPrice')}');
        print('  quote time (Unix ms): ${instrument.stringOf('quote.quoteTime')}  real-time: ${instrument.stringOf('realtime')}');
      } on ChilkatException {
        // reply.objectAt2() failed; continue anyway.
      }
    }

    i++;
  }

  var j = 0;
  while (j < reply.sizeOfArray('errors.invalidSymbols')) {
    reply.j = j;
    print('Invalid symbol: ${reply.stringOf('errors.invalidSymbols[j]')}');
    j++;
  }
}