memory / test-email.mjs
gcharanteja
b
d6293df
Raw
History Blame Contribute Delete
1.43 kB
const API_BASE = "https://maxxcarl-keyvault.hf.space/secrets";
const API_KEY = "Azure@123";
const { request } = await import("node:https");
function fetchSecret(k) {
return new Promise((resolve, reject) => {
const url = `${API_BASE}/${k}`;
request(url, { headers: { "X-API-Key": API_KEY, accept: "application/json" } }, (res) => {
let d = "";
res.on("data", (c) => d += c);
res.on("end", () => resolve(JSON.parse(d).value));
}).on("error", reject).end();
});
}
const [from, to, apiKey] = await Promise.all([
fetchSecret("FROM_EMAIL"),
fetchSecret("TO_EMAIL"),
fetchSecret("BREVO_API_KEY"),
]);
console.log(`from=${from} to=${to} apiKey=${apiKey.substring(0, 10)}...`);
const payload = JSON.stringify({
sender: { email: from },
to: [{ email: to }],
subject: "API Test",
textContent: "Sent via Brevo REST API",
});
const req = request({
hostname: "api.brevo.com",
port: 443,
path: "/v3/smtp/email",
method: "POST",
headers: {
"api-key": apiKey,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload),
},
}, (res) => {
let d = "";
res.on("data", (c) => d += c);
res.on("end", () => {
console.log(res.statusCode, d.substring(0, 200));
process.exit(res.statusCode === 201 || res.statusCode === 200 ? 0 : 1);
});
});
req.on("error", (e) => { console.error(e.message); process.exit(1); });
req.write(payload);
req.end();