Your key never leaves your server.
-
Gather the parts
A Partner API key with access to
/v1/feed, Node 18 or later, andfeed-client.mjscopied from the API quickstart. -
Copy this file into your backend
Save it as
cache.mjsnext tofeed-client.mjs.cache.mjs// The same requests for 1 visitor or 10,000: reads the feed of your tickers at the pace it names and // keeps the result for every visitor's page. Copy it into your backend next to feed-client.mjs (Node 18 or // later, no packages). import { followFeed } from './feed-client.mjs'; export function startCallsCache({ key, tickers }) { let problem = null, ready = false; const feed = followFeed({ key, tickers, onChange: () => { problem = null; ready = true; }, onProblem: (error) => { problem = error.message; console.error(problem); }, }); return { // The newest `limit` calls on one ticker, or on all of them; never a request to Buzzberg. calls(ticker = '', limit = 20) { const symbol = String(ticker).trim().toUpperCase(); return [...feed.posts.values()] .filter((post) => !symbol || post.ticker === symbol) .sort((a, b) => b.published_at.localeCompare(a.published_at)) .slice(0, limit); }, // False until the first complete read: there are no calls to show yet. ready: () => ready, // True when the last read failed: the calls are the last ones that arrived. stale: () => problem !== null, stop: feed.stop, }; } -
Read it from your route handlers
Your serverimport { startCallsCache } from './cache.mjs'; const cache = startCallsCache({ key: process.env.BUZZBERG_API_KEY, tickers: ['BTC', 'ETH', 'NVDA'] }); // In the handler of your page's route. Before the first read, or while reads fail, send your page // without calls or mark it stale, and do not let a shared cache keep that response. const calls = cache.ready() ? cache.calls('NVDA') : []; // the newest 20 calls on NVDA, from the kept copy const stale = !cache.ready() || cache.stale();
What it handles
| Many visitors | No visitor waits on Buzzberg. |
| Before the first read | ready() is false and there are no calls yet. |
| Failed read | The last calls stay available and stale() is true. |
| Untracked ticker, 429, rejected key | The feed client leaves the untracked ticker out, waits Retry-After, or stops polling (feed client). |