File size: 1,434 Bytes
1e8fcff
 
 
d6293df
 
 
1e8fcff
d6293df
 
 
 
 
 
1e8fcff
 
 
d6293df
 
 
 
 
1e8fcff
d6293df
1e8fcff
d6293df
 
 
 
 
1e8fcff
 
d6293df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();