Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.4.0+

Finnhub API - Get Stock Quote

See more AI Examples

Demonstrates how to get a stock quote from the Finnhub API.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cApiKey
LOCAL cSymbol
LOCAL oHttp
LOCAL cUrlWithoutParams
LOCAL oReq
LOCAL oResp
LOCAL oJson
LOCAL nStatusCode

nSuccess := 0

//  Replace with your actual Finnhub API key.
cApiKey := "YOUR_FINNHUB_API_KEY"
cSymbol := "AAPL"

oHttp := CreateObject("Chilkat.Http")

//  This is the URL without params.
cUrlWithoutParams := "https://finnhub.io/api/v1/quote"

oReq := CreateObject("Chilkat.HttpRequest")

//  Add params that will be sent in the URL.
oReq:AddParam("symbol", cSymbol)
oReq:AddParam("token", cApiKey)

oReq:HttpVerb := "GET"

//  Send the request to get the JSON response.
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq(cUrlWithoutParams, oReq, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oResp:GetBodyJson(oJson)

nStatusCode := oResp:StatusCode
? "response status code: " + Str(nStatusCode)

oJson:EmitCompact := 0
? oJson:Emit()

//  Sample result:

//  {
//    "c": 248.8,
//    "d": -4.09,
//    "dp": -1.6173,
//    "h": 255.493,
//    "l": 248.07,
//    "o": 253.9,
//    "pc": 252.89,
//    "t": 1774641600
//  }

IF (nStatusCode == 200)
    //  Add the symbol to the top of the result.
    oJson:AddStringAt(0, "symbol", cSymbol)

    //  Rename members for clarification.
    oJson:Rename("c", "currentPrice")
    oJson:Rename("d", "change")
    oJson:Rename("dp", "percentChange")
    oJson:Rename("h", "high")
    oJson:Rename("l", "low")
    oJson:Rename("o", "open")
    oJson:Rename("pc", "prevClose")
    oJson:Rename("t", "unixTime")

    ? oJson:Emit()

ELSE
    ? "Failed"
ENDIF

oHttp:destroy()
oReq:destroy()
oResp:destroy()
oJson:destroy()