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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var vault = new CkSecrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
var entry = new CkJsonObject();
entry.UpdateString("appName","SchwabTrader");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","personal-desktop");
var tokens = new CkJsonObject();
success = vault.GetSecretJson(entry,tokens);
if (success !== true) {
    console.log("Could not read the token record. Check the store and complete desktop authorization first.");
    return;
}

var accessToken = tokens.StringOf("access_token");
if (accessToken == "") {
    console.log("Run the Schwab authorization or refresh example to save an access token.");
    return;
}

var http = new CkHttp();
http.RequireSslCertVerify = true;
http.AuthToken = accessToken;

var resp = new CkHttpResponse();
var query = new CkHttpRequest();
query.HttpVerb = "GET";
query.AddParam("symbols","AAPL,MSFT,BRK/B");
query.AddParam("fields","quote");
success = http.HttpReq("https://api.schwabapi.com/marketdata/v1/quotes",query,resp);
if ((success !== true) || (resp.StatusCode !== 200)) {
    if (resp.StatusCode == 0) {
        console.log("No HTTP response. Check connectivity.");
    }
    else {
        console.log("Schwab returned HTTP " + resp.StatusCode);
        if (resp.StatusCode == 401) {
            console.log("Refresh the access token or repeat desktop authorization.");
        }

    }

    return;
}

var reply = new CkJsonObject();
success = reply.Load(resp.BodyStr);
if (success !== true) {
    console.log("Schwab returned unexpected JSON.");
    return;
}

//  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
var instrument = new CkJsonObject();
var i = 0;
while ((i < reply.Size)) {
    var key = reply.NameAt(i);
    if (key !== "errors") {
        success = reply.ObjectAt2(i,instrument);
        if (success == true) {
            console.log(key + "  bid " + instrument.StringOf("quote.bidPrice") + "  ask " + instrument.StringOf("quote.askPrice") + "  last " + instrument.StringOf("quote.lastPrice"));
            console.log("  quote time (Unix ms): " + instrument.StringOf("quote.quoteTime") + "  real-time: " + instrument.StringOf("realtime"));
        }

    }

    i = i+1;
}

var j = 0;
while ((j < reply.SizeOfArray("errors.invalidSymbols"))) {
    reply.J = j;
    console.log("Invalid symbol: " + reply.StringOf("errors.invalidSymbols[j]"));
    j = j+1;
}