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

Unicode C
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkHttpRequestW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSecretsW vault;
    HCkJsonObjectW entry;
    HCkJsonObjectW tokens;
    const wchar_t *accessToken;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkHttpRequestW query;
    HCkJsonObjectW reply;
    HCkJsonObjectW instrument;
    int i;
    const wchar_t *key;
    int j;

    success = FALSE;

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

    vault = CkSecretsW_Create();
    CkSecretsW_putLocation(vault,L"local_manager");
    CkSecretsW_putAutoSplit(vault,TRUE);
    entry = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(entry,L"appName",L"SchwabTrader");
    CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
    CkJsonObjectW_UpdateString(entry,L"username",L"personal-desktop");
    tokens = CkJsonObjectW_Create();
    success = CkSecretsW_GetSecretJson(vault,entry,tokens);
    if (success != TRUE) {
        wprintf(L"Could not read the token record. Check the store and complete desktop authorization first.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        return;
    }

    accessToken = CkJsonObjectW_stringOf(tokens,L"access_token");
    if (accessToken == L"") {
        wprintf(L"Run the Schwab authorization or refresh example to save an access token.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        return;
    }

    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putAuthToken(http,accessToken);

    resp = CkHttpResponseW_Create();
    query = CkHttpRequestW_Create();
    CkHttpRequestW_putHttpVerb(query,L"GET");
    CkHttpRequestW_AddParam(query,L"symbols",L"AAPL,MSFT,BRK/B");
    CkHttpRequestW_AddParam(query,L"fields",L"quote");
    success = CkHttpW_HttpReq(http,L"https://api.schwabapi.com/marketdata/v1/quotes",query,resp);
    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 200)) {
        if (CkHttpResponseW_getStatusCode(resp) == 0) {
            wprintf(L"No HTTP response. Check connectivity.\n");
        }
        else {
            wprintf(L"Schwab returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
            if (CkHttpResponseW_getStatusCode(resp) == 401) {
                wprintf(L"Refresh the access token or repeat desktop authorization.\n");
            }

        }

        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkHttpRequestW_Dispose(query);
        return;
    }

    reply = CkJsonObjectW_Create();
    success = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
    if (success != TRUE) {
        wprintf(L"Schwab returned unexpected JSON.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkHttpRequestW_Dispose(query);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    //  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
    instrument = CkJsonObjectW_Create();
    i = 0;
    while ((i < CkJsonObjectW_getSize(reply))) {
        key = CkJsonObjectW_nameAt(reply,i);
        if (key != L"errors") {
            success = CkJsonObjectW_ObjectAt2(reply,i,instrument);
            if (success == TRUE) {
                wprintf(L"%s  bid %s  ask %s  last %s\n",key,CkJsonObjectW_stringOf(instrument,L"quote.bidPrice"),CkJsonObjectW_stringOf(instrument,L"quote.askPrice")
                    ,CkJsonObjectW_stringOf(instrument,L"quote.lastPrice"));
                wprintf(L"  quote time (Unix ms): %s  real-time: %s\n",CkJsonObjectW_stringOf(instrument,L"quote.quoteTime"),CkJsonObjectW_stringOf(instrument,L"realtime"));
            }

        }

        i = i + 1;
    }

    j = 0;
    while ((j < CkJsonObjectW_SizeOfArray(reply,L"errors.invalidSymbols"))) {
        CkJsonObjectW_putJ(reply,j);
        wprintf(L"Invalid symbol: %s\n",CkJsonObjectW_stringOf(reply,L"errors.invalidSymbols[j]"));
        j = j + 1;
    }



    CkSecretsW_Dispose(vault);
    CkJsonObjectW_Dispose(entry);
    CkJsonObjectW_Dispose(tokens);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkHttpRequestW_Dispose(query);
    CkJsonObjectW_Dispose(reply);
    CkJsonObjectW_Dispose(instrument);

    }