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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0

set vault = Server.CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = Server.CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","SchwabTrader")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","personal-desktop")
set tokens = Server.CreateObject("Chilkat.JsonObject")
success = vault.GetSecretJson(entry,tokens)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read the token record. Check the store and complete desktop authorization first.") & "</pre>"
    Response.End
End If

accessToken = tokens.StringOf("access_token")
If (accessToken = "") Then
    Response.Write "<pre>" & Server.HTMLEncode( "Run the Schwab authorization or refresh example to save an access token.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = accessToken

set resp = Server.CreateObject("Chilkat.HttpResponse")
set query = Server.CreateObject("Chilkat.HttpRequest")
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 <> 1) Or (resp.StatusCode <> 200)) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response. Check connectivity.") & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned HTTP " & resp.StatusCode) & "</pre>"
        If (resp.StatusCode = 401) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Refresh the access token or repeat desktop authorization.") & "</pre>"
        End If

    End If

    Response.End
End If

set reply = Server.CreateObject("Chilkat.JsonObject")
success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Schwab returned unexpected JSON.") & "</pre>"
    Response.End
End If

'  Read members by index. A symbol may contain punctuation meaningful in a JSON path.
set instrument = Server.CreateObject("Chilkat.JsonObject")
i = 0
Do While (i < reply.Size)
    key = reply.NameAt(i)
    If (key <> "errors") Then
        success = reply.ObjectAt2(i,instrument)
        If (success = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( key & "  bid " & instrument.StringOf("quote.bidPrice") & "  ask " & instrument.StringOf("quote.askPrice") _
                 & "  last " & instrument.StringOf("quote.lastPrice")) & "</pre>"
            Response.Write "<pre>" & Server.HTMLEncode( "  quote time (Unix ms): " & instrument.StringOf("quote.quoteTime") & "  real-time: " & instrument.StringOf("realtime")) & "</pre>"
        End If

    End If

    i = i + 1
Loop
j = 0
Do While (j < reply.SizeOfArray("errors.invalidSymbols"))
    reply.J = j
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid symbol: " & reply.StringOf("errors.invalidSymbols[j]")) & "</pre>"
    j = j + 1
Loop

%>
</body>
</html>