Lua Requires Chilkat v11.4.0+
Lua
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Replace with your actual Finnhub API key.
local apiKey = "YOUR_FINNHUB_API_KEY"
local symbol = "AAPL"
local http = chilkat.Http.new()
-- This is the URL without params.
local urlWithoutParams = "https://finnhub.io/api/v1/quote"
local req = chilkat.HttpRequest.new()
-- Add params that will be sent in the URL.
req:add_param("symbol", symbol)
req:add_param("token", apiKey)
req.http_verb = "GET"
-- Send the request to get the JSON response.
local resp = chilkat.HttpResponse.new()
local success = http:http_req(urlWithoutParams, req, resp)
if not success then
print(http.last_error_text)
return
end
local json = chilkat.JsonObject.new()
resp:get_body_json(json)
local statusCode = resp.status_code
print("response status code: " .. statusCode)
json.emit_compact = false
print(json: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 statusCode == 200 then
-- Add the symbol to the top of the result.
json:add_string_at(0, "symbol", symbol)
-- Rename members for clarification.
json:rename("c", "currentPrice")
json:rename("d", "change")
json:rename("dp", "percentChange")
json:rename("h", "high")
json:rename("l", "low")
json:rename("o", "open")
json:rename("pc", "prevClose")
json:rename("t", "unixTime")
print(json:emit())
else
print("Failed")
end