Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.4.0+

Finnhub API - Get Stock Quote

See more AI Examples

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

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
apiKey: PWideChar;
symbol: PWideChar;
http: HCkHttp;
urlWithoutParams: PWideChar;
req: HCkHttpRequest;
resp: HCkHttpResponse;
json: HCkJsonObject;
statusCode: Integer;

begin
success := False;

//  Replace with your actual Finnhub API key.
apiKey := 'YOUR_FINNHUB_API_KEY';
symbol := 'AAPL';

http := CkHttp_Create();

//  This is the URL without params.
urlWithoutParams := 'https://finnhub.io/api/v1/quote';

req := CkHttpRequest_Create();

//  Add params that will be sent in the URL.
CkHttpRequest_AddParam(req,'symbol',symbol);
CkHttpRequest_AddParam(req,'token',apiKey);

CkHttpRequest_putHttpVerb(req,'GET');

//  Send the request to get the JSON response.
resp := CkHttpResponse_Create();
success := CkHttp_HttpReq(http,urlWithoutParams,req,resp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

json := CkJsonObject_Create();
CkHttpResponse_GetBodyJson(resp,json);

statusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('response status code: ' + IntToStr(statusCode));

CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(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
  begin
    //  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');

    Memo1.Lines.Add(CkJsonObject__emit(json));

  end
else
  begin
    Memo1.Lines.Add('Failed');
  end;

CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(json);