Sending high-concurrency transactional emails in Bun runtime using native fetch and AutoSend API

We replaced Node.js with Bun for our API gateway. How does Bun’s native HTTP client perform when dispatching transactional emails to AutoSend, and how do you structure the request handler?

Bun Runtime + AutoSend Native Integration

Bun includes a blazingly fast native C++ fetch implementation that requires zero external dependencies.

// server.ts (Bun)
const server = Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);

    if (req.method === "POST" && url.pathname === "/api/send-otp") {
      const { email, otp } = await req.json();

      const res = await fetch("https://api.autosend.com/v1/mails/send", {
        method: "POST",
        headers: {
          "Authorization": `Bearer ${process.env.AUTOSEND_API_KEY}`,
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          from: { email: "auth@yourdomain.com", name: "Security Bot" },
          to: { email },
          subject: `Your Login Code: ${otp}`,
          html: `<p>Your single-use sign-in code is: <strong>${otp}</strong></p>`
        })
      });

      if (!res.ok) {
        return new Response(JSON.stringify({ error: "Failed" }), { status: 500 });
      }

      return new Response(JSON.stringify({ success: true }), { status: 200 });
    }

    return new Response("Not found", { status: 404 });
  },
});

console.log(`Bun server running at http://localhost:${server.port}`);

Benchmark Note: Bun’s native connection reuse completes AutoSend HTTP dispatches in under 18 milliseconds on edge infrastructure.

Relevant Documentation