Delphi ActiveX
Delphi ActiveX
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Delphi ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
apiKey: WideString;
symbol: WideString;
http: TChilkatHttp;
urlWithoutParams: WideString;
req: TChilkatHttpRequest;
resp: TChilkatHttpResponse;
json: TChilkatJsonObject;
statusCode: Integer;
begin
success := 0;
// Replace with your actual Finnhub API key.
apiKey := 'YOUR_FINNHUB_API_KEY';
symbol := 'AAPL';
http := TChilkatHttp.Create(Self);
// This is the URL without params.
urlWithoutParams := 'https://finnhub.io/api/v1/quote';
req := TChilkatHttpRequest.Create(Self);
// Add params that will be sent in the URL.
req.AddParam('symbol',symbol);
req.AddParam('token',apiKey);
req.HttpVerb := 'GET';
// Send the request to get the JSON response.
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpReq(urlWithoutParams,req.ControlInterface,resp.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
json := TChilkatJsonObject.Create(Self);
resp.GetBodyJson(json.ControlInterface);
statusCode := resp.StatusCode;
Memo1.Lines.Add('response status code: ' + IntToStr(statusCode));
json.EmitCompact := 0;
Memo1.Lines.Add(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
begin
// Add the symbol to the top of the result.
json.AddStringAt(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');
Memo1.Lines.Add(json.Emit());
end
else
begin
Memo1.Lines.Add('Failed');
end;
end;