Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * AlphaVantage.co simple jQuery API demo
- * API Documentation: https://www.alphavantage.co/documentation/
- *
- */
- jQuery(document).ready(function($) {
- // AlphaVantage.co API Key - get your own from https://www.alphavantage.co/support/#api-key and replace `demo` below
- var apiKey = 'demo';
- // Define stock symbol
- var symbol = 'BABA';
- // Build request URL
- var requestUrl = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + symbol + '&apikey=' + apiKey;
- // Get JSON-formatted data from the AlphaVantage.co API (AJAX request)
- $.getJSON( requestUrl, function( response ) {
- if ( response.Information ) {
- // Handle wrong API key response
- console.log(response.Information);
- } else if (response["Error Message"]) {
- // Handle error responses
- console.log(response["Error Message"]);
- } else {
- // Get last refreshed date
- var lastRefreshed = response["Meta Data"]["3. Last Refreshed"];
- // Get time series object
- var timeSeries = response["Time Series (Daily)"];
- // Get last item series
- var lastData = timeSeries[Object.keys(timeSeries)[0]];
- // Get stock close from object
- var close = lastData["4. close"];
- // Output message to console
- console.log(symbol + ' ' + close + ' (Last Refreshed at ' + lastRefreshed + ')');
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement