/** * SendPier Send API — send a single message. * * Requirements: Node.js 18 or newer. Built-in fetch only, no packages. * * Usage: * export SENDPIER_API_KEY='sp_live_...' # the key of one verified domain * export SENDPIER_DOMAIN='example.com' # that same domain * export SENDPIER_FROM='noreply@example.com' # must live on that domain * node send-message.mjs recipient@example.org * * A successful call answers 202 Accepted and returns one identifier per * recipient. 202 means "accepted for delivery", not "delivered" -- ask * get-message.mjs later, or let a webhook tell you. */ const API_BASE = 'https://api.sendpier.com'; /** * One request to the Send API. * * Keep the key in the environment or in a secrets store, never in your source * and never anywhere a browser can read it: the Send API is meant for traffic * from your own server. */ async function sendpierRequest(method, path, body = null) { 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 headers = { Authorization: `Bearer ${key}`, // Required on every request. The key belongs to one domain and this header // has to name that same domain, or the answer is 403. 'X-Domain-Name': domain, Accept: 'application/json', }; if (body !== null) { headers['Content-Type'] = 'application/json'; } const response = await fetch(`${API_BASE}${path}`, { method, headers, body: body === null ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(30_000), }); const raw = await response.text(); let parsed = null; try { parsed = JSON.parse(raw); } catch { // A gateway between us and you may answer HTML; keep the raw text so the // status code and the body both end up in your log. } return { status: response.status, body: parsed, raw }; } const recipient = process.argv[2]; const from = process.env.SENDPIER_FROM; if (!recipient || !from) { console.error('usage: SENDPIER_FROM=noreply@example.com node send-message.mjs '); process.exit(64); } const response = await sendpierRequest('POST', '/v1/messages', { from: { email: from, name: 'Example' }, // Ten recipients fit in one request, but prefer one: then you know the status // per email and one bad address cannot take the rest down with it. to: [{ email: recipient }], subject: 'Hello from the Send API', text: 'This message was sent from Node.js.', html: '

This message was sent from Node.js.

', }); console.log(response.raw); if (response.status !== 202) { // Log the status code *and* the body: the body carries the error code that // says what to change. Retry 429 and 503 with a growing wait; a 422 will come // back in exactly the same way. console.error(`request failed with HTTP ${response.status}`); process.exit(1); } for (const message of response.body?.data ?? []) { // Store this identifier next to your own record: it is what you look the // message up with, and what a webhook reports back to you. console.error(`queued ${message.id} for ${message.to}`); }