/** * SendPier Send API — read how much of your allowance is left. * * Requirements: Node.js 18 or newer. Built-in fetch only, no packages. * * Usage: * export SENDPIER_API_KEY='sp_live_...' * export SENDPIER_DOMAIN='example.com' * node get-quota.mjs * * Check this before a large batch rather than after a 429: once the allowance * is spent, every further message is refused with quota_exceeded until the * period rolls over or you add credits. */ const API_BASE = 'https://api.sendpier.com'; /** One GET to the Send API. */ async function sendpierGet(path) { const key = process.env.SENDPIER_API_KEY; const domain = process.env.SENDPIER_DOMAIN; if (!key || !domain) { console.error('set SENDPIER_API_KEY and SENDPIER_DOMAIN first'); process.exit(78); } const response = await fetch(`${API_BASE}${path}`, { headers: { Authorization: `Bearer ${key}`, 'X-Domain-Name': domain, Accept: 'application/json', }, signal: AbortSignal.timeout(30_000), }); const raw = await response.text(); let parsed = null; try { parsed = JSON.parse(raw); } catch { // Keep the raw text: status code and body both belong in your log. } return { status: response.status, body: parsed, raw }; } const response = await sendpierGet('/v1/quota'); console.log(response.raw); if (response.status !== 200) { console.error(`request failed with HTTP ${response.status}`); process.exit(1); } const quota = response.body ?? {}; console.error( `${quota.domain}: ${quota.used_this_month} of ${quota.monthly_limit} used, ${quota.remaining} left this period` ); // A pause before you run out is cheaper than a batch that dies halfway. if ((quota.remaining ?? 0) < 100) { console.error('warning: fewer than 100 messages left'); }