Sample code for 30+ languages & platforms
DataFlex

Finnhub API - Get Stock Quote

See more AI Examples

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    String sApiKey
    String sSymbol
    Handle hoHttp
    String sUrlWithoutParams
    Variant vReq
    Handle hoReq
    Variant vResp
    Handle hoResp
    Variant vJson
    Handle hoJson
    Integer iStatusCode
    String sTemp1

    Move False To iSuccess

    // Replace with your actual Finnhub API key.
    Move "YOUR_FINNHUB_API_KEY" To sApiKey
    Move "AAPL" To sSymbol

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // This is the URL without params.
    Move "https://finnhub.io/api/v1/quote" To sUrlWithoutParams

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End

    // Add params that will be sent in the URL.
    Send ComAddParam To hoReq "symbol" sSymbol
    Send ComAddParam To hoReq "token" sApiKey

    Set ComHttpVerb Of hoReq To "GET"

    // Send the request to get the JSON response.
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoReq to vReq
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp sUrlWithoutParams vReq vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get pvComObject of hoJson to vJson
    Get ComGetBodyJson Of hoResp vJson To iSuccess

    Get ComStatusCode Of hoResp To iStatusCode
    Showln "response status code: " iStatusCode

    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // 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 (iStatusCode = 200) Begin
        // Add the symbol to the top of the result.
        Get ComAddStringAt Of hoJson 0 "symbol" sSymbol To iSuccess

        // Rename members for clarification.
        Get ComRename Of hoJson "c" "currentPrice" To iSuccess
        Get ComRename Of hoJson "d" "change" To iSuccess
        Get ComRename Of hoJson "dp" "percentChange" To iSuccess
        Get ComRename Of hoJson "h" "high" To iSuccess
        Get ComRename Of hoJson "l" "low" To iSuccess
        Get ComRename Of hoJson "o" "open" To iSuccess
        Get ComRename Of hoJson "pc" "prevClose" To iSuccess
        Get ComRename Of hoJson "t" "unixTime" To iSuccess

        Get ComEmit Of hoJson To sTemp1
        Showln sTemp1

    End
    Else Begin
        Showln "Failed"
    End



End_Procedure