Documentation
Sign inGet started

TypeScript · Bun

Send an email from a Bun.serve fetch handler with @messagebird/sdk — Bun runs TypeScript directly, no build step. You'll need a Bird API key — create one in Developers → API keys as covered in Send your first email, then put it in .env as BIRD_API_KEY (Bun loads .env automatically).

1. Install

Codevoorbeeld
bun init -y
bun add @messagebird/sdk
# npm install @messagebird/sdk
# pnpm add @messagebird/sdk
# yarn add @messagebird/sdk

2. Send

Create server.ts:
Codevoorbeeld
import { BirdClient } from "@messagebird/sdk";

const bird = new BirdClient({ apiKey: process.env.BIRD_API_KEY! });

Bun.serve({
  port: 3000,
  async fetch(req) {
    if (req.method !== "POST") return new Response("Not found", { status: 404 });

    const msg = await bird.email.send({
      from: "onboarding@messagebird.dev",
      to: ["delivered@messagebird.dev"],
      subject: "Hello from Bird",
      html: "<p>My first Bird email.</p>",
    });

    console.log(msg.id, msg.status); // "em_…", "accepted"
    return Response.json({ id: msg.id, status: msg.status });
  },
});
onboarding@messagebird.dev is Bird's shared onboarding sender and delivered@messagebird.dev is a sandbox recipient that always delivers — no domain verification needed. The region is inferred from your key's bk_us1_ / bk_eu1_ prefix, so there's no base URL to configure. The SDK auto-generates an Idempotency-Key per call, so retried sends are safe.

3. Try it

Run the server and POST to it:
Codevoorbeeld
bun run server.ts
curl -X POST http://localhost:3000/
The API responds with 202 — Bird has accepted the message and delivers it asynchronously:
Codevoorbeeld
{
  "id": "em_019c1930687b7bfa8a1b2c3d4e5f6789",
  "status": "accepted"
}

Next steps