Sample code for 30+ languages & platforms
Visual FoxPro

Finnhub API - Get Stock Quote

See more AI Examples

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

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lcApiKey
LOCAL lcSymbol
LOCAL loHttp
LOCAL lcUrlWithoutParams
LOCAL loReq
LOCAL loResp
LOCAL loJson
LOCAL lnStatusCode

lnSuccess = 0

* Replace with your actual Finnhub API key.
lcApiKey = "YOUR_FINNHUB_API_KEY"
lcSymbol = "AAPL"

loHttp = CreateObject('Chilkat.Http')

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

loReq = CreateObject('Chilkat.HttpRequest')

* Add params that will be sent in the URL.
loReq.AddParam("symbol",lcSymbol)
loReq.AddParam("token",lcApiKey)

loReq.HttpVerb = "GET"

* Send the request to get the JSON response.
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpReq(lcUrlWithoutParams,loReq,loResp)
IF (lnSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loReq
    RELEASE loResp
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
loResp.GetBodyJson(loJson)

lnStatusCode = loResp.StatusCode
? "response status code: " + STR(lnStatusCode)

loJson.EmitCompact = 0
? loJson.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 (lnStatusCode = 200) THEN
    * Add the symbol to the top of the result.
    loJson.AddStringAt(0,"symbol",lcSymbol)

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

    ? loJson.Emit()

ELSE
    ? "Failed"
ENDIF

RELEASE loHttp
RELEASE loReq
RELEASE loResp
RELEASE loJson