Dart Requires Chilkat v11.4.0+
Dart
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Replace with your actual Finnhub API key.
final apiKey = 'YOUR_FINNHUB_API_KEY';
final symbol = 'AAPL';
final http = CkHttp();
// This is the URL without params.
final urlWithoutParams = 'https://finnhub.io/api/v1/quote';
final req = CkHttpRequest();
// 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.
final resp = CkHttpResponse();
try {
http.httpReq(urlWithoutParams, req, resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final json = CkJsonObject();
resp.getBodyJson(json);
final statusCode = resp.statusCode;
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.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');
print(json.emit());
} else {
print('Failed');
}
}