Skip to main content

JavaScript

Use JavaScript from a server context or through your own backend route. Do not expose a Mailtarget API key in browser code.

const response = 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.",
}),
});

if (!response.ok) {
throw new Error(`Mailtarget request failed: ${response.status}`);
}

const data = await response.json();
console.log(data.transmissionId);

Related docs: