Sending examples
Send with Node.js
Send email from any Node.js 18+ project using the built-in fetch.
Prerequisites
- A Sendtern API key stored as
SENDTERN_API_KEY - Node.js 18 or newer, which ships with
fetch
- 1
Send an email
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
Check the response
A successful request returns
200 OKwith the email id. Use it to look the email up later.1{ 2 "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d" 3}
1node --env-file=.env send.mjs
Replace the example sender with an address on your verified domain.