Node.js
Node.js 18 and newer include fetch. Older runtimes should use a maintained HTTP client and keep the same headers and JSON body.
Legacy docs also reference an official package named @mailtarget/nodejs-sdk for Node 14 and newer, with helper methods such as sendMessage and sendMessageTemplate. Confirm current package status before making that package the default recommendation. The raw HTTP example below stays valid regardless of SDK packaging.
const res = await fetch("https://transmission.mailtarget.co/v1/layang/transmissions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.MAILTARGET_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
subject: "Hello from Mailtarget",
from: { email: "you@your-verified-domain.com", name: "Your Name" },
to: [{ email: "recipient@example.com" }],
bodyText: "If you can read this, the integration works.",
}),
});
const body = await res.json();
if (!res.ok) {
console.error(body);
process.exit(1);
}
console.log(body.transmissionId);
Production clients should add request timeouts, structured logging, and retry behavior only for retryable status codes.