![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) Finnhub API - Get Stock QuoteSee more AI ExamplesDemonstrates how to get a stock quote from the Finnhub API.Note: This example requires Chilkat v11.4.0 or greater.
func chilkatTest() { var success: Bool = false // Replace with your actual Finnhub API key. var apiKey: String? = "YOUR_FINNHUB_API_KEY" var symbol: String? = "AAPL" let http = CkoHttp()! // This is the URL without params. var urlWithoutParams: String? = "https://finnhub.io/api/v1/quote" let req = CkoHttpRequest()! // Add params that will be sent in the URL. req.addParam("symbol", value: symbol) req.addParam("token", value: apiKey) req.httpVerb = "GET" // Send the request to get the JSON response. let resp = CkoHttpResponse()! success = http.httpReq(urlWithoutParams, request: req, response: resp) if success == false { print("\(http.lastErrorText!)") return } let json = CkoJsonObject()! resp.getBodyJson(json) var statusCode: Int = resp.statusCode.intValue print("response status code: \(statusCode)") json.emitCompact = 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 { // Add the symbol to the top of the result. json.addString(at: 0, name: "symbol", value: symbol) // Rename members for clarification. json.rename("c", newName: "currentPrice") json.rename("d", newName: "change") json.rename("dp", newName: "percentChange") json.rename("h", newName: "high") json.rename("l", newName: "low") json.rename("o", newName: "open") json.rename("pc", newName: "prevClose") json.rename("t", newName: "unixTime") print("\(json.emit()!)") } else { print("Failed") } } |
||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.