React Native Requires Chilkat v11.4.0+
React Native
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat React Native Downloads
import { Http, HttpRequest, HttpResponse, JsonObject } from '@chilkat/react-native'
async function chilkatExample() {
// Replace with your actual Finnhub API key.
const apiKey = 'YOUR_FINNHUB_API_KEY';
const symbolArg = 'AAPL';
const http = new Http();
// This is the URL without params.
const urlWithoutParams = 'https://finnhub.io/api/v1/quote';
const req = new HttpRequest();
// Add params that will be sent in the URL.
req.addParam('symbol', symbolArg);
req.addParam('token', apiKey);
req.httpVerb = 'GET';
// Send the request to get the JSON response.
const resp = new HttpResponse();
try {
await http.httpReqAsync(urlWithoutParams, req, resp);
} catch {
console.log(http.lastErrorText);
return;
}
const json = new JsonObject();
resp.getBodyJson(json);
const statusCode = resp.statusCode;
console.log(`response status code: ${statusCode}`);
json.emitCompact = false;
console.log(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', symbolArg);
// 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');
console.log(json.emit());
} else {
console.log('Failed');
}
}