STOCKAP DEVELOPER GUIDES
Build a stock price watchlist with Node.js
By StockAP · Updated November 25, 2026
Fetch AAPL and MSFT prices with a server-side Node.js stock API example, preserving stale flags and handling request limits.
This small watchlist fetches two stock quotes sequentially and prints one snapshot. It uses Node.js 20 or later with no npm dependencies. The example makes two requests when run; it does not start a polling worker.
Set up your API access
Sign in, create a key in your dashboard, and set STOCKAP_API_KEY in your server environment. Check current account permissions and limits. Never paste the key into browser JavaScript or commit it to Git.
Download and run
curl --fail -O https://stockap.com/examples/stock-watchlist.mjs node stock-watchlist.mjs
The key must already be set in your environment. Missing keys fail before any request. A successful run prints symbol, price, sourceTime and stale for AAPL and MSFT.
// Node.js 20+, no dependencies. Run on your server, never in a browser.
export async function watchlist(key, symbols = ['AAPL', 'MSFT'], request = fetch) {
if (!key) throw new Error('Set STOCKAP_API_KEY in your server environment.');
const rows = [];
for (const symbol of symbols) {
const url = new URL('https://stockap.com/v1/quotes');
url.search = new URLSearchParams({market:'stock', symbol});
const response = await request(url, {headers:{Authorization:`Bearer ${key}`}, signal:AbortSignal.timeout(30000)});
if (!response.ok) throw new Error(`HTTP ${response.status}: stop and check access/limits; do not retry in a loop.`);
const payload = await response.json();
const q = payload.data;
if (!q || typeof q !== 'object') throw new Error('Missing quote data');
rows.push({symbol, price:q.price ?? null, sourceTime:q.time ?? null, stale:q.stale ?? null});
}
return rows;
}
if (process.argv[1] && import.meta.url === (await import('node:url')).pathToFileURL(process.argv[1]).href) {
try { console.table(await watchlist(process.env.STOCKAP_API_KEY)); }
catch (error) { console.error(error.message); process.exitCode=1; }
}
Understand the output
sourceTime preserves the API quote timestamp in milliseconds; it is not the time your script ran. Null remains null. A stale stock quote outside regular trading hours can be expected. Do not label it an after-hours trade without source/session evidence.
Turn this into a shared watchlist
Run requests on your backend and share a short-lived cached snapshot across users. Do not run this script once per browser visitor. The example stops on HTTP errors, including 429; choose a bounded backoff policy within your account limits before adding scheduled refreshes.