Invio di esempi
Invia con Node.js
Invia email da qualsiasi progetto Node.js 18+ utilizzando il fetch integrato.
Prerequisiti
- Un Sendtern chiave API memorizzato come
SENDTERN_API_KEY - Node.js 18 or newer, which ships with
fetch
- 1
Invia un messaggio
Create
send.mjs. No package is needed.1// send.mjs 2const res = await fetch("https://sendtern.com/emails", { 3 method: "POST", 4 headers: { 5 Authorization: `Bearer ${process.env.SENDTERN_API_KEY}`, 6 "Content-Type": "application/json", 7 "Idempotency-Key": crypto.randomUUID(), 8 }, 9 body: JSON.stringify({ 10 from: "Acme <hello@mail.example.com>", 11 to: ["you@example.com"], 12 subject: "Hello from Node.js", 13 html: "<p>It works!</p>", 14 }), 15}); 16 17if (!res.ok) throw new Error((await res.json()).message); 18console.log(await res.json()); - 2
Controlla la risposta
Una richiesta riuscita restituisce
200 OKcon l'ID email. Usalo per cercare l'email in seguito.1{ 2 "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d" 3}
1node --env-file=.env send.mjs
Sostituisci il mittente di esempio con un indirizzo sul tuo dominio verificato.