Tcl
Tcl
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Replace with your actual Finnhub API key.
set apiKey "YOUR_FINNHUB_API_KEY"
set symbol "AAPL"
set http [new_CkHttp]
# This is the URL without params.
set urlWithoutParams "https://finnhub.io/api/v1/quote"
set req [new_CkHttpRequest]
# Add params that will be sent in the URL.
CkHttpRequest_AddParam $req "symbol" $symbol
CkHttpRequest_AddParam $req "token" $apiKey
CkHttpRequest_put_HttpVerb $req "GET"
# Send the request to get the JSON response.
set resp [new_CkHttpResponse]
set success [CkHttp_HttpReq $http $urlWithoutParams $req $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
exit
}
set json [new_CkJsonObject]
CkHttpResponse_GetBodyJson $resp $json
set statusCode [CkHttpResponse_get_StatusCode $resp]
puts "response status code: $statusCode"
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]
# 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 {$statusCode == 200} then {
# Add the symbol to the top of the result.
CkJsonObject_AddStringAt $json 0 "symbol" $symbol
# Rename members for clarification.
CkJsonObject_Rename $json "c" "currentPrice"
CkJsonObject_Rename $json "d" "change"
CkJsonObject_Rename $json "dp" "percentChange"
CkJsonObject_Rename $json "h" "high"
CkJsonObject_Rename $json "l" "low"
CkJsonObject_Rename $json "o" "open"
CkJsonObject_Rename $json "pc" "prevClose"
CkJsonObject_Rename $json "t" "unixTime"
puts [CkJsonObject_emit $json]
} else {
puts "Failed"
}
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $json