Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
// dashboard-next: Data API developer console.
//
// The premium developer surface: buy/renew the monthly Premium pass on Solana
// ($THREE at a discount, SOL, or USDC), manage the x402_live_ API key it
// mints, watch usage, and copy working quickstarts against the news-archive
// Data API. Purchase is fully on-chain from the visitor's own wallet: quote,
// sign in Phantom, then poll /api/premium/subscribe until the pass lands.
//
// Session-authed like every dashboard page (requireUser), and the purchase is
// additionally wallet-bound: the session link is what enables key
// rotate/revoke on this page.
//
// Every panel renders independently. Pricing, pass status, and the corpus
// figures each come from a different endpoint, so one of them failing degrades
// its own card into a designed error state and leaves the rest of the console
// usable.
import { mountShell } from '../shell.js';
import { requireUser, get, post, esc } from '../api.js';
import {
skeletonHTML,
emptyStateHTML,
errorStateHTML,
ensureStateKitStyles,
attachRetry,
} from '../../shared/state-kit.js';
const WALLET_LS = 'threews:premium:wallet';
// A wallet-ownership signature is required to read the private half of
// /api/premium/status (keys, usage, purchase history). The server accepts it
// within a 5-minute window; we reuse a signature for 4 minutes so a single
// approval covers a normal session of key management without re-prompting.
const PROOF_TTL_MS = 4 * 60 * 1000;
// The only key string safe to put in a copyable snippet when we do not hold
// the plaintext. A stored key is only ever known by its prefix, and pasting a
// truncated prefix into a shell produces a 401, so never render one as if it
// were usable.
const KEY_PLACEHOLDER = 'x402_live_YOUR_KEY';
const state = {
plans: null,
plansError: null, // message when /api/premium/plans could not be read
wallet: localStorage.getItem(WALLET_LS) || null,
status: null, // /api/premium/status for state.wallet
statusError: null, // message when /api/premium/status could not be read
proof: null, // { wallet, signature, issuedAt, ts } ownership proof for keys/history
buying: null, // asset symbol while a purchase is in flight
freshKey: null, // plaintext shown exactly once after purchase/rotate
corpus: null, // live /api/news/archive?stats=true figures
feedSources: null, // live publisher-feed registry size
quickstartTab: 'curl',
};
// Panel-scoped status lines. Each renders next to the control that produced it,
// so a key-rotation failure never lands in the pricing card three screens up.
const notes = { status: null, buy: null, keys: null };
const $ = (id) => document.getElementById(id);
function fmtDate(iso) {
const d = new Date(iso);
return Number.isNaN(d.getTime())
? '-'
: d.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
}
function daysLeft(iso) {
return Math.max(0, Math.ceil((new Date(iso).getTime() - Date.now()) / 86_400_000));
}
function amountLabel(a) {
const n = Number(a.amount_atomics) / 10 ** a.decimals;
const rounded = n >= 1000 ? Math.round(n).toLocaleString() : n.toLocaleString(undefined, { maximumSignificantDigits: 4 });
return `${rounded} ${a.asset === 'THREE' ? '$THREE' : a.asset}`;
}
function setNote(slot, text, isError = false) {
notes[slot] = text ? { text, isError } : null;
paintNote(slot);
}
function paintNote(slot) {
const el = $(`da-${slot}-note`);
if (!el) return;
const n = notes[slot];
el.textContent = n ? n.text : '';
el.style.color = n?.isError ? 'var(--nxt-danger)' : 'var(--nxt-ink-dim)';
}
function noteHTML(slot) {
return `<p class="da-note" id="da-${slot}-note" role="status" aria-live="polite"></p>`;
}
function solanaProvider() {
return window.phantom?.solana || window.solana || null;
}
// Base64 of an ed25519 signature: the server (verifySiwsSignature) accepts base64.
function bytesToB64(bytes) {
const arr = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
let bin = '';
for (const b of arr) bin += String.fromCharCode(b);
return btoa(bin);
}
function freshProof() {
const p = state.proof;
return p && p.wallet === state.wallet && Date.now() - p.ts < PROOF_TTL_MS ? p : null;
}
// Prove control of the connected wallet so the server returns keys/usage/history.
// Reuses a still-fresh proof; otherwise asks the wallet to sign once. Returns the
// proof, or null if the wallet can't sign (falls back to public pass-state only).
async function ensureProof(provider) {
if (!state.wallet) return null;
const cached = freshProof();
if (cached) return cached;
if (!provider?.signMessage) return null;
const issuedAt = new Date().toISOString();
const message = `three.ws premium status\nWallet: ${state.wallet}\nIssued At: ${issuedAt}`;
const res = await provider.signMessage(new TextEncoder().encode(message), 'utf8');
const signature = bytesToB64(res?.signature ?? res);
state.proof = { wallet: state.wallet, signature, issuedAt, ts: Date.now() };
return state.proof;
}
// ── Clipboard ────────────────────────────────────────────────────────────────
// Reports honestly. navigator.clipboard is absent on insecure origins and can
// be refused by permission policy, and a button that claims "Copied" when the
// write threw is a lie the user only discovers on paste.
async function copyText(text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
return false;
}
}
function flashButton(btn, label, restore) {
if (!btn) return;
btn.textContent = label;
setTimeout(() => { if (btn.isConnected) btn.textContent = restore; }, 1800);
}
// Falls back to selecting the source node so the keyboard shortcut still works
// when the async clipboard API is unavailable.
function selectNode(el) {
if (!el || !window.getSelection) return;
const range = document.createRange();
range.selectNodeContents(el);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
// ── Purchase flow ────────────────────────────────────────────────────────────
async function connectWallet() {
const provider = solanaProvider();
if (!provider) {
throw new Error('No Solana wallet found. Install Phantom (phantom.com) and reload this page.');
}
const conn = await provider.connect();
const wallet = (conn?.publicKey || provider.publicKey)?.toString();
if (!wallet) throw new Error('Wallet did not return a public key.');
state.wallet = wallet;
localStorage.setItem(WALLET_LS, wallet);
return { provider, wallet };
}
async function pollSubscribe(quoteId, signature) {
// Confirmation usually lands in a few seconds; give it 90.
for (let i = 0; i < 30; i++) {
const out = await post('/api/premium/subscribe', { quote_id: quoteId, tx_signature: signature });
if (out?.pass) return out;
setNote('buy', `Payment sent. Waiting for Solana confirmation… (${i + 1})`);
await new Promise((r) => setTimeout(r, 3000));
}
throw new Error(`Transaction ${signature.slice(0, 8)}… did not confirm in time. Your funds are safe: reopen this page and the pass activates automatically once it lands.`);
}
async function buy(planId, asset) {
if (state.buying) return;
state.buying = `${planId}:${asset}`;
renderPricing();
try {
setNote('buy', 'Connecting wallet…');
const { provider, wallet } = await connectWallet();
setNote('buy', 'Locking the price and building your payment…');
const { quote, tx_base64 } = await post('/api/premium/quote', { asset, wallet, plan: planId });
setNote('buy', 'Confirm the payment in your wallet…');
const { VersionedTransaction } = await import('@solana/web3.js');
const bytes = Uint8Array.from(atob(tx_base64), (c) => c.charCodeAt(0));
const tx = VersionedTransaction.deserialize(bytes);
const sent = await provider.signAndSendTransaction(tx);
const signature = typeof sent === 'string' ? sent : sent?.signature;
if (!signature) throw new Error('Wallet did not return a transaction signature.');
const out = await pollSubscribe(quote.id, signature);
state.freshKey = out.api_key || null;
setNote('buy', '');
try { await ensureProof(provider); } catch { /* keys stay hidden until Verify */ }
await loadStatus();
} catch (err) {
if (!/user rejected|cancell?ed/i.test(err?.message || '')) {
setNote('buy', err?.message || 'Purchase failed. Nothing was charged beyond the on-chain transaction you approved.', true);
} else {
setNote('buy', '');
}
} finally {
state.buying = null;
renderPricing();
}
}
// ── Key management ───────────────────────────────────────────────────────────
async function keyAction(action, id) {
const verb = action === 'revoke' ? 'Revoke this API key? Anything using it stops working immediately.' : null;
if (verb && !window.confirm(verb)) return;
try {
setNote('keys', action === 'rotate' ? 'Rotating…' : 'Revoking…');
const out = await post('/api/premium/keys', { action, id });
if (out?.api_key) state.freshKey = out.api_key;
setNote('keys', '');
await loadStatus();
} catch (err) {
setNote('keys', err?.message === 'key_not_found' || err?.code === 'key_not_found'
? 'This key is not linked to your account. Keys bought without being signed in are managed through the API only.'
: (err?.message || 'Key action failed. Try again in a moment.'), true);
}
}
// ── Live corpus figures ──────────────────────────────────────────────────────
function corpusCount() {
const n = Number(state.corpus?.total_articles);
return Number.isFinite(n) && n > 0 ? n : null;
}
function corpusSinceYear() {
const d = new Date(state.corpus?.first_article_date || '');
return Number.isNaN(d.getTime()) ? null : d.getUTCFullYear();
}
// ── Rendering ────────────────────────────────────────────────────────────────
export function ledeText(corpus) {
const n = Number(corpus?.total_articles);
const size = Number.isFinite(n) && n > 0 ? `${n.toLocaleString()}-article ` : '';
const first = new Date(corpus?.first_article_date || '');
const since = Number.isNaN(first.getTime()) ? '' : ` Coverage runs back to ${first.getUTCFullYear()}.`;
return `The ${size}crypto-news archive as a developer product: a monthly pass in three tiers, paid on Solana in $THREE, SOL, or USDC, instead of a payment per call.${since}`;
}
function renderLede() {
const el = $('da-lede');
if (el) el.textContent = ledeText(state.corpus);
}
function renderStatusCard() {
const el = $('da-status');
if (!el) return;
if (!state.wallet) {
el.innerHTML = `
<div class="da-hero">
<div>
<div class="da-hero-kicker">Premium pass</div>
<div class="da-hero-title">Not connected</div>
<p class="da-hero-sub">Connect the Solana wallet you'll pay with to see your pass status, or buy below. The pass follows the wallet, the key follows your account.</p>
${noteHTML('status')}
</div>
<button class="dn-btn primary" id="da-connect" type="button">Connect wallet</button>
</div>`;
$('da-connect')?.addEventListener('click', async () => {
setNote('status', 'Connecting wallet…');
try { await verifyAndLoad(); setNote('status', ''); }
catch (e) { setNote('status', e.message, true); }
});
paintNote('status');
return;
}
const s = state.status;
const short = `${state.wallet.slice(0, 4)}…${state.wallet.slice(-4)}`;
if (state.statusError) {
el.innerHTML = `
<div class="da-hero da-hero-error">
<div>
<div class="da-hero-kicker">Premium pass · ${esc(short)}</div>
<div class="da-hero-title">Pass status unavailable</div>
<p class="da-hero-sub">${esc(state.statusError)} Your pass and key are unaffected: this page could not read them just now. Retry, or switch to a different wallet.</p>
${noteHTML('status')}
</div>
<div class="da-hero-actions">
<button class="dn-btn primary" id="da-status-retry" type="button">Retry</button>
<button class="dn-btn ghost" id="da-switch" type="button">Switch wallet</button>
</div>
</div>`;
$('da-status-retry')?.addEventListener('click', () => { loadStatus(); });
} else if (!s) {
el.innerHTML = `<div class="da-hero" aria-busy="true">${skeletonHTML(2, 'row')}</div>`;
return;
} else if (!s.active) {
el.innerHTML = `
<div class="da-hero">
<div>
<div class="da-hero-kicker">Premium pass · ${esc(short)}</div>
<div class="da-hero-title">No active pass</div>
<p class="da-hero-sub">Pick an asset below. One on-chain payment activates 30 days of unmetered archive search plus an API key. Paying in $THREE is the cheapest way in.</p>
${noteHTML('status')}
</div>
<button class="dn-btn ghost" id="da-switch" type="button">Switch wallet</button>
</div>`;
} else {
const left = daysLeft(s.pass.expires_at);
// A fresh ownership signature unlocks the key-management panel below.
const verifyBtn = freshProof()
? ''
: `<button class="dn-btn ghost" id="da-verify" type="button">Verify to manage keys</button>`;
el.innerHTML = `
<div class="da-hero da-hero-active">
<div>
<div class="da-hero-kicker">Premium pass · ${esc(short)}</div>
<div class="da-hero-title"><span class="da-dot"></span> Active, ${left} day${left === 1 ? '' : 's'} left</div>
<p class="da-hero-sub">Runs until ${esc(fmtDate(s.pass.expires_at))}. Renewing now appends 30 days to the end, with no lost time. Browser searches: choose “sign with wallet” in the payment dialog on <a href="/markets/archive">/markets/archive</a>.</p>
${noteHTML('status')}
</div>
<div class="da-hero-actions">${verifyBtn}<button class="dn-btn ghost" id="da-switch" type="button">Switch wallet</button></div>
</div>`;
}
$('da-verify')?.addEventListener('click', async () => {
setNote('status', 'Waiting for the signature in your wallet…');
try { await verifyAndLoad(); setNote('status', ''); }
catch (e) { setNote('status', e.message, true); }
});
$('da-switch')?.addEventListener('click', async () => {
localStorage.removeItem(WALLET_LS);
state.wallet = null;
state.status = null;
state.statusError = null;
state.proof = null;
setNote('status', '');
renderStatusCard();
try { await verifyAndLoad(); } catch (e) { setNote('status', e.message, true); }
});
paintNote('status');
}
function renderPricing() {
const el = $('da-pricing');
if (!el) return;
if (state.plansError) {
el.innerHTML = errorStateHTML({
title: 'Pricing is unavailable',
body: `${esc(state.plansError)} Your pass and API key are unaffected, and everything below still works.`,
});
return;
}
const plans = state.plans?.plans || [];
if (!plans.length) {
el.innerHTML = emptyStateHTML({
title: 'No passes on sale right now',
body: 'Premium plans are temporarily closed for purchase. Existing passes and API keys keep working until they expire.',
actions: [{ href: '/community', label: 'Talk to us', primary: true }],
});
return;
}
const active = Boolean(state.status?.active);
const fromUsd = Math.min(...plans.map((p) => Number(p.usd)));
const assetBtn = (plan, a) => {
const key = `${plan.id}:${a.asset}`;
const label = a.asset === 'THREE' ? '$THREE' : a.asset;
if (!a.available) {
return `<button class="da-paybtn" type="button" disabled title="${esc(a.reason || 'temporarily unavailable')}">${esc(label)} unavailable</button>`;
}
const busy = state.buying === key;
return `
<button class="da-paybtn ${a.asset === 'THREE' ? 'da-paybtn-three' : ''}" type="button"
data-buy-plan="${esc(plan.id)}" data-buy-asset="${esc(a.asset)}" ${state.buying ? 'disabled' : ''}
title="About $${Number(a.usd).toFixed(2)} in ${esc(label)}">
${busy ? 'Processing…' : `${esc(amountLabel(a))}${a.asset === 'THREE' ? ` <span class="da-off">−${Math.round((a.discount || 0) * 100)}%</span>` : ''}`}
</button>`;
};
const cards = plans.map((plan) => {
const hot = plan.id === 'pro';
return `
<div class="da-tiercard ${hot ? 'da-card-hot' : ''}">
${hot ? '<div class="da-card-badge">Most popular</div>' : ''}
<div class="da-card-asset">${esc(plan.tier)}</div>
<div class="da-card-amount">$${Number(plan.usd) % 1 ? Number(plan.usd).toFixed(2) : Number(plan.usd)}<span class="da-per">/${plan.days}d</span></div>
<ul class="da-feats">
<li><strong>${Number(plan.rate_limit_per_minute).toLocaleString()}</strong> requests/min</li>
<li>Unmetered archive search + API key</li>
<li>${plan.commercial ? 'Commercial use licensed' : 'Personal & evaluation use'}</li>
${plan.id === 'enterprise' ? '<li>Priority support · bulk corpus arrangements</li>' : ''}
</ul>
<p class="da-card-usd">${esc(plan.blurb || '')}</p>
<div class="da-paygroup" role="group" aria-label="Pay for ${esc(plan.tier)}">
${(plan.assets || []).map((a) => assetBtn(plan, a)).join('')}
</div>
</div>`;
}).join('');
el.innerHTML = `
<div class="dn-panel">
<div class="da-panel-head">
<h2>${active ? 'Renew or upgrade' : 'Go Premium'}, from $${fromUsd % 1 ? fromUsd.toFixed(2) : fromUsd}/30 days</h2>
<span class="da-tagline">Solana only · one transaction · pay in $THREE (20% off), SOL, or USDC</span>
</div>
<div class="da-cards da-cards-tiers">${cards}</div>
${noteHTML('buy')}
<p class="da-note" style="color:var(--nxt-ink-fade)">Buying a higher tier while a pass is active upgrades your key's rate limit immediately and appends the new period to the end. Enterprise needs something custom? <a href="/community" style="color:var(--nxt-accent)">Talk to us.</a></p>
</div>`;
el.querySelectorAll('[data-buy-plan]').forEach((b) =>
b.addEventListener('click', () => buy(b.dataset.buyPlan, b.dataset.buyAsset)),
);
paintNote('buy');
}
function renderFreshKey() {
const el = $('da-freshkey');
if (!el) return;
if (!state.freshKey) { el.innerHTML = ''; return; }
el.innerHTML = `
<div class="dn-panel da-fresh">
<div class="da-panel-head"><h2>Your API key, copy it now</h2></div>
<p class="da-note">This is the only time the full key is shown. It is already active.</p>
<div class="da-keyrow">
<code class="da-keycode" id="da-key-plain">${esc(state.freshKey)}</code>
<button class="dn-btn primary" id="da-key-copy" type="button">Copy</button>
</div>
</div>`;
$('da-key-copy')?.addEventListener('click', async () => {
const btn = $('da-key-copy');
if (await copyText(state.freshKey)) {
flashButton(btn, 'Copied ✓', 'Copy');
} else {
selectNode($('da-key-plain'));
flashButton(btn, 'Press Ctrl+C', 'Copy');
}
});
}
export function keysTableRows(keys) {
return keys.map((k) => `
<tr>
<td><code>${esc(k.key_prefix)}…</code></td>
<td>${k.status === 'active' ? '<span class="da-ok">active</span>' : `<span class="da-bad">${esc(k.status)}</span>`}</td>
<td class="da-num">${k.rate_limit_per_minute}/min</td>
<td>${esc(fmtDate(k.expires_at))}</td>
<td class="da-num">${Number(k.usage.granted).toLocaleString()}</td>
<td class="da-num">${Number(k.usage.denied).toLocaleString()}</td>
<td>${k.usage.last_seen ? esc(fmtDate(k.usage.last_seen)) : 'Never'}</td>
<td class="da-actions">
<button class="dn-btn ghost" type="button" data-rotate="${esc(k.id)}">Rotate</button>
<button class="dn-btn danger" type="button" data-revoke="${esc(k.id)}">Revoke</button>
</td>
</tr>`).join('');
}
function renderKeys() {
const el = $('da-keys');
if (!el) return;
const keys = state.status?.keys || [];
if (!state.wallet || !keys.length) {
// An active pass with no visible key means we haven't proved wallet
// ownership yet: the key exists server-side but is gated behind a signature.
const locked = state.status?.active && !freshProof();
el.innerHTML = locked
? `<div class="dn-panel">
<div class="da-panel-head"><h2>API key</h2></div>
<p class="da-note">Your key is protected. <button class="da-inline-link" id="da-keys-verify" type="button">Verify this wallet</button> to view its prefix, usage, and rotate/revoke controls.</p>
${noteHTML('keys')}
</div>`
: `<div class="dn-panel">
<div class="da-panel-head"><h2>API key</h2></div>
<p class="da-note">No key yet. Buying a pass mints one automatically (<code>x402_live_…</code>). It bypasses the per-call x402 charge on every premium endpoint via the <code>X-API-Key</code> header.</p>
${noteHTML('keys')}
</div>`;
$('da-keys-verify')?.addEventListener('click', async () => {
setNote('keys', 'Waiting for the signature in your wallet…');
try { await verifyAndLoad(); setNote('keys', ''); }
catch (e) { setNote('keys', e.message, true); }
});
paintNote('keys');
return;
}
el.innerHTML = `
<div class="dn-panel">
<div class="da-panel-head"><h2>API key</h2><span class="da-tagline">usage from the access log: granted vs denied calls</span></div>
<div class="da-scroll" tabindex="0" role="region" aria-label="API keys and usage">
<table class="da-table">
<thead><tr><th>Key</th><th>Status</th><th class="da-num">Rate limit</th><th>Expires</th><th class="da-num">Granted</th><th class="da-num">Denied</th><th>Last used</th><th><span class="da-visually-hidden">Actions</span></th></tr></thead>
<tbody>${keysTableRows(keys)}</tbody>
</table>
</div>
${noteHTML('keys')}
</div>`;
el.querySelectorAll('[data-rotate]').forEach((b) => b.addEventListener('click', () => keyAction('rotate', b.dataset.rotate)));
el.querySelectorAll('[data-revoke]').forEach((b) => b.addEventListener('click', () => keyAction('revoke', b.dataset.revoke)));
paintNote('keys');
}
// The key that goes into a copyable snippet. Only the plaintext minted this
// session is real; a stored key is known by prefix alone, so anything else
// renders the honest placeholder.
export function snippetKey({ freshKey } = {}) {
return freshKey || KEY_PLACEHOLDER;
}
export function quickstartSnippets(key) {
return {
curl: `curl "https://three.ws/api/news/archive?q=bitcoin+etf&start_date=2024-01-01&end_date=2024-03-31&limit=50" \\
-H "X-API-Key: ${key}"`,
javascript: `const res = await fetch(
'https://three.ws/api/news/archive?' + new URLSearchParams({
ticker: 'BTC', sentiment: 'negative', start_date: '2022-11-01', end_date: '2022-11-30',
}),
{ headers: { 'X-API-Key': '${key}' } },
);
const { articles, scanned } = await res.json();`,
mcp: `// claude_desktop_config.json / .mcp.json, crypto_news_archive tool
{
"mcpServers": {
"three-ws": { "command": "npx", "args": ["-y", "@three-ws/mcp-server"] }
}
}`,
};
}
function renderQuickstart() {
const el = $('da-quickstart');
if (!el) return;
const key = snippetKey(state);
const snippets = quickstartSnippets(key);
if (!snippets[state.quickstartTab]) state.quickstartTab = 'curl';
const label = (t) => (t === 'javascript' ? 'JavaScript' : t.toUpperCase());
const tabs = Object.keys(snippets).map((t) =>
`<button class="da-tab" type="button" data-tab="${t}" aria-pressed="${t === state.quickstartTab}">${label(t)}</button>`,
).join('');
// The placeholder is only worth explaining when the visitor actually owns a
// key they cannot see the plaintext of.
const hasStoredKey = Boolean(state.status?.keys?.length);
const keyHint = key === KEY_PLACEHOLDER && hasStoredKey
? '<p class="da-note">Swap <code>x402_live_YOUR_KEY</code> for the key you saved at purchase. Lost it? Rotate above to mint a fresh one.</p>'
: '';
el.innerHTML = `
<div class="dn-panel">
<div class="da-panel-head"><h2>Quickstart</h2><span class="da-tagline">works the moment your pass is active</span></div>
<div class="da-tabs" role="group" aria-label="Quickstart language">${tabs}</div>
<pre class="da-code" id="da-snippet" tabindex="0" role="region" aria-label="Quickstart snippet"><code>${esc(snippets[state.quickstartTab])}</code></pre>
<button class="dn-btn ghost" id="da-snippet-copy" type="button">Copy snippet</button>
${keyHint}
</div>`;
el.querySelectorAll('.da-tab').forEach((b) =>
b.addEventListener('click', () => {
state.quickstartTab = b.dataset.tab;
el.querySelectorAll('.da-tab').forEach((x) => x.setAttribute('aria-pressed', String(x === b)));
$('da-snippet').innerHTML = `<code>${esc(snippets[state.quickstartTab])}</code>`;
}),
);
$('da-snippet-copy')?.addEventListener('click', async () => {
const btn = $('da-snippet-copy');
if (await copyText(snippets[state.quickstartTab])) {
flashButton(btn, 'Copied ✓', 'Copy snippet');
} else {
selectNode($('da-snippet'));
flashButton(btn, 'Press Ctrl+C', 'Copy snippet');
}
});
}
// The endpoint catalog. Article and feed counts come from the live corpus so
// the table cannot drift away from what the API actually serves; the wording
// stays true (just less specific) when those probes have not landed yet.
export function catalogRows({ articles, sinceYear, feedSources } = {}) {
const archiveWhat = articles
? `Search ${articles.toLocaleString()} articles${sinceYear ? ` back to ${sinceYear}` : ''} by keyword, ticker, source, date, sentiment, or language`
: 'Search the full archive by keyword, ticker, source, date, sentiment, or language';
const feedWhat = feedSources
? `Live headlines from ${feedSources.toLocaleString()} publisher feeds`
: 'Live headlines from the publisher feed registry';
return [
['GET /api/news/archive', archiveWhat, 'Premium · 60/day free · $0.001/search'],
['GET /api/news/archive?stats=true', 'Corpus statistics and month range', 'Free'],
['GET /api/news/archive?trending=true', 'Most-covered tickers of the newest archive weeks', 'Free'],
['GET /api/news/feed', feedWhat, 'Free'],
['GET /api/news/digest', 'Last 1 to 72 h clustered into narratives with stance and tickers', 'Free'],
];
}
function renderCatalog() {
const el = $('da-catalog');
if (!el) return;
const rows = catalogRows({
articles: corpusCount(),
sinceYear: corpusSinceYear(),
feedSources: state.feedSources,
}).map(([ep, what, access]) => `
<tr><td><code>${esc(ep)}</code></td><td>${esc(what)}</td><td>${esc(access)}</td></tr>`).join('');
el.innerHTML = `
<div class="dn-panel">
<div class="da-panel-head"><h2>Endpoints</h2><a class="da-tagline" href="/docs/api-reference">full API reference →</a></div>
<div class="da-scroll" tabindex="0" role="region" aria-label="Data API endpoints">
<table class="da-table">
<thead><tr><th>Endpoint</th><th>What it does</th><th>Access</th></tr></thead>
<tbody>${rows}</tbody>
</table>
</div>
</div>`;
}
function renderAll() {
renderLede();
renderStatusCard();
renderPricing();
renderFreshKey();
renderKeys();
renderQuickstart();
renderCatalog();
}
// ── Data ─────────────────────────────────────────────────────────────────────
async function loadStatus() {
if (!state.wallet) {
state.statusError = null;
renderAll();
return;
}
try {
const p = freshProof();
const auth = p
? `&signature=${encodeURIComponent(p.signature)}&issuedAt=${encodeURIComponent(p.issuedAt)}`
: '';
state.status = await get(`/api/premium/status?wallet=${encodeURIComponent(state.wallet)}${auth}`);
state.statusError = null;
} catch (err) {
state.status = null;
state.statusError = err?.message || 'The pass service is unreachable.';
}
renderAll();
}
// Free, public, and slow enough to be worth keeping off the critical path: the
// pass UI never waits on either probe, and each upgrades its copy in place as
// it lands. Kept as two independent fetches on purpose, because the feed
// registry takes seconds and would otherwise hold the article count hostage.
function loadCorpus() {
get('/api/news/archive?stats=true')
.then((out) => {
state.corpus = out?.stats || null;
renderLede();
renderCatalog();
})
.catch(() => { /* the lede and catalog stay on their number-free wording */ });
get('/api/news/feed?meta=1&limit=1')
.then((out) => {
const n = Array.isArray(out?.sources) ? out.sources.length : 0;
state.feedSources = n > 0 ? n : null;
renderCatalog();
})
.catch(() => { /* the feed row stays on its number-free wording */ });
}
// Connect the wallet, prove ownership, then reload the (now full) status.
async function verifyAndLoad() {
const { provider } = await connectWallet();
await ensureProof(provider);
await loadStatus();
}
function injectStyles() {
if (document.getElementById('da-styles')) return;
ensureStateKitStyles();
const s = document.createElement('style');
s.id = 'da-styles';
s.textContent = `
.da-root { display: flex; flex-direction: column; gap: 1.25rem; padding-bottom: 3rem; }
.da-visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.da-hero { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; background: var(--nxt-glass); border: 1px solid var(--nxt-stroke); border-radius: var(--nxt-radius-sm); padding: 1.2rem 1.4rem; }
.da-hero-active { border-left: 3px solid var(--nxt-success); }
.da-hero-error { border-left: 3px solid var(--nxt-danger); }
.da-hero-kicker { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--nxt-ink-fade); }
.da-hero-title { font-size: 1.35rem; font-weight: 680; color: var(--nxt-ink); margin: .2rem 0; display: flex; align-items: center; gap: .5rem; }
.da-hero-sub { margin: 0; font-size: .85rem; color: var(--nxt-ink-dim); line-height: 1.55; max-width: 60ch; }
.da-hero-sub a { color: var(--nxt-accent); }
.da-hero-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.da-inline-link { background: none; border: none; padding: 0; font: inherit; color: var(--nxt-accent); cursor: pointer; text-decoration: underline; }
.da-inline-link:hover { color: var(--nxt-ink); }
.da-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--nxt-success); box-shadow: 0 0 8px var(--nxt-success); display: inline-block; }
.da-panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; flex-wrap: wrap; margin-bottom: .9rem; }
.da-panel-head h2 { margin: 0; font-size: .95rem; font-weight: 650; color: var(--nxt-ink); }
.da-tagline { font-size: .74rem; color: var(--nxt-ink-fade); }
.da-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); gap: .9rem; }
.da-card-hot { border-color: color-mix(in srgb, var(--nxt-accent) 55%, transparent); background: color-mix(in srgb, var(--nxt-accent) 6%, transparent); }
.da-card-badge { position: absolute; top: -.6rem; left: .8rem; font-size: .62rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--nxt-accent); background: var(--nxt-bg-0); border: 1px solid color-mix(in srgb, var(--nxt-accent) 55%, transparent); padding: .12rem .5rem; border-radius: var(--nxt-radius-pill); }
.da-card-asset { font-size: .78rem; color: var(--nxt-ink-dim); font-weight: 600; }
.da-card-amount { font-size: 1.3rem; font-weight: 700; color: var(--nxt-ink); letter-spacing: -.01em; }
.da-card-usd { font-size: .74rem; color: var(--nxt-ink-fade); margin-bottom: .55rem; }
.da-cards-tiers { grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr)); align-items: stretch; }
.da-tiercard { position: relative; border: 1px solid var(--nxt-stroke); border-radius: var(--nxt-radius-sm); padding: 1.2rem 1.1rem 1rem; display: flex; flex-direction: column; gap: .4rem; transition: border-color .15s ease, transform .15s ease; }
.da-tiercard:hover { border-color: var(--nxt-stroke-strong); transform: translateY(-1px); }
.da-tiercard button:focus-visible { outline: 2px solid var(--nxt-accent); outline-offset: 2px; }
.da-per { font-size: .8rem; font-weight: 500; color: var(--nxt-ink-fade); margin-left: .15rem; }
.da-feats { list-style: none; margin: .2rem 0 .3rem; padding: 0; display: flex; flex-direction: column; gap: .3rem; }
.da-feats li { font-size: .78rem; color: var(--nxt-ink-dim); padding-left: 1.1rem; position: relative; }
.da-feats li::before { content: '✓'; position: absolute; left: 0; color: var(--nxt-success); font-weight: 700; }
.da-paygroup { display: flex; flex-direction: column; gap: .4rem; margin-top: auto; }
.da-paybtn { display: flex; align-items: center; justify-content: center; gap: .4rem; width: 100%; border: 1px solid var(--nxt-stroke); background: color-mix(in srgb, var(--nxt-ink) 4%, transparent); color: var(--nxt-ink); border-radius: var(--nxt-radius-sm); font-size: .8rem; font-weight: 600; padding: .5rem .7rem; cursor: pointer; transition: border-color .12s ease, background .12s ease; }
.da-paybtn:hover:not(:disabled) { border-color: var(--nxt-stroke-strong); background: color-mix(in srgb, var(--nxt-ink) 8%, transparent); }
.da-paybtn:focus-visible { outline: 2px solid var(--nxt-accent); outline-offset: 2px; }
.da-paybtn:disabled { opacity: .55; cursor: default; }
.da-paybtn-three { border-color: color-mix(in srgb, var(--nxt-accent) 55%, transparent); background: color-mix(in srgb, var(--nxt-accent) 8%, transparent); }
.da-off { font-size: .68rem; font-weight: 700; color: var(--nxt-accent); }
.da-note { margin: .8rem 0 0; font-size: .8rem; color: var(--nxt-ink-dim); line-height: 1.5; min-height: 1em; }
.da-fresh { border-color: color-mix(in srgb, var(--nxt-success) 50%, transparent); }
.da-keyrow { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-top: .6rem; }
.da-keycode { font-family: var(--nxt-mono, ui-monospace, monospace); font-size: .82rem; background: color-mix(in srgb, var(--nxt-ink) 7%, transparent); padding: .5rem .7rem; border-radius: 6px; word-break: break-all; user-select: all; }
.da-scroll { overflow-x: auto; }
.da-scroll:focus-visible, .da-code:focus-visible { outline: 2px solid var(--nxt-accent); outline-offset: 2px; }
.da-table { width: 100%; border-collapse: collapse; font-size: .82rem; }
.da-table th { text-align: left; font-weight: 600; color: var(--nxt-ink-fade); font-size: .7rem; text-transform: uppercase; letter-spacing: .05em; padding: .35rem .5rem; border-bottom: 1px solid var(--nxt-stroke); white-space: nowrap; }
.da-table td { padding: .45rem .5rem; border-bottom: 1px solid color-mix(in srgb, var(--nxt-stroke) 55%, transparent); color: var(--nxt-ink); vertical-align: middle; }
.da-num { text-align: right; font-variant-numeric: tabular-nums; }
.da-ok { color: var(--nxt-success); font-weight: 600; }
.da-bad { color: var(--nxt-danger); font-weight: 600; }
.da-actions { white-space: nowrap; display: flex; gap: .4rem; }
.da-tabs { display: flex; gap: .4rem; margin-bottom: .6rem; flex-wrap: wrap; }
.da-tab { background: none; border: 1px solid var(--nxt-stroke); color: var(--nxt-ink-dim); border-radius: var(--nxt-radius-pill); font-size: .74rem; padding: .3rem .8rem; cursor: pointer; transition: all .12s ease; }
.da-tab[aria-pressed="true"] { color: var(--nxt-ink); border-color: var(--nxt-stroke-strong); background: color-mix(in srgb, var(--nxt-ink) 6%, transparent); }
.da-tab:focus-visible { outline: 2px solid var(--nxt-accent); outline-offset: 2px; }
.da-code { background: color-mix(in srgb, var(--nxt-ink) 6%, transparent); border: 1px solid var(--nxt-stroke); border-radius: var(--nxt-radius-sm); padding: .9rem 1rem; overflow-x: auto; font-size: .78rem; line-height: 1.55; margin: 0 0 .7rem; }
.da-code code { font-family: var(--nxt-mono, ui-monospace, monospace); white-space: pre; }
@media (prefers-reduced-motion: reduce) {
.da-tiercard, .da-paybtn, .da-tab { transition: none; }
.da-tiercard:hover { transform: none; }
}
`;
document.head.appendChild(s);
}
(async function boot() {
const main = await mountShell();
await requireUser();
injectStyles();
main.innerHTML = `
<div style="margin-bottom:1.5rem">
<h1 class="dn-h1" style="margin-bottom:.25rem">Data API</h1>
<p class="dn-h1-sub" id="da-lede" style="margin:0">${esc(ledeText(null))}</p>
</div>
<div class="da-root" id="da-root">
<div id="da-status">${`<div class="da-hero" aria-busy="true">${skeletonHTML(2, 'row')}</div>`}</div>
<div id="da-freshkey"></div>
<div id="da-pricing">${`<div class="dn-panel">${skeletonHTML(3, 'row')}</div>`}</div>
<div id="da-keys"></div>
<div id="da-quickstart"></div>
<div id="da-catalog"></div>
</div>`;
attachRetry($('da-root'), () => boot2());
async function boot2() {
state.plansError = null;
try {
state.plans = await get('/api/premium/plans');
} catch (err) {
state.plans = null;
state.plansError = err?.message || 'The pricing endpoint is unreachable.';
}
// Runs whatever happened above, so a pricing outage never blanks the
// pass card, the key table, the quickstarts, or the endpoint catalog.
await loadStatus();
}
await boot2();
loadCorpus();
})().catch((err) => {
const main = document.querySelector('.dn-main-inner') || document.body;
main.innerHTML = `<h1 class="dn-h1">Data API</h1><div class="dn-panel"><div class="dn-panel-title" style="color:var(--nxt-danger)">Failed to load</div><div class="dn-panel-sub">${String(err?.message || 'unknown').replace(/</g, '<')}</div></div>`;
});